// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}


// apply plugin: 'build-dashboard'

allprojects {
	apply plugin: 'java'
	apply plugin: 'maven'
	apply plugin: "jacoco"
	apply plugin: 'eclipse'

	eclipse {
		classpath {
			downloadSources = true
		}
	}

	group = 'house.intelli'
	version = '0.9.1-SNAPSHOT'

	sourceCompatibility = JavaVersion.VERSION_1_8
	targetCompatibility = JavaVersion.VERSION_1_8

	[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

	repositories {
		jcenter()

		// This sucks: mavenLocal() must be last according to
		// http://stackoverflow.com/questions/10156847/how-to-tell-gradle-to-download-all-the-source-jars#10655347
		// in order to get sources. But it must be first, if we want
		// to make sure that locally built stuff is used
		// instead of an older version built on the Jenkins.
		mavenLocal()

		maven {
			url 'https://codewizards.co/maven/release'
		}
		maven {
			url 'https://codewizards.co/maven/snapshot'
		}
	}

	project.ext.intellihouseVersion = version

	project.ext.bouncycastleVersion = '1.60'
	project.ext.bouncycastleWotVersion = bouncycastleVersion + '.0';

	project.ext.slf4jVersion = '1.7.21'
	project.ext.logbackVersion = '1.1.7'
	project.ext.springVersion = '4.3.5.RELEASE'
	project.ext.postgresqlVersion = '42.2.5.jre7'
	project.ext.jaxbVersion='2.4.0-b180830.0438'

	sourceSets {
		main {
			java {
				srcDir 'src/main/java'
			}
			resources {
				srcDir 'src/main/java'
				srcDir 'src/main/resources'
			}
		}

		test {
			java {
				srcDir 'src/test/java'
			}
			resources {
				srcDir 'src/test/java'
				srcDir 'src/test/resources'
			}
		}
	}

	eclipse {
		classpath {
			downloadSources = true
		}
	}
}

subprojects {
	apply plugin: 'eclipse'
	apply plugin: 'osgi'

	dependencies {
		compile "org.slf4j:slf4j-api:$slf4jVersion"
//		compile "org.apache.commons:commons-lang3:3.4"
		compile "org.apache.commons:commons-collections4:4.0"
		// jmockit must be *before* junit.
		testCompile 'org.jmockit:jmockit:1.24', 'junit:junit:4.12', 'org.assertj:assertj-core:2.4.1' // assertj 3.4.1 requires Java 8 - using the newest one that works with Java 7.
		testCompile "org.slf4j:log4j-over-slf4j:$slf4jVersion", "ch.qos.logback:logback-classic:$logbackVersion"
	}

	jar {
		manifest {
			def manifestFile = "${projectDir}/src/main/resources/META-INF/MANIFEST.MF"
			if ( new File( manifestFile ).exists() )
				from (manifestFile)

			instruction 'Service-Component', 'OSGI-INF/*.xml'
		}
		manifest.attributes Provider: 'CodeWizards GmbH'
	}

	tasks.withType(Test) {
		jvmArgs "-XX:MaxPermSize=256m"
		jvmArgs "-Xmx512m"

		String igFa = System.properties['test.ignoreFailures'];
		ignoreFailures = igFa == null ? false : Boolean.parseBoolean(igFa);
// We run the tests in parallel (separate processes)! I copied this from subshare, where this increased performance significantly.
		maxParallelForks = Runtime.runtime.availableProcessors();
	}
}

task copyDistributions(type: Copy) {
	into "$buildDir/distributions"
	from 'house.intelli.android/build/distributions'
	from 'house.intelli.desktop/build/distributions'
	from 'house.intelli.raspi/build/distributions'

	// This task depends on 'subprojects.build' in order to run *after* all sub-projects
	// have been built. Without this, it would run before the sub-projects (when there's
	// nothing to be copied, yet).
	dependsOn subprojects.build
}

// The 'copyDistributions' task should always run when 'build' runs.
build.finalizedBy copyDistributions
