Why Maven project is tied to J2SE-1.5 by default?

前端 未结 3 1174
南笙
南笙 2021-02-07 11:48

If I create default empty based on no archetype Maven project in Eclipse, it will be based on J2SE-1.5.

相关标签:
3条回答
  • 2021-02-07 12:12

    It's consistent with source and target settings of maven-compiler-plugin, which are 1.5 by default.

    If you change them, generated project will use different version of as well:

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

    Note that if you change compliance settings of Eclipse project without changing of maven-compiler-plugin settings, your Maven builds would be inconsistent with your Eclipse environment.

    0 讨论(0)
  • 2021-02-07 12:32

    If you want to make sure that newly created 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.7 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/

    0 讨论(0)
  • 2021-02-07 12:38

    @axtavt is right, add source level configuration to your project. But do not configure maven-compiler-plugin, simply put this properties into pom.xml.

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

    After this refresh maven configuration in Eclipse.

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