我想用默认的系统JRE运行Gradle,在我的例子中是Java8。
在构建过程中,我希望通过option.fork=true使用特定的JDK13
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设置为JDK13时,它就会成功运行。
有没有办法让Gradle从JRE8开始运行,但使用JDK13进行构建?
弗兰克
发布于 2019-10-08 00:09:21
Gradle会检查targetCompatibility是否高于当前运行的Java版本。老实说,我不知道为什么当你把编译器分支到一个较新的版本时,它会检查这个(如果有的话,它可能应该检查那个版本)。但也许这有一个很好的理由。
但是,在使用--release标志时,这两个属性也是完全多余的。这已经告诉编译器它应该为该特定版本生成字节码。我甚至不认为编译器支持同时使用source和target参数以及--release。因此,如果您删除sourceCompatibility和targetCompatibility这两个属性,它应该可以工作。
此外,Java编译器默认针对相同版本进行编译,因此也不需要--release标志。
最后,您只需配置'main‘源码集编译,您也应该对'test’执行同样的操作。虽然这可能并不重要,但您可能还希望将JavaDoc和任何类型为JavaExec的任务配置为使用Java13,否则它们将默认使用Java8。
因此,当针对比Gradle使用的Java版本更高的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 )。
https://stackoverflow.com/questions/58247800
复制相似问题