I have the following gradle task for cucumber (which is more or less the same as mentioned on the cucumber website).
task cucumber() {
dependsOn assemble, testClasses
def jacocoAgent = zipTree(configurations.jacocoAgent.singleFile).filter { it.name == "jacocoagent.jar" }.singleFile
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
jvmArgs = ["-javaagent:$jacocoAgent=destfile=$buildDir/jacoco/cucumber.exec,append=false"]
args = ['--plugin', 'pretty', 'src/test/resources/features', '--glue', 'foo.bar.baz.cucumber.glue', '--glue', 'foo.bar.baz.cucumber.config']
}
}
}
Now we started (or starting) to use the toolchains to support java17 and run Gradle with java 11 and not to have to install java 17 ourselves. Compilation and regular tests run fine, but the cucumber stuff is using the JDK used to run gradle (which is 11, and thus fails with an unsupported class version).
I tried the solution as given on Shouldn't JavaExec default to using the project's toolchain if any? · Issue #16791 · gradle/gradle · GitHub
tasks.withType<JavaExec>().configureEach {
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}
But that doesn’t appear to work or being called at all.
According to the docs we should be able to set the javaLauncher
property on the JavaExec
directly but when doing so the build fails with an unknown property.
task cucumber() {
dependsOn assemble, testClasses
def jacocoAgent = zipTree(configurations.jacocoAgent.singleFile).filter { it.name == "jacocoagent.jar" }.singleFile
doLast {
javaexec {
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
jvmArgs = ["-javaagent:$jacocoAgent=destfile=$buildDir/jacoco/cucumber.exec,append=false"]
args = ['--plugin', 'pretty', 'src/test/resources/features', '--glue', 'foo.bar.baz.cucumber.glue', '--glue', 'foo.bar.baz.cucumber.config']
}
}
}
This fails with
* What went wrong:
Execution failed for task ':cucumber'.
> Could not set unknown property 'javaLauncher' for object of type org.gradle.process.internal.DefaultJavaExecAction_Decorated.
So now I’m at a bit of a loss how to fix this for this task? Any pointers or obvious mistakes?