How to specify Java version in Spring/Spring Boot pom.xml?
Asked Answered
G

5

74

What is the correct way of specifying Java 11 as the version to be used in Spring (or Spring Boot) pom.xml file?

I.e., what value do I need to put in the java.version property of the pom?

For Java 8 we use 1.8, just like the documentation shows, for Java 9 it's 1.9.

I'm not sure if Java 11 would be 1.11 (although it seems unlikely), and I've seen it specified as just 11 when using maven-compiler-plugin, however I'm not using the compiler plugin.

e.g.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>11</release>
    </configuration>
</plugin>

I've searched around and couldn't find the answer, as pretty much any documentation I've read as well as blog posts related to the Java version only show versions 8 or 9.

So what should I use? 1.11 or just 11?

I tried both and my web server seems to start correctly and compile correctly (IntelliJ shows Information:javac 11.0.2 was used to compile java sources) however I'm not entirely convinced that changing the java.version value is doing anything, because I can set it to e.g. 100 and everything works fine.

The only related questions I could find are: Minimum Spring version compatible with Java 11, Spring Boot fails due to a Hibernate error after migrating to JDK 11 and Spring 4.3.x with OpenJDK 11, but they don't really shed any light.

Gamut answered 31/1, 2019 at 18:44 Comment(3)
After Java 9 there is no "1.xx" anymore. It's just 9, 10 and 11.Herminiahermione
The configuration you have given is perfect. Using the most recent maven-compiler-plugin and use <release>11</release>...Talmud
yes, in works in IntelliJ IDEA 2022.3.3 (Community Edition) with openjdk 11.0.18 2023-01-17 - OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu120.04.1), i just specifed java version for plugin in pom, and bug fixed.Heptad
T
170

Short Answer:

The correct way is to use the followings values in <java.version> for different Java versions:

  • Java 8 : 1.8 or 8
  • Java 9 : 9
  • Java 10 : 10
  • Java 11 : 11
  • Java 12 : 12
  • .....
  • .....
  • Java 17 : 17
  • Java 18 : 18
  • Java 19 : 19

So for Java 11 , it should be:

<properties>
   <java.version>11</java.version>
</properties>

However I'm not sure if Java 11 would be "1.11" (seems unlikely), and I've seen it specified as just "11" when using maven-compiler-plugin, however I'm not using the compiler plugin.

Actually , at the end it still uses maven-compiler-plugin to compile. Springboot just configures a <java.version> property such that by changing this value , you are implicitly changing maven-compiler-plugin 's <source/> and <target/> to the same value as what specified in the <java.version> :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>11</source>  <!-- same as <java.version> -->
        <target>11</target>    <!-- same as <java.version> -->
    </configuration>
</plugin>

Detailed Answer:

Seem like you want details to convince you.

It is because every spring boot project will extend the parent pom spring-boot-starter-parent which defines <java.version> as follows:

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

From the maven-compiler-plugin docs, maven.compiler.source and maven.compiler.target are the user property for the <source> and <target> config parameters. Due to the behaviour of the user property, setting these two properties to 11 means to set the following :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>11</source>   <!-- maven.compiler.source  -->
        <target>11</target> <!-- maven.compiler.target -->
    </configuration>
</plugin>

From the maven-compiler-plugin docs again, <source> and <target> are the -source and -target argument for the Java compiler (javac). Then, from javac docs, we can see that these two arguments are allowed to have the following values:

  • 1.6 : No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as was done in earlier releases of Java Platform, Standard Edition.
  • 6 : Synonym for 1.6.
  • 1.7 : The compiler accepts code with features introduced in Java SE 7.
  • 7 : Synonym for 1.7.
  • 1.8 : The compiler accepts code with features introduced in Java SE 8.
  • 8 : Synonym for 1.8.
  • 9 : The compiler accepts code with features introduced in Java SE 9.
  • 10 : The compiler accepts code with features introduced in Java SE 10.
  • 11 : The compiler accepts code with features introduced in Java SE 11.
  • 12 : The compiler accepts code with features introduced in Java SE 12.

Hence, <java.version> should be set to 11 for Java 11.

Tymon answered 31/1, 2019 at 19:42 Comment(4)
Aha. Nice explanation! This makes everything much clearer. Thanks.Gamut
Consider some projects if not most, in big systems, use spring-boot bom importing instead of extending it's parent. And bom import does not import properties nor plugin versionsJessy
I have java.version in my pom, set to 1.8, but still when I run mvn spring-boot:Run, it says "Starting MyApplication using Java 11.0.15 ...". That's the version my JAVA_HOME points to. Shouldn't the java.version property take precedence over that?Jehiah
Nice ! that's clearVendace
H
10

If any one looking to specifying Java 11 as the version to be used in Spring (or Spring Boot) in a gradle project, then use either sourceCompatibility = '11' or sourceCompatibility = '1.11' in your build.gradle file

plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
}

apply plugin: 'io.spring.dependency-management'

group = 'au.com.ranuka'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '11'
//or 
sourceCompatibility = '1.11'

repositories {
   mavenCentral()
}

dependencies {

}
Herzberg answered 7/6, 2019 at 0:1 Comment(2)
As this is the top result in Google search, much appreciatedDichotomy
sourceCompatibility = JavaVersion.VERSION_11, It works.Nata
H
7

I am adding it as an answer so it can be helpful to more people:

Since Java 9 the versions are specified as 9, 10, 11, and so on, instead of 1.8, 1.7, etc. Some further reading here.

The way you are specifying the JDK version with <release>11</release> is correct. It was introduced with maven-compiler-plugin 3.6 and is equivalent as the old way, as below:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <source>11</source> -- Using JDK 11 to compile
      <target>11</target> -- Using JDK 11 as target
    </configuration>
  </plugin>
</plugins>

This post can be helpful on using maven-compiler-plugin with JDK 11.

Herminiahermione answered 31/1, 2019 at 19:37 Comment(0)
T
2

You should not need to mention the maven-compiler-plugin in your POM file, if you only want to specify the compiler version. Instead, add these properties to your pom file

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

Edit: My bad, it looks like the version number properties have changed starting with the 3.6 release of the maven-compiler-plugin. The example above is fine for java 8 and lower. For java 9, add this property:

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>

Check out the accepted answer to this question: Specifying java version in maven - differences between properties and compiler plugin

Trioecious answered 31/1, 2019 at 18:49 Comment(1)
First this is simply wrong cause there does not exist a JDK version 1.11 furthermore starting with JDK9+ there is an option --release for the compiler which is supported by maven-compiler-plugin via <release>11</release>...Talmud
N
0

Gradle API provides an enum for JavaVersion. We can user these constant to define sourceCompatibility.

eg. sourceCompatibility = JavaVersion.VERSION_11

Below are the few constants

VERSION_1_8 - Java8
VERSION_1_9 - Java9
VERSION_1_10- Java10
VERSION_11 - Java11
VERSION_12 - Java12
VERSION_13 - Java13
Nata answered 11/1, 2023 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.