用Java8运行Gradle,用fork编译Java13

用Java8运行Gradle,用fork编译Java13,gradle,java-13,Gradle,Java 13,我想用默认的系统JRE运行Gradle,在我的例子中是Java8 在构建中,我想使用一个带有选项.fork=true的特定JDK13 compileJava { options.encoding = 'UTF-8' sourceCompatibility = 13 targetCompatibility = 13 options.compilerArgs << "-parameters" options.compilerArgs <<

我想用默认的系统JRE运行Gradle,在我的例子中是Java8

在构建中,我想使用一个带有选项.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"
}
但是当我将JAVA_HOME设置为JDK13时,它就成功运行了

有没有办法让Gradle从JRE8开始,但使用JDK13进行构建


Frank

Gradle检查了
targetCompatibility
是否高于当前运行的Java版本。老实说,我不知道为什么当你将编译器转换到一个新版本时,它会检查这个问题(如果有的话,它可能会检查这个版本)。但也许有一个很好的理由

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

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

最后,您只需要配置“main”源代码集编译,您也应该对“test”执行同样的操作。虽然这可能并不重要,但您可能还希望配置
JavaDoc
和任何类型的
JavaExec
任务来使用java13,否则它们将默认为java8

因此,要将这一切结合起来,当针对比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:
// 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
}