gpt4 book ai didi

gradle - 使用 Java 8 运行 Gradle 并使用 fork 编译 Java 13

转载 作者:行者123 更新时间:2023-12-03 03:11:05 24 4
gpt4 key购买 nike

我想使用默认系统 JRE 运行 Gradle,在我的例子中是 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 版本。老实说,我不知道为什么当您将编译器 fork 到更新版本时它会检查这个(如果有的话,它可能应该检查那个版本)。但也许有一个很好的理由。

但是,当您使用 --release 标志时,这两个属性也是完全多余的。这已经告诉编译器它应该为该特定版本生成字节码。我什至认为编译器不支持同时具有 sourcetarget 参数以及 --release 。因此,如果您删除 sourceCompatibilitytargetCompatibility 这两个属性,它应该可以工作。

此外,Java 编译器默认为同一版本编译,因此您也不需要 --release 标志。

最后,您只配置“主要”源代码集编译,您也应该为“测试”做同样的事情。虽然这可能并不重要,但您可能还想配置 JavaDoc 和任何 JavaExec 类型的任务以使用 Java 13,否则它们将默认为 Java 8。

因此,为了将它们整合在一起,当目标是比用于 Gradle 的版本更高的 Java 版本时,请使用如下内容:

// 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,因此您不需要任何这些(除非您决定在 Gradle 获得对它的支持之前更新到 Java 14,因为).

关于gradle - 使用 Java 8 运行 Gradle 并使用 fork 编译 Java 13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com