Roufid

3 ways to solve Fatal error compiling: invalid target release in Maven build

Maven

Problem

You may encounter the following error when running a Maven build:

Fatal error compiling: invalid target release: <JDK_VERSION>

Solution

Below three ways to solve the problem:

  1. Adding the environment variable JAVA_HOME
  2. Using Maven on Eclipse
  3. Configure your pom.xml by forking the maven-compiler-plugin

1- Adding the environment variable JAVA_HOME

This problem is due to the fact that the environment variable JAVA_HOME and the target property of the maven-compiler-plugin point to a different JDK versions. The easy solution will be to fill JAVA_HOME with the correct value.

To verify that, open the pom.xml file of your project and check the target property of the maven-compiler-plugin. For example

The target property points to JDK version 1.7.

Let’s see the value of the JAVA_HOME environment variable used by Maven. Open a prompt command and execute the following command:

Output

Output of mvn -version

The JAVA_HOME points to a JDK 1.6 and this is why Maven throws the exception: invalid target release. You must set the value of JAVA_HOME to the correct JDK 1.7 home directory. Below how to perform it:

On windows

If you are using command line you can set the variable before running the build as following:

The default location in Windows is : C:\Program Files\Java\jdk1.7.x_xx

On Unix

On Mac

in ~/.profile:

2- Using Maven on Eclipse

Another solution if you are using Maven on Eclipse IDE is to set the correct JDK in your Maven configuration:

3- Setting the correct compiler in the Maven-compiler-plugin

Another solution is to fork the maven-compiler-plugin and set the full path to the correct Java compiler as below:

References