Gradle Multi Project using both Java8 and Java11

Is it possible to have a gradle multi module project that uses both java 8 and java 11? Is the proper approach to upgrade gradle to latest, build under java 11 JDK and then per project use the sourceCompatibility and targetCompatibility set to 8 for some subprojects?

Yes, that will work. But you will still be compiling against the Java 11 SDK, and you could accidentally reference something that is not present in Java 8. In that case, everything will compile file, but you will get a runtime error instead. This may not be a problem, but if it is, there are a few ways around it.

You can either compile with Java 8 like this:

compileJava {
    options.fork = true
    options.forkOptions.executable = "/path/to/jdk8/bin/javac"
    options.bootstrapClasspath = files("/path/to/jdk8/jre/lib/rt.jar")
}

Or you can use the --release flag in Java 11:

compileJava {
  options.compilerArgs = ['--release', '8']
}

If you want to run your test under Java 8 as well, you will need something like this:

test {
    executable = "/path/to/jdk8/bin/java"
}
3 Likes

Thanks. Luckily the team opted for breaking up the project upgrade work by tool rather than subproject. Gradle 5 first, spring next then java.