I am working in multi project gradle build, and was tasked with modfying the project such that it supports both jdk 1.8 and jdk 17 at compile time and runtime, and binaries compiled in jdk 1.8 should be executable with jdk 17 runtime.
For this I introduced JVM toolchain (ref) in allprojects block in the root project, as shown in the below code snippet
allProjects {
java {
println "Present in the java task which is running in the project = " + project.name + " with jdk version for compile = " + compileJavaVersion
toolchain {
// Set the language version, defaulting to JDK8.
languageVersion = JavaLanguageVersion.of(compileJavaVersion)
vendor = JvmVendorSpec.XYZ_ABC
}
}
}
and there was a unit test to check if the correct version of jvm is being used to execute the test or not
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@Category(UnitTest.class)
public class JavaVersionTest {
@Test
public void JavaRuntimeVersionTest(){
//Currently it should check if jdk17 is being used or not
Assert.assertEquals("JDK version being used","17.0.6", System.getProperty("java.version"));
}
}
With the above setup the testcase was failing, implying that the wrong jdk version is being used.
When I moved the java extension block to a sub project where this test case was written and executed the sub project tests alone this test was successful.
I am unable to figure out why the above java extension is working in sub project but not the main project.
I tried applying the java plugin in the allProjects block, as shown in the snippet below
allProjects {
apply plugin: 'java' //change that was made for compilation in different verison to work
java {
println "Present in the java task which is running in the project = " + project.name + " with jdk version for compile = " + compileJavaVersion
toolchain {
// Set the language version, defaulting to JDK8.
languageVersion = JavaLanguageVersion.of(compileJavaVersion)
vendor = JvmVendorSpec.XYZ_ABC
}
}
}
The correct version of java was being used, after making this change.
My question
Can anyone give inputs on why the compilation was not using the correct jdk version before but is using the correct jdk version after making the small change?
Additional information if required, and my thoughts
There are some sub projects where there are no java tasks and they are present for the purpose of aggregating mutliple files, is it possible that the java extension block has failed for these projects and hence the extension setting were failed to apply for all the other sub projects as well?
Any suggestions and help regarding this is deeply appreciated.
Thanks