1. Overview

In this tutorial, we’re going to look at how we can set the Java version on a Maven project.

To compile our source code, Maven uses the Maven Compiler Plugin which in turn calls the javac tool. In the following sections, we’ll dive into the configuration options of the Compiler Plugin.

2. Java 8 And Below

The javac tool provides –source and –target options to work with different Java versions. We’ll now examine the ways we can set these options using the Maven Compiler Plugin.

2.1. Set Version by Property

Firstly we can set the Java version through the maven.compiler.source and maven.compiler.target properties:

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

Here, Maven Compiler Plugin will read these properties and set the Java version to Java 8.

Note that we’ve specified Java 8 as 1.8, but we can also use just 8:

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

To list all our options, we can use 1.3, 1.4, 1.5, 5, 1.6, 6, 1.7, 7, 1.8 and 8.

2.2. Set Version by Configuration

We can also set the Java version using the source and target configuration options of Maven Compiler Plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

Similar to the previous example, we’re setting the Java version to Java 8.

The source and target options also accept 1.3, 1.4, 1.5, 5, 1.6, 6, 1.7, 7, 1.8 and 8.

2.3. Set Version by Compiler Args

Lastly, we’ll set the Java version by passing compiler arguments in the compilerArgs option:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <compilerArgs>
            <arg>-source</arg>
            <arg>1.8</arg>
            <arg>-target</arg>
            <arg>1.8</arg>
        </compilerArgs>
    </configuration>
</plugin>

This example produces the same result as the previous ones.

2.4. Default Values

So far we’ve set the Java version explicitly. If we don’t set the source and target values, they default to 1.6 – Java 6.

3. Java 9 And Beyond

3.1. Using –source and –target

With the release of Java 9, javac doesn’t support versions less than or equal to 5 in the -source option. So we can only specify 1.6, 6, 1.7, 7, 1.8, 8, 9, 10 and 11 – and also the newer versions.

Thus we can still use the previous configurations for Java 9 and greater.

3.2 Using –release

Beginning with Java 9, javac includes a new option –release. We’ll now use this new option to set the Java version.

Firstly, we’ll set the Java version through the maven.compiler.release property:

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

Here, we’re setting the Java version to 9. Similar to the source option, supported release targets are 6, 7, 8, 9, 10, and 11.

Alternatively, we can set the version by configuring the release option of the Maven Compiler Plugin:

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

Lastly, we’ll set the compilerArgs in the plugin configuration:

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

4. Summary

In this tutorial, we’ve looked at how we can set the Java version using Maven.

Check out the source code for all examples over on Github.

Leave a Reply

Close Menu