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?

前端 未结 13 2100
情深已故
情深已故 2020-11-27 09:51

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

相关标签:
13条回答
  • 2020-11-27 10:10

    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

    0 讨论(0)
  • 2020-11-27 10:12

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

    0 讨论(0)
  • 2020-11-27 10:14

    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)
    0 讨论(0)
  • 2020-11-27 10:14

    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.

    0 讨论(0)
  • 2020-11-27 10:17

    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.

    0 讨论(0)
  • 2020-11-27 10:18

    Simplest solution in Springboot

    I'll give you the most simpler 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.

    0 讨论(0)
提交回复
热议问题