This document demonstrates ways to set the java compiler version in maven via the maven.compiler.target property and the maven-compiler-plugin configuration section.
<properties>
<java.version>1.10</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
</properties>
<properties>
<java.version>1.10</java.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
The following is displayed when a compiler mismatch is detected during compile phase. In this example, the java compiler used (1.7) is less than the required version (1.10).
$ java -version
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
The error message as shown on Line 25 of the Maven Console Log
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile
(default-compile) on project mvn-demo-java-compiler-version:
Fatal error compiling: invalid target release: 1.10 -> [Help 1]
$ mvn clean install
$ git clone https://github.com/kapresoft/mvn_set_java_compiler_version.git
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kapresoft.demo</groupId>
<artifactId>mvn-demo-java-compiler-version</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mvn-demo-java-compiler-version</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.10</java.version>
<!-- Note 1: Set the following maven property for compiler version -->
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Note 2: Alternate way for setting java compiler version via the maven-compiler plugin -->
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>-->
</plugins>
</build>
</project>