Maven Java Compiler Version

Jakob Jenkov
Last update: 2020-02-09

The default Java compiler version used by Maven is Java 1.5 . Why that is still the default Java compiler version is a good question, since Java 5 was released in 2004. To make Maven compile your Java code with a newer version of the Java compiler, you need to specify the Java compiler explicitly in your project's POM file (pom.xml).

Exactly how you specify the Java compiler version depends on whether you are using Java 8 or earlier, or Java 9 or later. From Java 9 Java got modules which complicates the compilation somewhat in the short run, but should help in the long run.

Maven Java Compiler for Java 8 and Earlier

From Java 8 and earlier, there are two ways to set the Java compiler version in a Maven POM file:

  1. Via the Maven Java compiler properties.
  2. Via the Maven Java compiler plugin.

Both of these methods to set the Java compiler version in Maven will be explained in the following sections.

Maven Java Compiler Properties

The first, newest and easiest way to set the Java compiler version in your Maven POM file, is via the Maven Java compiler properties. Here is how the Maven Java compiler properties look:

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

These properties have to be included in the properties element of your POM file. I usually have the properties element as the last element in my POM files.

Maven Java Compiler Plugin

The second, oldest, and more verbose to set the Java compiler version in your Maven POM files, is via the Maven Java compiler plugin. Here is how the configuration of the Maven Java compiler plugin looks:

<build>
  <plugins>

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

Full Maven POM File With Java Compiler Version Set

For your convenience, here is a full Maven POM file with the Java compiler version set using both of the above mechanisms. Please note, that only one of the mechanisms should be used in your POM file. The example only shows both examples so you can see where in the POM file they are to be placed.

<project
 xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

<modelVersion>4.0.0</modelVersion>

<groupId>com.nanosai</groupId>
<artifactId>grid-ops</artifactId>
<version>0.8.0</version>
<packaging>jar</packaging>


<dependencies>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>


</dependencies>


<build>
  <plugins>

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

<properties>
  <project.build.sourceEncoding>
      UTF-8
  </project.build.sourceEncoding>
  <maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.source>1.8</maven.compiler.source>
</properties>

</project>

Maven Java Compiler for Java 9 and Later

For Java 9 and later, you need to use a slight variant of the Java compiler plugin. Instead of the source and target properties, you need to use the release property. Here is an example of how the Maven Java compiler plugin looks with a release property instead:

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

This example sets the Java version to 11 - inside the release property. Java 11 is the latest long-term-support (LTS) version of Java at the time of writing.

Notice also that the version of the Maven Java compiler plugin has changed from 3.6.1 to 3.8.0 .

Please keep in mind, that when you upgrade to Java 9+ you will most likely get more issues, like assuring that all Maven plugins and dependencies your Java project uses, are also Java 9+ compatible. Going into detail about how this is done is beyond the scope of this Maven Java compiler tutorial though.

Enable Preview Features

Some of the later Java versions (Java 10, 12 and 13 at least) have had some features which were in preview. A feature in preview is a feature that the Java SDK team has implemented but are not yet sure if should stay in the Java SDK long term. These features are not enabled in the Java SDK by default. To use them you must explicitly enable them. To enable Java preview features in your Maven POM file, insert the compilerArgs element into the compiler configuration, as shown below:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>11</release>
        <compilerArgs>--enable-preview</compilerArgs>
    </configuration>
</plugin>

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next