简体   繁体   English

如何在 Gradle 中设置语言级别? (因此它与 IDE 无关)

[英]How to set the language level in Gradle ? (so it is IDE-agnostic)

I would like to set the java language level in gradle, in an IDE-agnostic fashion.我想以与 IDE 无关的方式在 gradle 中设置 java 语言级别。

sourceCompatibility = 1.x at the root level seems deprecated in Gradle 2.21.根级别的sourceCompatibility = 1.x在 Gradle 2.21 中似乎已弃用。

(edit: or is it? IntelliJ gives me a groovy inspection error) (编辑:或者是吗?IntelliJ 给了我一个常规检查错误)

So I found this, which works.所以我找到了这个,它有效。

idea {
    project {
        languageLevel = '1.7'
    }
}

But doesn't the configuration here tie gradle to IntelliJ IDEA, because of the idea { } structure...但是这里的配置不是将 gradle 与 IntelliJ IDEA 联系起来,因为idea { }结构......

Is there a way to do this in an IDE-agnostic fashion?有没有办法以与 IDE 无关的方式做到这一点?

I would like my gradle build script to run in any IDE (be it IntelliJ IDEA or Eclipse) or on Jenkins (or whatever).我希望我的 gradle 构建脚本可以在任何IDE(无论是 IntelliJ IDEA 还是 Eclipse)或 Jenkins(或其他)上运行。

The way to do this for CLI builds is shown.显示了为 CLI 构建执行此操作的方法。 However I'm not sure if every IDE will pick this up.但是我不确定是否每个 IDE 都会选择这个。

allprojects {
    tasks.withType(JavaCompile) {
        sourceCompatibility = '1.7'
        targetCompatibility = '1.7'
    } 
}

If you use Gradle in command line, the language level works fines as specified in build.gradle file.如果您在命令行中使用 Gradle,则语言级别可以按照build.gradle文件中的指定正常工作。

But When import Gradle java project into IntelliJ IDEA, Gradle's plugin take responsibility to generate IDEA's project setting files,但是当将 Gradle java 项目导入 IntelliJ IDEA 时,Gradle 的插件负责生成 IDEA 的项目设置文件,

Unfortunately, the plug in does not respect build.gradle's sourceCompatibility/targetCompatibility property, instead, it use IDEA's setting: File -> Other Settings -> Default Project Structure -> Project Language Level -> 6 (By default).不幸的是,该插件不尊重 build.gradle 的 sourceCompatibility/targetCompatibility 属性,而是使用 IDEA 的设置: File -> Other Settings -> Default Project Structure -> Project Language Level -> 6 (By default). 在此处输入图片说明

So, i think this is a bug of gradle's idea plugin.所以,我认为这是gradle的idea插件的一个错误。 see https://issues.gradle.org/browse/GRADLE-2198https://issues.gradle.org/browse/GRADLE-2198

Currently, i have to change language level in above dialog sometimes.目前,我有时必须在上面的对话框中更改语言级别。

Adding this as an answer since I got it from the comments:添加此作为答案,因为我是从评论中得到的:

Instead of having sourceCompatibility/targetCompatibility in the compileJava task, use:不要在 compileJava 任务中使用 sourceCompatibility/targetCompatibility,而是使用:

project.sourceCompatibility = '1.7'
project.targetCompatibility = '1.7'

in the configuration for the relevant projects在相关项目的配置中

If they are defined under compileJava you won't get the warning and IntelliJ and gradle both respect it.如果它们是在compileJava下定义的,您将不会收到警告,并且 IntelliJ 和 gradle 都会尊重它。

compileJava {
    sourceCompatibility = '1.7'
    targetCompatibility = '1.7'
}

As with any gradle file change, you will need sync to get IntelliJ to pick up changes.与任何 gradle 文件更改一样,您需要同步才能让 IntelliJ 获取更改。

同步

inside of the android closure of your build.gradle inside your app module:在你的 app 模块中的 build.gradle 的android闭包内:

android {
    compileSdkVersion 27
    buildToolsVersion "26.0.2"
    // etc
    // other things

    // add this
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

I tested the following on Gradle 6.1 and it works:我在 Gradle 6.1 上测试了以下内容并且它有效:

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM