Or to show some concrete alternatives:
This will result in Java 8 compatible bytecode and will restrict the API to Java 8 too
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
tasks.compileJava {
options.release.set(8)
}
This will set source and target compatibility to Java 8 but will still allow to use Java 11 API
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
tasks.compileJava {
sourceCompatibility = "${JavaVersion.VERSION_1_8}"
}
This will use the Java 8 toolchain for exactly that task:
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
tasks.compileJava {
javaCompiler.set(javaToolchains.compilerFor {
languageVersion.set(JavaLanguageVersion.of(8))
})
}
There are many different ways, depending on the actual needs.