score:253

Accepted answer

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

score:0

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

  • 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/

score:0

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

score:1

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.

score:2

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.

score:2

project specific settings

one more place where this can go wrong is in the project specific settings, in eclipse.

  1. 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)
  2. click on "java compiler"

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

score:2

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.

score:4

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.

score:8

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.

score:9

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

enter image description here

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.

score:27

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

score:29

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.

score:36

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>

Related Query

More Query from same tag