I have a Gradle project which I want to execute with Java 11. The project applies the Groovy plugin because it contains Groovy sources. Now the requirement is that the Groovy compiler emits Java 8 compatible class files. First step is to set target compatibility:
tasks.withType(GroovyCompile) {
targetCompatibility = JavaVersion.VERSION_1_8
}
This works fine. However, the second step would be that the compiler emits an error when the Java library usage is not compatible with Java 8, e.g. if List.of is called. With the Java compiler this is easily possible by setting the --release property. I don’t see that the Groovy compiler offers this. Hence I tried to fork the GroovyCompile tasks with a different Java home path like so:
tasks.withType(GroovyCompile) {
options.fork = true
options.forkOptions.javaHome = file('path/to/jdk8')
}
This does not seem to have an effect, the GroovyCompile task is still forked on Java 11. The Gradle source code also does not look like the fork options are taken into consideration. Strangely, the Gradle documentation states that it should: The Groovy Plugin
Can anyone point me to a solution for my task?