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:
<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 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 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:
<properties> <maven.compiler.release>7</maven.compiler.release> </properties>
And for the maven-compiler-plugin starting from the 3.6 version 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>
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:
<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 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.
This quick tutorial demonstrates the possible ways of setting Java version in our Maven project.
- 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