Question

I have a Maven aggregator project (i.e. a project with modules) which I want to build in Eclipse using "Run As -> Maven install". 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.

I have tried to set the source and target properties in the POM, both like this:

<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.

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.

Was it helpful?

Solution

Open the "Run Configurations" editor. There is a category "Maven Build". Create a new configuration in there. That will give you a JRE tab on the right side of the dialog. Select whatever JRE you want in there.

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. That allows you to add checks to the build that fail when the wrong JRE is used.

OTHER TIPS

So if the parent is using the packaging type of pom, is the Java version really an issue? 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. 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. There are of course things like the pom itself but that does not differ between the Java versions.

This warning can happing when you have a project that has the packaging type pom and is configured as Java project in Eclipse. This also applies for or any other packaging type that is not supported by the m2e plugin.

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. The aggregator project usually doesn't need to be a Java project and might just accidentally got converted into one on creation or import.

The best solution would be to just remove the Java nature from the project. This shouldn't affect the build. In the Eclipse project explorer view, the "Maven Dependencies" will be gone though. 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. The modules of the project will still be Java projects.

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. 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.

Another solution is manually edit the Java version in the project settings. It could be that for some reason other target versions are defined somewhere that can't be set using th 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top