Setting Java Version used by Maven Compiler

Learn to configure the Java compiler version in a Maven project using different approaches. Also, learn to set the Java version for a Spring boot application using Maven as a build tool.

1. Using the Maven Compiler Plugin

The most straightforward solution to specify the Java version to compile the project is using the Maven compiler plugin properties. The following configuration will compile the project using Java 17.

  • maven.compiler.source: will allow the language features from Java 17 (and previous versions) in the source code.
  • maven.compiler.target: will generate the compiled classes compatible with Java 17. The application will run on Java 17 and later versions. It will not run in the previous versions of Java 17.

The default value for both properties is Java 1.8 in the latest version of the Maven compiler plugin.

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    ...

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

	...
</project>

The same configuration can directly be applied to the compiler plugin as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    ...

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

	...
</project>

Avoid Pitfall due to Version Mismatch

Note that setting the target option does not guarantee that the application actually runs on a JRE with the specified version. If we have used the source with a later version and used new language features then the code will fail at runtime with a linkage error.

Consider the following configuration where source is set to Java 17 and target is set to Java 11.

<properties>
	<maven.compiler.source>17</maven.compiler.source>
	<maven.compiler.target>11</maven.compiler.target>
</properties>

If we use the Java 17 specific new Language features (such as Sealed Classes) then, in runtime, the project will fail with a linkage error.

2. Cross-Compilation Support Since Java 9

As noticed earlier, the default source and target attributes don’t guarantee a cross-compilation preventing the application from running on the older JDK versions.

We can enable the cross-compilation, allowing the application to run on older Java versions, by using the maven.compiler.release property.

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

The same configuration can be written with the release attribute in the Maven compiler plugin configuration:

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

Note that, for cross-compilation, we must have installed Java 9 or later in the machine. We can check the Java version used by Maven using the ‘mvn -v’ command. Maven uses the JAVA_HOME parameter to find which Java version it is supposed to run.

mvn -v

Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: C:\devsetup\maven\bin\..
Java version: 19-loom, vendor: Oracle Corporation, runtime: C:\devsetup\JDKs\jdk19
Default locale: en_IN, platform encoding: UTF-8
OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"

3. Set Java Version in a Spring Boot Project

The Spring boot applications use java.version property for compiling the project’s source code. Note that java.version property is specific to the Spring boot framework only.

We can specify this property in the pom.xml as follows:

<properties>
    <java.version>17</java.version>
</properties>

It is worth knowing that if we do not define this property then Spring boot resolves this property from the starter POM file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
</parent>
  • The default and minimum Java version for Spring Boot 2.x projects is Java 1.8.
  • The default and minimum Java version for Spring Boot 3.x projects is Java 17.

4. Changing the Java Version from Command Line

If we have not overridden the Java versions in the pom.xml file and the project is using the default Java version used by Maven, we can still compile the project with a different Java version. To do so, we can change the JAVA_HOME environment variable in the command prompt and then use the mvn build command to build the application binary.

JAVA_HOME=/path/to/jdk17/ mvn build

5. Conclusion

The key takeaways from this tutorial are:

  • Use java.version property to configure the Java version for a Spring boot application.
  • Use maven.compiler.source and maven.compiler.target properties for other Java applications.
  • Use maven.compiler.release property for cross-compilation. It is usually a better approach than the former.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.