ChatGPT解决这个技术问题 Extra ChatGPT

What causes imported Maven project in Eclipse to use Java 1.5 instead of Java 1.6 by default and how can I ensure it doesn't?

I imported a Maven project and it used Java 1.5 even though I have 1.6 configured as my Eclipse default Preferences->Java->Installed JREs.

When I changed the Maven project to use the 1.6 JRE it still had the build errors left over from when the project was using Java 1.5 (I described these build errors earlier in: I have build errors with m2eclipse but not with maven2 on the command line - is my m2eclipse misconfigured?)

I'm going to delete the project and try again but I want to make sure this time that it uses Java 1.6 from the start to see if this eliminates the build problems.

How do I make sure the project uses Java 1.6 when I import it?

m2eclipse ignores what you configure for the Eclipse default. It will always use 1.5 by default. However in your case the project uses the maven-compiler-plugin and sets the version explicitly to 1.5. You will need to change that in the pom and update the project configuration to change the version. However I already tried this and it didn't work for me. I'm pretty sure that's not the problem despite what others said in your other question.
No, m2eclipse will not always default to 1.5, it will default to the settings of the maven-compiler-plugin.
How do you change the settings of the maven-compiler-plugin?

S
Stefan Falk

The m2eclipse plugin doesn't use Eclipse defaults, the m2eclipse plugin derives the settings from the POM. So if you want a Maven project to be configured to use Java 1.6 settings when imported under Eclipse, configure the maven-compiler-plugin appropriately, as I already suggested:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>

If your project is already imported, update the project configuration (right-click on the project then Maven V Update Project Configuration).


Please don't shout. That's just rude. It did work after deleting and re-importing the project. But not otherwise.
@Raoul It work without reimporting the project if you Update Project Configuration as I wrote. I did test this 3 times and it just works on my machine.
As clear as Pascal's answer is (I plus 1'd it) 'update project configuration' doesn't seem to work in all circumstances as I had to remove the project as well, and I tried a lot of other things (perhaps a bug in a version of eclipse)
for newer Java versions, see this answer
S
Stefan Falk

I added this to my pom.xml below the project description and it worked:

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

This is simpler than the accepted answer and also was good for me.
This worked for me however the accepted answer did not work.
What's the difference between this and configuring maven-compiler-plugin (as per accepted answer)? Both ways are working for me.
If you are using Java 9, be aware that the version numbering has changed. So use 9 instead of 1.9. <properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties>
I thought setting a property "java.version" and using it in the build configuration of the "maven-compiler-plugin" configuration was enough. Obviously, "maven.compiler.source" and "maven.compiler.target" properties must have some Eclipse Maven plugin interaction as the problem just vanished.
N
Nicholas

I wanted to add something to the answer already provided. maven-compiler-plugin by default will compile your project using Java 1.5 which is where m2e get's its information.

That's why you have to explicitly declare the maven-compiler-plugin in your project with something other then 1.5. Your effective pom.xml will implicitly use the default set in the maven-compiler-plugin pom.xml.


to me this more clearly answers "why is it 1.5 when I make a new Maven project?"
@LIttleAncientForestKami verified with 3.7.0, by now the values are set as constants DEFAULT_SOURCE and DEFAULT_TARGET, but still both set to 1.5.
now it should be 1.6, right? I wonder becuase it still seems to use 1.5 for me.
@Line: No, it will likely be 1.5 until the end of the known maven-verse as the super effective parent pom shouldn't ever change.
Is this irony? ;) The site which is linked in the answer says "Also note that at present the default source setting is 1.6 and the default target setting is 1.6"..
S
Stefan Falk
<project>

    <!-- ... -->

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Inside
don't like this answer, because it's only code, and I don't see advantage over the accepted one (maybe because it's not described..)
T
Tony Vu

Your JRE was probably defined in run configuration. Follow these steps in Eclipse to change the build JRE.

1) Right click on the project and select Run As > Run Configurations

2) From Run Configurations window, select your project build configuration on the left panel. On the right, you will see various tabs: Main, JRE, Refresh, Source,...

3) Click on JRE tab, you should see something like this

https://i.stack.imgur.com/RPisT.png

4) By default, Work Default JRE (The JRE you select as default under Preferences->Java->Installed JREs) will be used. If you want to use another installed JRE, tick the Alternate JRE checkbox and select your preferred JRE from the dropdown.


Thanks for this answer, my run configuration was running with workspace default so this really helped!
This helped, can't believe we overlooked this!
I had the "Fatal error compiling: invalid target release: 11" exception during compilation. mvn -version revealed that mvn used Java 8 instead, also both Eclipse and CMD used Java11. The reason was as mentioned here: alternate JSE configuration. Thanks for the advice!
r
rogue lad

Here is the root cause of java 1.5:

Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target.

Reference : Apache Mavem Compiler Plugin

Following are the details:

Plain pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

    <groupId>com.pluralsight</groupId>
    <artifactId>spring_sample</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

Following plugin is taken from an expanded POM version(Effective POM),

This can be get by this command from the command line C:\mvn help:effective-pom I just put here a small snippet instead of an entire pom.

    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
      <execution>
        <id>default-compile</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
      <execution>
        <id>default-testCompile</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Even here you don't see where is the java version defined, lets dig more...

Download the plugin, Apache Maven Compiler Plugin » 3.1 as its available in jar and open it in any file compression tool like 7-zip

Traverse the jar and findout

plugin.xml

file inside folder

maven-compiler-plugin-3.1.jar\META-INF\maven\

Now you will see the following section in the file,

      <configuration>
    <basedir implementation="java.io.File" default-value="${basedir}"/>
    <buildDirectory implementation="java.io.File" default-value="${project.build.directory}"/>
    <classpathElements implementation="java.util.List" default-value="${project.testClasspathElements}"/>
    <compileSourceRoots implementation="java.util.List" default-value="${project.testCompileSourceRoots}"/>
    <compilerId implementation="java.lang.String" default-value="javac">${maven.compiler.compilerId}</compilerId>
    <compilerReuseStrategy implementation="java.lang.String" default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy>
    <compilerVersion implementation="java.lang.String">${maven.compiler.compilerVersion}</compilerVersion>
    <debug implementation="boolean" default-value="true">${maven.compiler.debug}</debug>
    <debuglevel implementation="java.lang.String">${maven.compiler.debuglevel}</debuglevel>
    <encoding implementation="java.lang.String" default-value="${project.build.sourceEncoding}">${encoding}</encoding>
    <executable implementation="java.lang.String">${maven.compiler.executable}</executable>
    <failOnError implementation="boolean" default-value="true">${maven.compiler.failOnError}</failOnError>
    <forceJavacCompilerUse implementation="boolean" default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
    <fork implementation="boolean" default-value="false">${maven.compiler.fork}</fork>
    <generatedTestSourcesDirectory implementation="java.io.File" default-value="${project.build.directory}/generated-test-sources/test-annotations"/>
    <maxmem implementation="java.lang.String">${maven.compiler.maxmem}</maxmem>
    <meminitial implementation="java.lang.String">${maven.compiler.meminitial}</meminitial>
    <mojoExecution implementation="org.apache.maven.plugin.MojoExecution">${mojoExecution}</mojoExecution>
    <optimize implementation="boolean" default-value="false">${maven.compiler.optimize}</optimize>
    <outputDirectory implementation="java.io.File" default-value="${project.build.testOutputDirectory}"/>
    <showDeprecation implementation="boolean" default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
    <showWarnings implementation="boolean" default-value="false">${maven.compiler.showWarnings}</showWarnings>
    <skip implementation="boolean">${maven.test.skip}</skip>
    <skipMultiThreadWarning implementation="boolean" default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
    <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
    <staleMillis implementation="int" default-value="0">${lastModGranularityMs}</staleMillis>
    <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
    <testSource implementation="java.lang.String">${maven.compiler.testSource}</testSource>
    <testTarget implementation="java.lang.String">${maven.compiler.testTarget}</testTarget>
    <useIncrementalCompilation implementation="boolean" default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
    <verbose implementation="boolean" default-value="false">${maven.compiler.verbose}</verbose>
    <mavenSession implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
    <session implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
  </configuration>

Look at the above code and find out the following 2 lines

    <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
    <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>

Good luck.


Yes. This has nothing to do with Eclipse. For example, you get the same issue in IntelliJ IDEA if create a Maven project with the quickstart artifact.
Hi, thank you for posting the root cause of the problem : ) I really appreciate it!
s
sancho21

Simplest solution in Springboot

I'll give you the simplest one if you use Springboot:

<properties>
  <java.version>1.8</java.version>
</properties>

Then, right click on your Eclipse project: Maven > Update project > Update project configuration from pom.xml

That should do.


G
Gábor Lipták

One more possible reason if you are using Tycho and Maven to build bundles, that you have wrong execution environment (Bundle-RequiredExecutionEnvironment) in the manifest file (manifest.mf) defined. For example:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Engine Plug-in
Bundle-SymbolicName: com.foo.bar
Bundle-Version: 4.6.5.qualifier
Bundle-Activator: com.foo.bar.Activator
Bundle-Vendor: Foobar Technologies Ltd.
Require-Bundle: org.eclipse.core.runtime,
 org.jdom;bundle-version="1.0.0",
 org.apache.commons.codec;bundle-version="1.3.0",
 bcprov-ext;bundle-version="1.47.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.5
Export-Package: ...
...
Import-Package: ...
...

In my case everything else was ok. The compiler plugins (normal maven and tycho as well) were set correctly, still m2 generated old compliance level because of the manifest. I thought I share the experience.


That's it! Thank you very much! Also when building with maven and using DEBUG log level you could see the wrong execution environment setting and where the setting has its origin: "[DEBUG] Using execution environment 'JavaSE-1.8' configured in Bundle-RequiredExecutionEnvironment"
m
manuelvigarcia

Project specific settings

One more place where this can go wrong is in the project specific settings, in Eclipse.

project properties: click your project and one of the following: Alt + Enter Menu > Project > Properties right click your project > project properties (last item in the menu) click on "Java Compiler" Uncheck "Enable project specific settings" (or change them all by hand).

Because of client requirements we had them enabled to keep our projects in 1.6. When it was needed to upgrade to 1.7, we had a hard time because we needed to change the java version all over the place:

project POM

Eclipse Workspace default

project specific settings

executing virtual machine (1.6 was used for everything)


C
Christian Gabrisch

In case anyone's wondering why Eclipse still puts a J2SE-1.5 library on the Java Build Path in a Maven project even if a Java version >= 9 is specified by the maven.compiler.release property (as of October 2020, that is Eclipse version 2020-09 including Maven version 3.6.3): Maven by default uses version 3.1 of the Maven compiler plugin, while the release property has been introduced only in version 3.6.

So don't forget to include a current version of the Maven compiler plugin in your pom.xml when using the release property:

<properties>
    <maven.compiler.release>15</maven.compiler.release>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
    </plugins>
</build>

Or alternatively but possibly less prominent, specify the Java version directly in the plugin configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>15</release>
            </configuration>
        </plugin>
    </plugins>
</build>

This picks up Line's comment on the accepted answer which, had I seen it earlier, would have saved me another hour of searching.


S
Stefan Falk

I found that my issue was someone committed the file .project and .classpath that had references to Java1.5 as the default JRE.

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
    <attributes>
        <attribute name="owner.project.facets" value="java"/>
    </attributes>
</classpathentry>

By closing the project, removing the files, and then re-importing as a Maven project, I was able to properly set the project to use workspace JRE or the relevant jdk without it reverting back to 1.5 . Thus, avoid checking into your SVN the .project and .classpath files

Hope this helps others.


u
user1201626

If you want to make sure that newly created projects or imported projects in Eclipse use another default java version than Java 1.5, you can change the configuration in the maven-compiler-plugin.

Go to the folder .m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1

Open maven-compiler-plugin-3.1.jar with a zip program.

Go to META-INF/maven and open the plugin.xml

In the following lines: ${maven.compiler.source} ${maven.compiler.target}

change the default-value to 1.6 or 1.8 or whatever you like.

Save the file and make sure it is written back to the zip file.

From now on all new Maven projects use the java version you specified.

Information is from the following blog post: https://sandocean.wordpress.com/2019/03/22/directly-generating-maven-projects-in-eclipse-with-java-version-newer-than-1-5/


A
Achref EL Mouelhi

To change JDK's version, you can do:

1- Project > Properties 2- Go to Java Build Path 3- In Libraries, select JRE System ... and click on Edit 4- Choose your appropriate version and validate