问题
使用IntelliJ 12,我有一个java项目,我使用maven和pom.xml。我的项目使用的是java8,但在导入项目时,似乎默认的项目语言级别已设置为6。
我可以将语言级别更改为8.0(F4 - >模块 - >语言级别)但是每次编辑我的pom.xml时,项目级别都会切换回"使用项目语言级别",我必须再次编辑此设置,再次。
是否需要添加到pom.xml以将默认语言级别设置为8.0?
#1 热门回答(123 赞)
根据马克的评论,以下是如何做到这一点:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
#2 热门回答(64 赞)
更短版本的vikingsteve的答案是:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
#3 热门回答(20 赞)
我认为这与Maven编译器插件和IntelliJ想法之间的概念冲突有关。显然,较新版本的编译器插件的默认级别为1.5(参见http://maven.apache.org/plugins/maven-compiler-plugin/)。因此,如果在项目中根本使用了编译器插件,并且未在pom.xml中显式设置编译器级别,则每当重新处理POM时,级别将恢复为默认值。
因此,Intellij IDEA会忽略概念冲突。 IDE仍允许设置项目和模块设置,但不提供此设置由pom.xml控制的警告或反馈。解决方案可能是明确允许覆盖POM编译器插件设置(可能不明智,因为当你在命令行上使用maven时会发生这种情况),或者当POM中的此设置生效时停用IDE中的控件。
目前的解决方案是在pom中的编译器插件中设置所需的编译器级别,重新导入,而不是尝试在模块设置中设置它。