如何使用 gradle 强制执行 java 编译器版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28065488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:57:56  来源:igfitidea点击:

How to enforce a java compiler version with gradle?

javagradlejavac

提问by fge

In all of my projects, I use gradle and specify the following:

在我的所有项目中,我使用 gradle 并指定以下内容:

sourceCompatibility = "1.7"; // for example
targetCompatibility = "1.7"; // defaults to sourceCompatibility

Now, I have three different versions of the JDK installed, from 1.6 to 1.8. In order to switch from one version to another, I sourceshell files to change PATH, JAVA_HOMEand even JDK_HOME.

现在,我安装了三个不同版本的 JDK,从 1.6 到 1.8。为了从一个版本切换到另一个,我sourceShell文件来改变PATHJAVA_HOME甚至JDK_HOME

By accident it can happen that I use the wrong JDK version and I don't want that... Is there a possibility to check that the compiler version is equal to targetCompatibility before attempting any compilation task?

偶然地,可能会发生我使用错误的 JDK 版本而我不想要的情况……在尝试任何编译任务之前,是否有可能检查编译器版本是否等于 targetCompatibility?

采纳答案by JB Nizet

I use the following:

我使用以下内容:

task checkJavaVersion << {
    if (!JavaVersion.current().isJava6()) {
        String message = "ERROR: Java 1.6 required but " +
                          JavaVersion.current() +
                          " found. Change your JAVA_HOME environment variable.";
        throw new IllegalStateException(message);
    }
}

compileJava.dependsOn checkJavaVersion

回答by fge

Answer to self, and thanks to @JBNizetfor providing the initial solution...

自我回答,感谢@JBNizet提供初始解决方案...

The solution is indeed to use JavaVersion, and it happens that both sourceCompatibilityand targetCompatibilityaccept a JavaVersionas an argument...

该解决方案确实是在使用JavaVersion,并且它发生,无论sourceCompatibilitytargetCompatibility接受JavaVersion作为参数...

Therefore the build file has become this:

因此构建文件变成了这样:

def javaVersion = JavaVersion.VERSION_1_7;
sourceCompatibility = javaVersion;
targetCompatibility = javaVersion; // defaults to sourceCompatibility

And then the task:

然后是任务:

task enforceVersion << {
    def foundVersion = JavaVersion.current();
    if (foundVersion != javaVersion) 
        throw new IllegalStateException("Wrong Java version; required is "
            + javaVersion + ", but found " + foundVersion);
}

compileJava.dependsOn(enforceVersion);

And it works:

它有效:

$ ./gradlew clean compileJava
:clean UP-TO-DATE
:enforceVersion FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/home/fge/src/perso/grappa-tracer-backport/build.gradle' line: 55

* What went wrong:
Execution failed for task ':enforceVersion'.
> Wrong Java version; required is 1.7, but found 1.8

回答by codehearts

If you want the version to be checked for all tasks, you can add an assertion in build.gradleto enforce it:

如果您希望检查所有任务的版本,您可以添加一个断言build.gradle来强制执行它:

assert JavaVersion.current().isJava9Compatible(): "Java 9 or newer is required"

With Gradle 5.4.1, the failure looks like this:

使用 Gradle 5.4.1,失败看起来像这样:

$ ./gradlew --quiet build
FAILURE: Build failed with an exception.

* Where:
Build file '/home/codehearts/build.gradle' line: 15

* What went wrong:
A problem occurred evaluating root project 'Codehearts'.
> Java 9 or newer is required. Expression: org.gradle.api.JavaVersion.current().isJava9Compatible()

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s

You can also check exact versions if needed:

如果需要,您还可以检查确切的版本:

assert JavaVersion.current().isJava9(): "Java 9 is required"