Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set a different java version for a gradle javaexec task?

I have a java 11 project and I am trying to add a javaexec task to run a standalone cukedoctor documentation generation task, but it needs java 8 otherwise it errors unsupported Java version "11", defaulting to 1.7.

Is there a way I can explicitly set the java version on a javaexec task that's different from the main project?

build.gradle


version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

task cukeDoctor() {
    group = "Documentation"
    description = "Publish cucumber documentation and test results"
    dependsOn test
    doLast {
        javaexec {
            main = "-jar"
            args = ['tools/cukedoctor-main-1.2.1.jar', '-p', 'docs/cucumber.json']
        }
    }
}
like image 774
MonkeyBonkey Avatar asked Dec 18 '25 07:12

MonkeyBonkey


1 Answers

Sorry for the late answer. With gradle versions 6.7 onwards, you can specify which version of java to use and ignore the version used by the machine (JAVA_HOME) with java toolchains.

java {
   toolchain {
      languageVersion.set(JavaLanguageVersion.of(8))
   }
}

You also have to tell gradle that you want it to use that java in all "JavaExec" tasks, to do this add these lines:

tasks.withType(JavaExec).configureEach {
   javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}

I don't really know how it works in depth, but I put the link to the sites that helped me to solve this problem.

https://www.cloudhadoop.com/gradle-configure-java-version/ https://github.com/gradle/gradle/issues/16791

like image 184
Moises Suarez Avatar answered Dec 20 '25 21:12

Moises Suarez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!