如何让 Maven 使用 Eclipse 工作区默认 JRE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12322202/
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
How to make Maven use Eclipse workspace default JRE?
提问by Jared
I'm using Spring Tool Suite and m2e to convert some of our existing projects to Maven projects. The project in question uses jdk1.6.0_20 which is named [jdk1.6] in Eclipse. When I do Maven -> Update project, though, it replaces that jre with the standard [JavaSE-1.6]. While they seem to point to the same libraries, the change in name causes a bunch of exceptions like:
我正在使用 Spring Tool Suite 和 m2e 将我们现有的一些项目转换为 Maven 项目。有问题的项目使用 jdk1.6.0_20,它在 Eclipse 中名为 [jdk1.6]。但是,当我执行 Maven -> Update 项目时,它将 jre 替换为标准 [JavaSE-1.6]。虽然它们似乎指向相同的库,但名称的更改会导致一系列异常,例如:
Access restriction: The type WindowsPopupMenuSeparatorUI is not accessible due to restriction on required library C:\Program Files\Java\jdk1.6.0_20\jre\lib\rt.jar
访问限制:由于所需库 C:\Program Files\Java\jdk1.6.0_20\jre\lib\rt.jar 的限制,无法访问类型 WindowsPopupMenuSeparatorUI
My pom.xml has this:
我的 pom.xml 有这个:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Is there any way to get Maven/m2e to use the default workspace JRE instead of replacing it with a specific one in the .classpath?
有没有办法让 Maven/m2e 使用默认工作区 JRE 而不是用 .classpath 中的特定工作区替换它?
采纳答案by Jared
It turns out there doesn't seem to be a way to do this with Maven. Instead I changed the error to a warning in Eclipse settings while I work with the owners of the offending code to fix the problems.
事实证明,Maven 似乎没有办法做到这一点。相反,我在 Eclipse 设置中将错误更改为警告,同时与违规代码的所有者一起解决问题。
回答by Chris Gerken
- Go into Configure Build Path on the project and go to the Libraries tab.
- Remove the JRE System Library
- Click on "Add Library..." and select "Workspace Default JRE"
- 进入项目上的配置构建路径并转到库选项卡。
- 删除 JRE 系统库
- 单击“添加库...”并选择“工作区默认 JRE”
That will give you the current JRE and not specify a specific JRE
这将为您提供当前的 JRE,而不是指定特定的 JRE
回答by Dariusz Szczepaniak
Adding maven-compiler-plugin to your pom forces maven to use given version of Java:
将 maven-compiler-plugin 添加到你的 pom 强制 maven 使用给定版本的 Java:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
However if you have both jre and jdk installed on your system(for a particular java version), maven may choose jre and then complain that it needs JDK. This may happen after using Maven > Update Projectin Eclipse.
但是,如果您的系统上同时安装了 jre 和 jdk(对于特定的 java 版本),maven 可能会选择 jre 然后抱怨它需要 JDK。在 Eclipse 中使用 Maven > Update Project后可能会发生这种情况。
To solve this, in Eclipse, right click on JRE System Library > Properties > Environments and select JavaSE-1.8. If you have more than one compatible JREs in your system then tick the jdk1.8.x > OK > select JavaSE-1.8 (jdk1.8.x) as Execution environment > OK
为了解决这个问题,在 Eclipse 中,右键单击 JRE System Library > Properties > Environments 并选择 JavaSE-1.8。如果您的系统中有多个兼容的 JRE,请勾选 jdk1.8.x > OK > 选择 JavaSE-1.8 (jdk1.8.x) 作为执行环境 > OK
Now maven should choose Java version 1.8 and Eclipse will tell maven to use jdk1.8 as the default JRE for this java version.
现在 maven 应该选择 Java 版本 1.8,Eclipse 会告诉 maven 使用 jdk1.8 作为这个 java 版本的默认 JRE。