Fix Maven Build Error invalid target release
- Details
- Written by Nam Ha Minh
- Last Updated on 01 April 2022   |   Print Email
When building a Java project using Maven (for example, run mvn package in command line) you may encounter this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ProjectName: Fatal error compiling: error: invalid target release: 17 -> [Help 1]
The error is invalid target release: 17 - that means the project’s Java version is 17 but Maven is running under lower JDK version. To see the version of JDK that runs Maven, type mvn -v and you will see something like this:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: g:\apache-maven-3.6.3\bin\.. Java version: 15, vendor: Oracle Corporation, runtime: g:\OpenJDK15\jdk-15
That means Maven is using Java version 15, whereas the project uses Java version 17, hence that invalid target release error.
So to fix this kind of error, you can either:
- Change Java version of the project to the one that less than or equal to the version of JDK that runs Maven.
- Change the JDK that runs Maven, to be greater than or equal the project’s Java version
1. Fix invalid target release error by changing project’s Java version
Open the pom.xml file, and update Java version to a version that is less than or equal to the JDK version that runs Maven. For example:
<properties> <java.version>11</java.version> </properties>
This is for a Spring Boot project. For a general Maven project, you may need to update as follows:
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties>
Or maybe you need to update in the Maven compiler plugin’s configuration like this:
<configuration> <release>13</release> </configuration>
Then run mvn package again, the invalid target release error will be gone.
2. Fix invalid target release error by changing Maven’s Java version
Apply this solution if the project’s Java version cannot be lower. That means you need to have another JDK newer than the one that runs Maven. You can check by typing:
java -version
Or:
mvn -v
You need to install a newer JDK and then update JAVA_HOME or PATH environment variables. Follow my guides here (for example, install JDK 17):
Note that you need to open a new command prompt window after updating the environment variables.
3. Fix invalid target release error in IDE
If you encounter the Maven error invalid target release in your IDE such as Eclipse or Spring Tool Suite, follow these steps:
- Open the Run Configurations dialog (Go to menu Run > Run Configurations)
- Select the Run configuration that you use to run Maven build
- Click JRE tab. Then choose the Runtime JRE that has version greater than or equal to your project’s Java version.
Refer to this article to learn more: How to manage JRE installations in Eclipse.
To see how to fix the Maven build error invalid target release in action, watch the following video:
Video:
Comments