在 Eclipse 中为 Maven 项目设置默认的 Java 合规性级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16798066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set default java compliance level for Maven projects in Eclipse
提问by LAC
The default compliance level for Maven is 1.5 and every time that I update a maven project in Eclipse it sets back the compliance level from 1.6 to 1.5 which is really annoying for me.
Maven 的默认合规性级别是 1.5,每次我在 Eclipse 中更新 Maven 项目时,它都会将合规性级别从 1.6 设置回 1.5,这对我来说真的很烦人。
I know that I can set the target to 1.6 in the POM file but the problem is that I cannot set this in the parent POM and expect the children to inherit it. So I have to do it for every single Maven module. How can I set it in my Maven project or in the whole eclipse once for a "lifetime" without modifying every single Maven module!?
我知道我可以在 POM 文件中将目标设置为 1.6,但问题是我不能在父 POM 中设置它并期望孩子继承它。所以我必须为每一个 Maven 模块做这件事。如何在不修改每个 Maven 模块的情况下,在我的 Maven 项目或整个 Eclipse 中一次“终身”设置它!?
采纳答案by Martin Ellis
I know that I can set the target to 1.6 in pom file but the problem is that I cannot set this in the parent pom and expect the children to inherit it.
我知道我可以在 pom 文件中将目标设置为 1.6,但问题是我不能在父 pom 中设置它并期望孩子继承它。
Setting the <source>
and <target>
version in the parent pom does work.
在父 pom 中设置<source>
和<target>
版本确实有效。
For example, in my parent pom, I have:
例如,在我的父 pom 中,我有:
<pluginManagement>
<plugins>
? ? <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
If you are having problems, you might want to check that:
如果您遇到问题,您可能需要检查:
- the child specifies the correct version of the parent;
- the parent pom specifies both
source
andtarget
values; - you have run
mvn install
on the parent; and mvn help:effective-pom
on the child project shows the expected source/target values.
- 子项指定了父项的正确版本;
- 父 pom 指定了
source
和target
值; - 你已经
mvn install
在父母身上跑了;和 mvn help:effective-pom
在子项目上显示预期的源/目标值。
After modifying the poms, you may need to select both projects and use Maven->Update Project.
修改poms后,您可能需要选择两个项目并使用Maven->Update Project。
回答by LAXIT KUMAR
This worked for me..
这对我有用..
<build>
<finalName>ProjectName</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
回答by Svilen
Another way (if you do not have already compiler plugin defined) is to set just these properties in your pomfile:
另一种方法(如果你还没有定义编译器插件)是在你的pom文件中设置这些属性:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Details: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
详情:https: //maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
回答by user4425403
Go to the project's pom.xml and insert below in between tags. Then the eclipse plugin (like m2eclipse) should rebuild the workspace.
转到项目的 pom.xml 并在下面的标签之间插入。然后 eclipse 插件(如 m2eclipse)应该重建工作区。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Note: this is project-level as only the project's pom.xml is affected.
注意:这是项目级别的,因为只有项目的 pom.xml 受到影响。