How to correctly use java version in Spring Boot applications?

How to correctly use java version in Spring Boot applications?

How to correctly use java version in Spring Boot applications?

In this article, we are trying to explore the correct way to use java version in Spring Boot application. Before getting into detail, lets first see the issues we face while declaring java version.

Issues in setting up your first app – use java version in Spring Boot correctly.

How to use java version correctly in Spring or Spring Boot applications? The issue especially occurs for the higher version, for example, Java 11. You could see below errors in version is not setup correctly –

We use <java.version> property to configure versions in Spring Boot projects. Here’s the right way of using java version in pom.xml for different versions.

1. Java 1.8
<properties>
   <java.version>1.8</java.version>
</properties>
2. Java 9
<properties>
   <java.version>9</java.version>
</properties>
3. Java 10
<properties>
   <java.version>10</java.version>
</properties>
4. Java 11
<properties>
   <java.version>11</java.version>
</properties>
5. Java 12
<properties>
   <java.version>12</java.version>
</properties>

What do we change when we provide <java.version> property?

Spring Boot internally uses maven-compiler-plugin to compile. By changing <java.version> we are telling Spring Boot to internally manage the <release> and <target> attributes of maven-compiler-plugin to the <java.version> value.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.9.0</version>
    <configuration>
        <release>9</release>
        <target>9</target>
    </configuration>
</plugin>

Note that <release> & <target> are same as <java.version> for Java 9 here.

Explanation

Any Spring Boot application will have a pom with a parent pom spring-boot-starter-parent. This parent form has the declaration of java version as –

<properties>
    <java.version>11</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 and config parameters. Due to the behavior of the user property, set these two properties to 11 means to set the following

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

Summary

Hope by now you’ve understood how to use java version in Spring Boot applications correctly. Fee free to comment in case you have any query.

Please feel free to ask your questions in the comments section and let us know if you feel the article has helped you in understanding the use of java version in Spring Boot projects. 

References

https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html#getting-started-installation-instructions-for-java

Recommendation

Was this page helpful? Please use the commenting form below to ask any questions or to make a comment. Please do not put the same comment multiple times. Your comment will appear after some time. Also please note that we do not reply to emails or comments on social media. So if you have any question, please put it in the form below and we will try to reply to it as soon as possible. If you are facing any issue with the code provided in the blog, please be as specific with your requirements as possible and share the complete stack trace with us. We usually reply within a day. 

Leave a Reply

Your email address will not be published. Required fields are marked *