Saturday, 24 June 2023

Maven build error: Fatal error compiling: invalid flag: --release -> [Help 1]

 When try to build the maven java project using mvn clean install or mvn package, you may get this error if maven java version and project java version mismatches.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile
 (default-testCompile) on project spring-boot-postgresql-crud: 
Fatal error compiling: invalid target release: 11 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The error is invalid target release: 11 - that means the project’s Java version is 11 but Maven is running under lower JDK version. 

Need to check maven version which compatible with java version.

you can use the maven command, version -> mvn -version,

command prompt -  maven version command

So maven compatible java version is 1.8 or below versions.

Solutions:-

1) Changing the project java version i.e compatible java maven version - here it's java 1.8
       If the project is a Spring Boot web application, update the Java version in the pom.xml as follows,

<properties>
    <java.version>1.8</java.version>
</properties>

If project is a general java project, update the Java version in the pom.xml as follows,

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

2) Changing maven's java version

     If you don’t want to change the project’s Java version, then update the Maven Java version. Install the new JDK (e.g., JDK 11) and set the environment variables so that JAVA_HOME points to the new JDK path.

3) Changing runtime JRE in IDE(Eclipse, Intellij)

    We can fix this issue in developer tool/ide like eclipse or intellij if you are using these tools to build,

Select Project -> Go to run configurations -> maven build then select or click on JRE

Invalid target maven build issue fix in IDE
Invalid target relase issue fix in IDE

The default or selected JRE should be greater than or equal to the project’s Java version. In this case, you need to select JRE version 11.


Thank you for reading the blog.