移动到 OpenJDK-11 但在 Java 8 中编译

Move to OpenJDK-11 but compile in Java 8

我有一个现有的 Spring boot 应用程序,是我通过 gradle 构建的。这些天我一直在使用 JDK / JRE 8 现在我正在尝试使用 JDK-11

所以为了检查兼容性,我将 JAVA_HOME 设置为 JDK-11 但尝试在 Java 8 模式下编译

我在 build.gradle

中添加了以下块
compileJava {
    targetCompatibility = '1.8'
}

然后我明确地设置 JAVA_HOME set JAVA_HOME=C:\Users\arun\Desktop\jdk-11.0.1

然后执行gradlew clean build

但我因以下异常而停止

> Configure project :
Build Version = build-182-ga03cf6c

> Task :compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> warning: source release 11 requires target release 11

如何将我的 JAVA_HOME 指向 JDK-11 但仍然以 Java-8 模式执行它?

您还需要设置 sourceCompatibility。

在此处查看 post Gradle, "sourceCompatibility" vs "targetCompatibility"?

我会说:

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

因为sourceCompatibility的默认值是当前使用的JVM的版本。

来源:https://docs.gradle.org/current/userguide/java_plugin.html

从 Java 9 开始,您可以将 --release N 选项用于 cross-compile 和 Gradle。设置 sourceCompatibility 和 targetCompatibility 是不够的,因为在这种情况下,您还需要将 bootClasspath 设置为 JDK N。有关详细信息,请参阅

相反,使用 Java 9+ “--release” compilerArg,如下所示:

compilerArgs.addAll(['--release', '8'])

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html