简体   繁体   English

在 Gradle 和 IntelliJ 中设置语言级别

[英]Set language level in Gradle and IntelliJ

I've set the language level to 8 in the IntelliJ project settings, and I've also tried setting it in gradle like this:我在 IntelliJ 项目设置中将语言级别设置为 8,并且我也尝试在 gradle 中设置它,如下所示:

   buildscript {
    ext.kotlinVersion = '1.3.41'

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

    }

}

allprojects {

    version = '1.0'
    ext {
        appName = "VolumeFlux"
        gdxVersion = '1.9.10'
        roboVMVersion = '2.3.8'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
        visuiVersion = '1.4.2'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        google()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }


    apply plugin: 'java'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

}

sourceCompatibility = 1.8
targetCompatibility = 1.8

subprojects {
    apply plugin: 'java'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

project(":desktop") {
    apply plugin: "kotlin"

    dependencies {
        implementation project(":core")
        api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
        api "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
        api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}

project(":core") {
    apply plugin: "kotlin"

    dependencies {
        api "com.badlogicgames.gdx:gdx:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        api "com.kotcrab.vis:vis-ui:1.3.0"
        api "com.github.czyzby:gdx-kiwi:1.9.1.9.6"
        api "net.dermetfan.libgdx-utils:libgdx-utils:0.13.4"
        api "com.badlogicgames.ashley:ashley:$ashleyVersion"
        api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

        compile "com.kotcrab.vis:vis-ui:$visuiVersion"

    }
}

But for some reason I still get a gradle build error:但由于某种原因,我仍然收到一个 gradle 构建错误:

...\VolumeFlux.java:79: error: lambda expressions are not supported in -source 7
                stockButtons.forEach(b -> {
                                       ^
  (use -source 8 or higher to enable lambda expressions)

I recently overcame the incorrect source / target issue above with this gradle setup.我最近通过这个 gradle 设置克服了上面不正确的源/目标问题。 Looking at the posted build file I cant see why this is happening everything seems correct.查看发布的构建文件,我不明白为什么会发生这种情况,一切似乎都是正确的。

I am posting an outline of my setup that resolved the incorrect source / target compatiblitiy我发布了解决不正确的源/目标兼容性的设置大纲

top level build.gradle顶级 build.gradle

plugins {
id 'base'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'  apply false
......
// note apply false
}

allprojects {
group = ".."
version = ".."
repositories {
    mavenCentral()
    jcenter()
    ...
}
ext {
    ...
  }
}

subprojects {
  // only apply plugins common to all subprojects here
  apply  plugin: "org.beryx.runtime"
  apply  plugin: "com.google.protobuf"
  sourceCompatibility = 11
  targetCompatibility = 11
  // only declare dependencies common to all subprojects here
  dependencies {
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.4.2'
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
   }
 }

each subproject will have a build.gradle where you apply plugins that are needed and declared in the top level build file每个子项目都有一个 build.gradle,您可以在其中应用在顶层构建文件中需要和声明的插件

plugins {
// applied from top level build
id 'java'
id 'application'
id 'org.jetbrains.kotlin.jvm'
.....
}  

dependencies {
// dependencies here that are unique to the subproject
}

with respect to the issue originally I declared the source / target compatibility in the subproject build file and I ran in to the compatibility issue.关于这个问题,我最初在子项目构建文件中声明了源/目标兼容性,但遇到了兼容性问题。 It was resolved when I moved it to the subprojects closure block in the top level file.当我将它移动到顶级文件中的子项目关闭块时,它得到了解决。

Note @BAR's use of project(":core") {} is correct see example 2 here .注意@BAR 对project(":core") {}是正确的, 请参见此处的示例 2 When you use this form then you do not need a build.gradle in the subproject but possibly this leads to intellij / gradle snafu当您使用此表单时,您不需要子项目中的 build.gradle 但这可能会导致 intellij / gradle snafu

我必须在子项目的 build.gradle 设置中而不是在根 build.gradle 中设置sourceCompatibility = 1.8

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

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