将sourceCompatibility和targetCompatibility设置为
JavaVersion.VERSION_11在我的IntelliJ / Grade / Java项目中没有预期的效果.
即使我手动将项目结构中的Project SDK和项目语言级别设置为11,它在I Refresh Gradle项目(或导入更改)之后还原为8和6.见下图:
确保我创建了一个新项目并在其中测试这些项目.这是它的树视图:
唯一的build.gradle文件:
plugins {
id 'java'
}
group 'test'
version '1.0-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Gradle版本是4.10.2,您可以在gradle-wrapper.properties文件中看到它:
#Tue Nov 13 00:53:45 IRST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
这是环境信息:
IntelliJ IDEA 2018.1.5 (Community Edition)
Build #IC-181.5281.24, built on June 12, 2018
JRE: 1.8.0_152-release-1136-b39 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
这是我的SDK中的图片:
另外,根据this answer,我在build.gradle下面的配置,但问题仍然存在:
group 'test'
version '1.0-SNAPSHOT'
allprojects {
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
repositories {
...
并且根据another answer在同一帖子下面的配置:
group 'test'
version '1.0-SNAPSHOT'
allprojects {
apply plugin: 'java'
tasks.withType(JavaCompile) {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
repositories {
...
最佳答案 对于此类问题,工具兼容性很重要,您可以将IDE版本升级到版本> = 2018.2.
引用JetBrains博客#Java11 in IntelliJ IDEA 2018.2
The next version of Java is due out in September, and IntelliJ IDEA 2018.2 is ready for it.