Akom's Tech Ruminations

Various tech outbursts - code and solutions to practical problems

Gradle Toolchains Support - different JVMs for compile and test

Posted by Admin • Thursday, February 4. 2021 • Category: DevOps, Java

I'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 Trackbacks

  1. No Trackbacks


Display comments as (Linear | Threaded)
  1. No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Standard emoticons like :-) and ;-) are converted to images.
Markdown format allowed

Submitted comments will be subject to moderation before being displayed.