3

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']
        }
    }
}

1 Answer 1

4

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.