Setting the Java Version in Maven

1. Overview

In this quick tutorial, we’ll show how to set the Java version in Maven.

Before moving on, we can check the default JDK version of Maven. Running the mvn -v command will show the Java version in which Maven runs.

2. Use the Compiler Plugin

We can specify the desired Java version in the compiler plugin.

2.1. Compiler Plugin

The first option is setting the version in compiler plugin properties:

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

The Maven compiler accepts this command with –target and –source versions. If we want to use the Java 8 language features, the –source should be set to 1.8.

Also, for the compiled classes to be compatible with JVM 1.8, the –target value should be 1.8.

The default value for both of them is the 1.6 version.

Alternatively, we can configure the compiler plugin directly:

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

The maven-compiler-plugin also has additional configuration properties that allow us to have more control over the compilation process beyond -source and -target versions.

2.2. Java 9 and Beyond

Furthermore, starting from the JDK 9 version, we can use a new -release command-line option. This new argument will automatically configure the compiler to produce class files that will link against the implementation of the given platform version.

By default, the -source and -target options don’t guarantee a cross-compilation.

This means that we cannot run our application on older versions of the platform. Additionally, to compile and run the programs for older Java versions, we also need to specify -bootclasspath option.

To cross-compile correctly, the new -release option replaces three flags: -source, -target and -bootclasspath.

After transforming our examples, we can declare the following for the plugin properties:

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

And for the maven-compiler-plugin starting from the 3.6 version, this is what we can write:

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

Notice that we can add the Java version in a new <release> attribute. In this example, we compile our application for Java 7.

What’s more, we don’t need a JDK 7 installed in our machine. Java 9 already contains all the information for linking the new language features with JDK 7.

3. Spring Boot Specification

Spring Boot applications specify the JDK version inside of the properties tags in the pom.xml file.

First, we need to add spring-boot-starter-parent as a parent to our project:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
</parent>

This parent POM allows us to configure default plugins and multiple properties including the Java version — by default, the Java version is 1.8.

However, we can override the default version of the parent by specifying the java.version property:

<properties>
    <java.version>1.9</java.version>
</properties>

By setting the java.version property, we declare that the source and the target Java versions are both equal to 1.9.

Above all, we should keep in mind that this property is a Spring Boot Specification. Additionally, starting from Spring Boot 2.0, Java 8 is the minimum version.

This means we can’t use or configure Spring Boot for the older JDK versions.

4. Conclusion

This quick tutorial demonstrated the possible ways of setting Java version in our Maven project.

Here’s a summary of the main takeaways:

  • Using <java.version> is possible only with the Spring Boot application.
  • For simple cases, maven.compiler.source and maven.compiler.target properties should be the best fit.
  • Finally, to have more control over the compilation process, use the maven-compiler-plugin configuration settings.

Related posts:

Runnable vs. Callable in Java
Java Program to Implement Bloom Filter
Java Program to Implement ConcurrentHashMap API
Hướng dẫn Java Design Pattern – Strategy
Returning Custom Status Codes from Spring Controllers
Hướng dẫn Java Design Pattern – Adapter
Java Program to Find Transitive Closure of a Graph
Chuyển đổi Array sang ArrayList và ngược lại
Intro to the Jackson ObjectMapper
Giới thiệu Design Patterns
Create Java Applet to Simulate Any Sorting Technique
Java Program to Implement Variable length array
Spring Boot - CORS Support
Java Program to Generate Random Numbers Using Middle Square Method
Vòng lặp for, while, do-while trong Java
Java Program to Perform Stooge Sort
Working with Tree Model Nodes in Jackson
Summing Numbers with Java Streams
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement ArrayBlockingQueue API
Xây dựng ứng dụng Client-Server với Socket trong Java
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Implement Randomized Binary Search Tree
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Mệnh đề Switch-case trong java
Copy a List to Another List in Java
Hướng dẫn Java Design Pattern – Decorator
New Features in Java 10
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x