How to specify Java Source and Target Compatibility in a Gradle multi project
I have a multiple project setup and I want to build top level projects using different JDKs.
I did some experiments by myself and the following configuration worked for me
(using gradle 3.1 and the wrapper function).
At the root level my gradle.properties file looks like this:
org.gradle.daemon=true JDK7_HOME=/usr/lib/jvm/jdk-7-oracle-x64 JDK8_HOME=/usr/lib/jvm/oracle-java8-jdk-amd64
Default JDK should be JDK 7. So I defined this in my root build.gradle file
... subprojects { apply plugin: 'java' sourceCompatibility = 1.7 targetCompatibility = 1.7 compileJava.doFirst { println 'source compatibility: ' + sourceCompatibility println 'targetCompatibility: ' + targetCompatibility } tasks.withType(JavaCompile) { if (project.hasProperty('JDK7_HOME')) { options.fork = true options.bootClasspath = '$JDK7_HOME/jre/lib/rt.jar:$JDK7_HOME/jre/lib/jce.jar' } } test.doFirst { println 'executing tests with JDK ' + test.executable } tasks.withType(Test) { if (project.hasProperty('JDK7_HOME')) { test.executable = '$JDK7_HOME/bin/java' } } ... } ...
With this configuration the source and target compatibility is now set to 1.7 for every
sub project. I use compileJava.doFirst and test.doFirst to verify that everything works as expected.
Now, in my sub project B I want to use Java 8. The following did the trick:
build.gradle
... tasks.withType(JavaCompile) { if (project.hasProperty('JDK8_HOME')) { sourceCompatibility = 1.8 targetCompatibility = 1.8 options.fork = true options.bootClasspath = '$JDK8_HOME/jre/lib/rt.jar:$JDK8_HOME/jre/lib/jce.jar'; } } tasks.withType(Test) { if (project.hasProperty('JDK8_HOME')) { test.executable = '$JDK8_HOME/bin/java' } } ...
That it’s. I hope this has helped you.
If not, please check these ones:
http://stackoverflow.com/questions/21028438/gradle-sourcecompatibility-has-no-effect-to-subprojects