I have a multi project build which should use different Java version. That I have set a different sourceCompatibility. Have set the flag fork = true. The different Java versions are installed but it seems to use only one Java version for all scripts.
How can I use different Java versions for compiling in one project. With ANT was this not a problem.
I have add the follow to our base script. This solve the problem for all our scripts without any change. I had expected that gradle do this job for us.
/**
* Find the javac executable for the current sourceCompatibility. If the current JVM match then it return null.
*/
String findJavacExecutable() {
File javaHome = new File( System.getProperty( "java.home" ) )
if( javaHome.getName().equals( "jre" ) ) {
javaHome = javaHome.getParentFile()
}
if( javaHome.getName().startsWith( "jdk${sourceCompatibility}" ) ) {
return null
}
File installDir = javaHome.getParentFile();
for( File file : installDir.listFiles() ) {
if( file.getName().startsWith( "jdk${sourceCompatibility}" ) ) {
return new File( file, "bin/javac" ).getAbsolutePath()
}
}
println "Warning: javac executable for ${sourceCompatibility} was not found."
return null
}
task findJavac {
description '===
find and set the javac executable for the given sourceCompatibility ==='
compileJava.dependsOn(findJavac)
doLast {
String executable = findJavacExecutable()
if( executable != null ) {
println "\tjavac: ${executable}"
compileJava.options.fork = true
compileJava.options.forkOptions.executable = executable
}
}
}
‘sourceCompatibility’ is equivalent to javac’s ‘-source’ flag. As such, Gradle isn’t using the wrong JDK. However, there is work underway to easily specify the JDK to be used in the build script.