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

℡╲_俬逩灬. 提交于 2019-12-04 10:22:14

问题


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

I am to change manually both Build Path entry and code compliance.

Why?

How to make it be other?


回答1:


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




回答2:


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.




回答3:


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/



来源:https://stackoverflow.com/questions/21243619/why-maven-project-is-tied-to-j2se-1-5-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!