简体   繁体   English

如何在Eclipse中为Maven聚合器项目构建定义JRE?

[英]How to define the JRE for a Maven aggregator project build in Eclipse?

I have a Maven aggregator project (ie a project with modules) which I want to build in Eclipse using "Run As -> Maven install". 我有一个Maven聚合器项目(即带有模块的项目),我想使用“运行方式-> Maven安装”在Eclipse中构建它。 My problem is that the project requires JDK 1.6, while Eclipse uses JDK 1.7 (the default JRE) to run Maven in. This problem only occurs when building the aggregator ("parent") project, not when building the modules separately. 我的问题是该项目需要JDK 1.6,而Eclipse使用JDK 1.7(默认的JRE)在其中运行Maven。 仅在构建聚合器(“父”)项目时才会发生此问题,而在单独构建模块时则不会发生。

I have tried to set the source and target properties in the POM, both like this: 我试图在POM中设置源和目标属性,两者都这样:

<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>

and like this: 像这样:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

but neither worked. 但都没有奏效。

I guess the problem is the following: For "normal" Maven projects, Eclipse uses the build path JRE (which is determined by the POM settings) to decide which JRE to run Maven in. Now for an aggregator project, which has a packaging type of "pom", there is no Eclipse build path and therefore Eclipse fails to choose the "right" JRE. 我猜问题出在以下方面:对于“普通” Maven项目,Eclipse使用构建路径JRE(由POM设置确定)来决定在哪个JRE中运行Maven。现在,对于具有打包类型的聚合器项目。对于“ pom”而言,没有Eclipse构建路径,因此Eclipse无法选择“正确的” JRE。

Does anybody know how to solve this problem? 有人知道如何解决这个问题吗? Ideally, the solution would only involve changing the POM and not the project or global Eclipse settings. 理想情况下,该解决方案仅涉及更改POM,而不涉及项目或全局Eclipse设置。

Open the "Run Configurations" editor. 打开“运行配置”编辑器。 There is a category "Maven Build". 有一个类别“ Maven构建”。 Create a new configuration in there. 在此处创建新配置。 That will give you a JRE tab on the right side of the dialog. 这将在对话框的右侧提供一个JRE选项卡。 Select whatever JRE you want in there. 在其中选择您想要的任何JRE。

You can save this launch config in the project using the "Share file" option in the tab "Common". 您可以使用“公共”选项卡中的“共享文件”选项将该启动配置保存在项目中。

You may also want to check the Maven enforcer plugin , especially requireJavaVersion . 您可能还需要检查Maven实施程序插件 ,尤其是requireJavaVersion That allows you to add checks to the build that fail when the wrong JRE is used. 这样,您便可以将使用错误JRE时失败的检查添加到生成中。

So if the parent is using the packaging type of pom , is the Java version really an issue? 因此,如果父级使用pom的包装类型,那么Java版本真的有问题吗? We are using a (much) later Java version that we are compiling code for and I have even used both the JDK supplied with eclipse as well as the server runtime JDK . 我们正在使用一个(很多)更高版本的Java版本来为其编译代码,我什至都使用了eclipse附带的JDK以及服务器运行时JDK In both cases it compiles code with the correct version for our modules. 在这两种情况下,它都会使用适用于我们模块的正确版本来编译代码。

However for the parent pom using pom packaging this is done with the JDK we are using at the time, but since there are no java binaries (classes or jar files) generated there are no built artifacts in the parent project and as such no residual dependencies on the Java version. 但是对于使用pom打包的父pom,这是使用我们当时使用的JDK完成的,但是由于没有生成Java二进制文件(类或jar文件),因此父项目中没有构建的构件,因此也没有残留的依赖项在Java版本上。 There are of course things like the pom itself but that does not differ between the Java versions. 当然,有些东西像pom本身,但是在Java版本之间没有区别。

This warning can happing when you have a project that has the packaging type pom and is configured as Java project in Eclipse. 当您的项目的包装类型为pom且在Eclipse中配置为Java项目时,可能会出现此警告。 This also applies for or any other packaging type that is not supported by the m2e plugin. 这也适用于m2e插件不支持的任何其他包装类型。

In this case, the reason for this issue is that Eclipse validates the JDK version, but because of the packaging type m2e does not update the Eclipse settings when providing new source and target versions using the maven-compiler-plugin or compiler properties. 在这种情况下,此问题的原因是Eclipse验证了JDK版本,但是由于打包类型m2e在使用maven-compiler-plugin或编译器属性提供新的源版本和目标版本时不会更新Eclipse设置。 The aggregator project usually doesn't need to be a Java project and might just accidentally got converted into one on creation or import. 聚合器项目通常不需要是Java项目,并且可能只是在创建或导入时意外地转换为一个项目。

The best solution would be to just remove the Java nature from the project. 最好的解决方案是从项目中删除Java性质 This shouldn't affect the build. 这不应该影响构建。 In the Eclipse project explorer view, the "Maven Dependencies" will be gone though. 但是,在Eclipse项目浏览器视图中,“ Maven依赖关系”将消失。 It's possible that it could breaks some integration with some Eclipse plugins or features when the aggregator project is not a Java project any more. 当聚合器项目不再是Java项目时,它可能会破坏与某些Eclipse插件或功能的某些集成。 The modules of the project will still be Java projects. 该项目的模块仍将是Java项目。

To convert it to a non-Java project, you need to remove the Java project nature and Java builder from the .project file in the aggregator projects root directory. 要将其转换为非Java项目,您需要从聚合器项目根目录中的.project文件中删除Java项目性质和Java构建器。 You need to remove the line 您需要删除该行

<nature>org.eclipse.jdt.core.javanature</nature>

and the lines 和线

<buildCommand>
    <name>org.eclipse.jdt.core.javabuilder</name>
    <arguments>
    </arguments>
</buildCommand>

Safe the file and select the project in eclipse and hit F5 to update it. 保存文件,然后在eclipse中选择项目,然后按F5进行更新。

Another solution is manually edit the Java version in the project settings. 另一种解决方案是在项目设置中手动编辑Java版本。 It could be that for some reason other target versions are defined somewhere that can't be set using th UI. 可能由于某种原因,其他目标版本在某个地方无法使用UI进行设置。 For example I've found the entry org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.4 in the .settings/org.eclipse.jdt.core.prefs file which seems to have caused the warning even though every other setting was correct. 例如,我在.settings/org.eclipse.jdt.core.prefs文件中找到了org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.4条目,即使所有其他设置似乎也引起了警告是正确的。 If the warning still doesn't go away you might need to search in the setting files for similar entries. 如果警告仍然存在,则可能需要在设置文件中搜索类似的条目。

A last option would be to add pom packinging type projects to the m2e lifecyclemapping, so that it will be converted to a Java project if it isn't already and updated when the target and source versions change. 最后一个选择是将pom打包类型项目添加到m2e生命周期映射中,以便将其转换为Java项目(如果尚未转换),并在目标版本和源版本更改时进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM