• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Maven the default value of source and target version on the maven-compiler-plugin

 
Ranch Hand
Posts: 62
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys,
Where is the default value of java version on the maven-compiler-plugin officially written ?
3.8.x -> source, target 1.6
3.9.x -> source, target 1.7
 
Saloon Keeper
Posts: 28713
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forget. But it's safer if you don't assume. Put in the specific compiler/code versions you know work for the project. Maven likes it when you nail down versions so that if you send your project to another continent or another decade it will still build properly.
 
Marshal
Posts: 4810
604
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache agrees with Tim - specify the configuration for source and target yourself and don't rely on defaults.

The documentation for version 3.13.0 wrote:Also note that at present the default source setting is 1.8 and the default target setting is 1.8, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.

Apache Maven Compiler Plugin:  Introduction
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Setting just maven.compiler.release is less verbose and more secure than setting both maven.compiler.source and maven.compiler.target.
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The defaults are documented at https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html:

NOTE:

Since 3.8.0 the default value has changed from 1.5 to 1.6

Since 3.9.0 the default value has changed from 1.6 to 1.7

Since 3.11.0 the default value has changed from 1.7 to 1.8


Since they have shown they don't mind changing the default value from one release to another, I'd follow the advice given here: specify them explicitly.
 
Tim Holloway
Saloon Keeper
Posts: 28713
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:The defaults are documented at https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html:

NOTE:

Since 3.8.0 the default value has changed from 1.5 to 1.6

Since 3.9.0 the default value has changed from 1.6 to 1.7

Since 3.11.0 the default value has changed from 1.7 to 1.8


Since they have shown they don't mind changing the default value from one release to another, I'd follow the advice given here: specify them explicitly.



They also don't mind defaultin to a very old compiler level. Hence my advice to set it yourself.
 
hiroki inoue
Ranch Hand
Posts: 62
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys, I liked more the posted of Rob Spoor.

Why the Maven does not update the default value of compiler java version to the java version which is referenced from the Maven?
I think it would stand to reason instead of set the version which seems a little old rispect to latest java version.
 
Tim Holloway
Saloon Keeper
Posts: 28713
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maven isn't about "use the latest thing". It's about "use the version we know works". While that primarily applies to the POM artefacts, it also applies to plugins and everywhere else.

If I haven't upgraded my Maven and you have (or vice versa) then when I do a Maven build, and the compiler updates along the way, then the generated bytecodes won't match up and possibly compilation will fail.

On the other hand, explicitly specifying the syntax and bytecode versions makes the POM repeatable.
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strongly agree with Tim.

A Maven project should be built exactly the same way, and yield the same results, on every system it's built, regardless of the tools you have installed on that system.

That means that using default settings that depend on the system that the project is built on, is generally a bad idea.
 
Tim Holloway
Saloon Keeper
Posts: 28713
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back when Maven was young, and my boss couldn't persuade me to switch from Ant because I didn't like how Maven did so much by "magic", there came a call for two of our software development groups to adopt a common development system.

Unfortunately, without Maven, the exact locations and even existence of various critical files and directories was inconsistent even within out department. The other department's setup even conflicted with ours.

With Maven, I can do a "mvn clean", zip up the project directory, ship it to Devaka in Sri Lanka and he could build a running copy of the project in less than an hour with only suitable versions of JDK and Maven. Much more civilised.
 
hiroki inoue
Ranch Hand
Posts: 62
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys, I agree with you to configure the java version in the pom.xml.
I'm happy I discovered the maven has default value on the maven-compiler-plugin.

The origin reason that I started this thread was that I had a problem of Maven build on old java project. The project runs with jdk 8 so I thought I can use a lambda expression. But after added it the Jenkins job of project started to fail. At first I didn't understand why there is a compile error on the lambda expression. In addition to the project compiles on my laptop. And then I noticed the difference of the version of Maven between my laptop and the Jenkins server. I added the below snippet in the pom.xml to solve it.
I thought if the maven would applied the java version of JAVA_HOME to the maven.compiler.source and target I don't have the problem.

I listed the maven version when changed the default value of java version on the maven-compiler-plugin, just for my curious.
Maven versionrelease datedefault valuejava release date
4.0.0-alpha-132024-03-06172021-01 Java 17
4.0.0-alpha-122024-01-1282014-03 Java 8
3.9.02023-01-3182014-03 Java 8
3.8.82023-03-0872011-07 Java 7
3.3.12015-03-1872011-07 Java 7
3.2.52014-12-2062006-12 Java 6
3.2.12014-02-2162006-12 Java 6
3.1.12013-10-0452004-09 Java 5
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're on a sufficiently new compiler version, make sure to use release instead of source and target.

This will ensure that your code is built against the intended standard API version, and not just with the intended compiler version.
 
hiroki inoue
Ranch Hand
Posts: 62
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan, thank you for your post, I didn't know about option "release".
So sum-up, I discovered:

  • maven.compiler.source and target
  • maven.compiler.release *from JDK 9
  • java.version *with Spring Boot Application


  • Thanks a lot!
     
    Stephan van Hulst
    Bartender
    Posts: 15741
    368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't really like that a web application framework takes control of my POM. I generally eschew importing third party POMs or using them as my parent POM.

    I just use Spring Boot's plugins and dependencies, and all the other settings I configure myself. That means that I would be using maven.compiler.release instead of java.version as well.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic