使用Java 8编译Gradle Java项目,但使用Java 11运行测试。

huangapple go评论80阅读模式
英文:

Compiling a Gradle Java project with Java 8 but running tests with Java 11

问题

作为我项目转换过程的第一部分,我希望在保持使用JDK-8编译器的情况下,使用JDK-11运行时执行测试。

我的项目是Gradle项目(如果有影响则为6.+版本),使用了java插件或java-library插件。

我无法找到使用Gradle选项来实现这一目标的方法。

我尝试了在一个终端中使用JDK-8进行编译(gradlew build),然后切换到另一个终端使用JDK-11运行测试(gradlew test),但它重新编译了代码。

正确的做法是什么?

英文:

As the first part of a transition process of my projects, I would like to keep compile with JDK-8 compiler, but execute tests with JDK-11 runtime.

My projects are Gradle projects (6.+ if it matters), using the java plugin or java-library plugins.

I could not find a way to do it with Gradle options.

I tried to compile (gradlew build) in one terminal with JDK-8, and then switch to another terminal with JDK 11 and run the tests (gradlew test), but it re-compiled the code.

What is the correct way to do it?

答案1

得分: 2

你可以使用不同于运行Gradle的JDK的配置来处理所有与Java相关的任务(编译、测试、JavaExec、JavaDoc等)。用户指南中有一章节提供了一个示例,说明如何在使用Java 8运行Gradle的情况下,让所有任务使用Java 7。同样适用于Java 11。

对于你的项目,你可以继续使用Java 8运行Gradle,但是为了使用不同的版本运行测试,可以添加以下内容:

// Gradle <= 6.6 (Groovy DSL)
tasks.withType(Test) {
    executable = new File("/my/path/to/jdk11/bin/java")
}

用户指南中提供了一个更可配置的解决方案,但这是其中的要点。

Gradle即将发布的版本6.7中的一个很酷的功能是支持JVM工具链。目前情况下,你必须自己下载JDK 11分发版并配置其路径。新版本将允许你声明所需版本,并在缺失时让Gradle为你下载(从AdoptOpenJDK):

// Gradle >= 6.7 (Groovy DSL)
// 此刻尚未发布,在写作时语法可能会有所变化
test {
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

最后一个选项是使用Java 11运行Gradle,并使用release选项将编译器目标设置为Java 8:

// 使用Java 11运行Gradle
compileJava {
    options.release = 8
}
英文:

You can configure all Java-related tasks (compilations, tests, JavaExec, JavaDoc, etc) with a different JDK than what is used to run Gradle. There is a chapter in the user guide that gives an example of how to run Gradle with Java 8 but use Java 7 in all tasks. It works the same with Java 11.

For your project, you can continue running Gradle with Java 8 but add the following for running tests with a different version:

// Gradle &lt;= 6.6 (Groovy DSL)
tasks.withType(Test) {
    executable = new File(&quot;/my/path/to/jdk11/bin/java&quot;)
}

The user guide has a more configurable solution, but this is the gist of it.

A cool feature that is part of the upcoming version 6.7 of Gradle is support for JVM toolchains. As it is now, you have to download a JDK 11 distribution yourself and configure the path to it. Newer versions will allow you to declare the version and let Gradle download it for you (from AdoptOpenJDK) if missing:

// Gradle &gt;= 6.7 (Groovy DSL)
// Unreleased at the time of this writing and the syntax is therefore subject to change
test {
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

A final option is to run Gradle with Java 11 and make the compiler target Java 8 using the release option:

// Run Gradle with Java 11
compileJava {
    options.release = 8
}

huangapple
  • 本文由 发表于 2020年10月12日 22:15:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64319620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定