I just announced the new Spring Boot 2 material, coming in REST With Spring:


1. Overview

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

Before moving ahead, 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:


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 1.6 version.

Alternatively, we can configure the compiler plugin directly:


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 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, for the plugin properties we can declare:


And for the maven-compiler-plugin starting from the 3.6 version we can write:


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

Even 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:


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:


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 the 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 demonstrates the possible ways of setting Java version in our Maven project.

In summary:

  • 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

I just announced the new Spring Boot 2 material, coming in REST With Spring:


Leave a Reply

Notify of