16

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

enter image description here

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

Why?

How to make it be other?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • Check your installed JRE's, and which JRE maven is configured to use under Windows > preferences. – Taylor Jan 20 '14 at 20:37
  • Possible duplicate http://stackoverflow.com/questions/3539139/what-causes-a-new-maven-project-in-eclipse-to-use-java-1-5-instead-of-java-1-6-b/43210342#43210342 – rogue lad Apr 04 '17 at 14:39
  • @Taylor from the link above (accepted answer) it seems you're not right: "The m2eclipse plugin doesn't use Eclipse defaults, the m2eclipse plugin derives the settings from the POM." – Line Dec 31 '18 at 00:30
  • @Line if OP is creating a default `default empty based on no archetype Maven project in Eclipse` then there is no pom. – Taylor Dec 31 '18 at 17:43
  • @Taylor but it's cretaed. The defaults point (now already) to 1.6, as defined here: https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html. – Line Jan 01 '19 at 14:41

3 Answers3

17

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

MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • 2
    It is no sense to put settings into `pom.xml`. `pom.xml` appears only AFTER project is already created and AFTER java 1.5 is already set. I would not go to `pom.xml` since I would just go to `Build Path` configuration and change java version there. I need some PRESET configuration, so that NEW files will be java 7 based, i.e. BEFORE project `pom.xml` created. – Suzan Cioc Jan 22 '14 at 20:35
  • Your settings worked in `MYMAVEN/conf/settings.xml` file, while I don't see if it is possible to put plugin code @axtavt suggested there – Suzan Cioc Jan 22 '14 at 20:39
  • Put this properties into `pom.xml` and **reimport** maven project from Eclipse. – MariuszS Jan 22 '14 at 20:45
  • 3
    So you suggest (1) create Eclipse project, (2) edit `pom.xml`, (3) reimort? These are 3 steps. I want one: (1) create Eclipse project. So that it be already 1.7 – Suzan Cioc Jan 22 '14 at 21:02
  • Add this information to question, or better ask another question :) I'm not using Eclipse anymore (IntelliJ is much better), so I can help you with one step configuration. My answer describes Maven configuration, Eclipse is just a tool, you can use this project without Eclipse. – MariuszS Jan 22 '14 at 21:04
  • 1
    Hmm... My question is definitely about Eclipse and tagged appropriately. – Suzan Cioc Jan 22 '14 at 21:26
  • Your question is *How to make it be other?*. Fix pom.xml and reimport and this is done :) This is common pattern for doing this, creating project is not very often task :) – MariuszS Jan 22 '14 at 21:27
  • This answers "how to fix it" part, but not "why is that". Could you please add this part by mentioning it's derived in POM and pointing out to https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html site, which say what is default value for source and target values? – Line Dec 31 '18 at 00:33
7

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1
    To `` section of your `pom.xml`. If your Eclipse project is already generated, click `Update project` after adding it. – axtavt Jan 23 '14 at 08:09
1

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/