我可以将语言级别更改为8.0(F4->模块->语言级别),但每次编辑pom.xml时,项目级别都会切换回“使用项目语言级别”,我不得不一遍又一遍地编辑这些设置。
有什么需要添加到pom.xml中来将默认语言级别设置为8.0吗?
根据Mark的评论,这是如何做到的:
<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>
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
- genonymousvikingsteve的答案的简短版本如下:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
我正在将一个项目从JDK 8升级到JDK 10+。我已经正确地指定了编译器属性,如下所示:
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
我认为这与Maven编译器插件和IntelliJ idea之间的概念冲突有关。显然,较新版本的编译器插件具有默认级别1.5(参见http://maven.apache.org/plugins/maven-compiler-plugin/)。因此,如果在项目中使用编译器插件,并且未在pom.xml中明确设置编译器级别,则每当重新处理POM时,级别将恢复为默认值。
因此存在一个被Intellij IDEA忽略的概念上的冲突。该IDE仍然允许设置项目和模块设置,但不提供警告或反馈,指出此设置由pom.xml控制。解决方案要么是明确允许覆盖POM编译器插件设置(也许不明智,因为那么您在命令行上使用maven时会发生什么),要么在POM生效时停用IDE中的控件。
目前的解决方案是在pom中设置所需的编译器级别,然后重新导入,而不是尝试在模块设置中进行设置。
有两种方法可以做到这一点,将其中之一添加到您的pom.xml文件中:
第一种方法- 添加属性
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
第二步 - 添加插件
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
如果有帮助,请告诉我。
在我的情况下,这些解决方案都没有帮助。我不需要在pom.xml
中指定任何Java版本。
我需要打开<project-name>.iml
文件并在其中更改JDK版本。
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<!-- ... ^ -->
<!-- ... | -->
更新:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<!-- ... ^ -->
<!-- ... | -->
<profiles>
<profile>
<id>foo</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>bar</id>
.......
</profile>
<profiles>
问题:期望的Java版本为11,但卡在版本8
我尝试了几乎所有的答案,但仍然被language_level 8困住了,在我的项目或Intellij IDE中找不到任何与Java版本8相关的痕迹。
然后我探索了父级的pom.xml,但没有成功,但在祖父级别的pom.xml中,我发现发布版本是8。
解决方案:在属性中添加以下3行使得差异。
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>