运行 Gradle 使用 Java8 并使用 fork 编译 Java13
Running Gradle with Java8 and compile Java13 with fork
我想 运行 Gradle 使用默认系统 JRE,在我的例子中 Java8。
并且在构建中我想使用特定的 JDK 13 和 option.fork=true
compileJava {
options.encoding = 'UTF-8'
sourceCompatibility = 13
targetCompatibility = 13
options.compilerArgs << "-parameters"
options.compilerArgs << "--release" << "13"
options.compilerArgs << "--enable-preview"
options.fork = true
options.forkOptions.executable = "${projectDir}/tools/java/bin/javac.exe"
}
当我使用 JRE 8 启动 gradle 时,失败并显示消息:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Could not target platform: 'Java SE 13' using tool chain: 'JDK 8 (1.8)'.
但是当我将 JAVA_HOME 设置为 JDK 13 时,它 运行 成功了。
有没有一种方法可以实现 运行,Gradle 从 JRE8 开始,但使用 JDK13 构建?
弗兰克
Gradle 检查 targetCompatibility
不高于当前 运行 版本的 Java。老实说,我不知道为什么当您将编译器分叉到更新版本时它会检查这个(如果有的话,它可能应该检查那个版本)。但也许有一个很好的理由。
但是,当您使用 --release
标志时,这两个属性也是完全多余的。这已经告诉编译器它应该为该特定版本生成字节码。我什至认为编译器不支持同时具有 source
和 target
参数以及 --release
。因此,如果您删除 sourceCompatibility
和 targetCompatibility
这两个属性,它应该可以工作。
此外,Java 编译器默认编译相同版本,因此您也不需要 --release 标志。
最后,您只配置了 'main' 源集编译,您也应该为 'test' 做同样的事情。虽然它可能并不重要,但您可能还需要配置 JavaDoc
和任何类型 JavaExec
的任务以使用 Java 13,否则它们将默认为 Java 8。
因此,为了将它们整合在一起,当目标 Java 的版本高于 Gradle 的版本时,请使用如下内容:
// Paths for the various executables in the Java 'bin' directory
def javaHome = "${projectDir}/tools/java"
def javaCompilerPath = "$javaHome/bin/javac.exe"
def javaExecutablePath = "$javaHome/bin/java.exe"
def javaDocPath = "$javaHome/bin/javadoc.exe"
tasks.withType(AbstractCompile) { // Configures all compile tasks (both for main and test)
// The sourceCompatibility and targetCompatibility properties have been removed
options.encoding = 'UTF-8'
options.compilerArgs.addAll(["-parameters", "--enable-preview"]) // The --release flag is optional when used for the same version
options.fork = true
options.forkOptions.executable = javaCompilerPath
options.forkOptions.javaHome = file(javaHome)
}
tasks.withType(Javadoc) { // Configures JavaDoc to use the tool in Java 13
executable = javaDocPath
}
tasks.withType(JavaExec) { // Only needed if you have any tasks of this type
executable = javaExecutablePath
}
当 Gradle 6.0 发布时,大约一周后,它将支持 Java 13,因此您不需要任何这些(直到您决定更新到 Java 14 在 Gradle 得到支持之前,这是有原因的)。
我想 运行 Gradle 使用默认系统 JRE,在我的例子中 Java8。
并且在构建中我想使用特定的 JDK 13 和 option.fork=true
compileJava {
options.encoding = 'UTF-8'
sourceCompatibility = 13
targetCompatibility = 13
options.compilerArgs << "-parameters"
options.compilerArgs << "--release" << "13"
options.compilerArgs << "--enable-preview"
options.fork = true
options.forkOptions.executable = "${projectDir}/tools/java/bin/javac.exe"
}
当我使用 JRE 8 启动 gradle 时,失败并显示消息:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Could not target platform: 'Java SE 13' using tool chain: 'JDK 8 (1.8)'.
但是当我将 JAVA_HOME 设置为 JDK 13 时,它 运行 成功了。
有没有一种方法可以实现 运行,Gradle 从 JRE8 开始,但使用 JDK13 构建?
弗兰克
Gradle 检查 targetCompatibility
不高于当前 运行 版本的 Java。老实说,我不知道为什么当您将编译器分叉到更新版本时它会检查这个(如果有的话,它可能应该检查那个版本)。但也许有一个很好的理由。
但是,当您使用 --release
标志时,这两个属性也是完全多余的。这已经告诉编译器它应该为该特定版本生成字节码。我什至认为编译器不支持同时具有 source
和 target
参数以及 --release
。因此,如果您删除 sourceCompatibility
和 targetCompatibility
这两个属性,它应该可以工作。
此外,Java 编译器默认编译相同版本,因此您也不需要 --release 标志。
最后,您只配置了 'main' 源集编译,您也应该为 'test' 做同样的事情。虽然它可能并不重要,但您可能还需要配置 JavaDoc
和任何类型 JavaExec
的任务以使用 Java 13,否则它们将默认为 Java 8。
因此,为了将它们整合在一起,当目标 Java 的版本高于 Gradle 的版本时,请使用如下内容:
// Paths for the various executables in the Java 'bin' directory
def javaHome = "${projectDir}/tools/java"
def javaCompilerPath = "$javaHome/bin/javac.exe"
def javaExecutablePath = "$javaHome/bin/java.exe"
def javaDocPath = "$javaHome/bin/javadoc.exe"
tasks.withType(AbstractCompile) { // Configures all compile tasks (both for main and test)
// The sourceCompatibility and targetCompatibility properties have been removed
options.encoding = 'UTF-8'
options.compilerArgs.addAll(["-parameters", "--enable-preview"]) // The --release flag is optional when used for the same version
options.fork = true
options.forkOptions.executable = javaCompilerPath
options.forkOptions.javaHome = file(javaHome)
}
tasks.withType(Javadoc) { // Configures JavaDoc to use the tool in Java 13
executable = javaDocPath
}
tasks.withType(JavaExec) { // Only needed if you have any tasks of this type
executable = javaExecutablePath
}
当 Gradle 6.0 发布时,大约一周后,它将支持 Java 13,因此您不需要任何这些(直到您决定更新到 Java 14 在 Gradle 得到支持之前,这是有原因的)。