Gradle Toolchains Support - different JVMs for compile and test
Posted by Admin • Thursday, February 4. 2021 • Category: DevOps, JavaI'm testing a product that needs to be compiled with JDK 8 but tested (sometimes) on JDK 11. This is now possible to do with maven surefire (although that took some effort). With gradle, I was doing it as follows, which is terrible, even if the path comes from configuration:
// The old way:
test {
executable = '/some/hardcoded/path/to/java'
}
Now that Gradle 6.7+ has built-in toolchains support, it's trivial to configure the compile toolchain:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}
But what about tests? What if I want to test with a different JVM? It's a little more verbose:
test {
JavaToolchainService javaToolchainService = project.getExtensions().getByType(JavaToolchainService.class)
def launcher = javaToolchainService.launcherFor{
languageVersion = JavaLanguageVersion.of(11)
}
javaLauncher = launcher
environment 'JAVA_HOME', launcher.get().metadata.installationPath //if your tests care about JAVA_HOME
}
0 Comments
Add Comment