Sunday, December 2, 2018

How to fix invalid target release: 1.7, 1.8, 1.9, or 1.10 Error in Maven Build

If you are building your Java project using Maven, maybe in Eclipse or from the command prompt by running mvn install and your build is failing with an error like "invalid target release: 1.7" or "invalid target release: 1.8" then you have come to the right place. In this article, I'll show you the reason why this error occurs and how you can deal with these errors even with higher Java version like Java 9, 10 installed on your machine, or maybe with Java 11 in the coming month. The root cause of the problem is that you have specified a higher Java version in your pom.xml file for Maven compiler plugin then what Maven knows in your system, and that's why it's saying invalid target release.

A simple solution to this problem is either reduce your target version in pom.xml or install a new Java version if you want to build your project in higher version

But, the key to solving this problem is knowing that Maven picks the Java version from the JAVA_HOME variable and not from the PATH environment variable. 

This means even if you have JDK 8 installed but if your JAVA_HOME is still referring to JDK 1.7 then you will get this error.

Thankfully, you can find out that, I mean which version of Java your Maven is using. Just run the mvn -version command from the command prompt and it will print the value of JAVA_HOME variable and confirm that which version of JDK it is using to build your project.

Btw, if you are starting with Java and Maven and not familiar with fundamental concepts like PATH, CLASSPATH, JAVA_HOME etc, I suggest you to The Complete Java Masterclass to learn more those and how Java works in general, and if you want to learn Maven or just refresh the basic concepts, Maven Crash Course is a nice course to start with.




Invalid target release: 1.7 

Recently I was building a Java project using Maven on my old machine when I encountered this error. I thought I have the latest Java version which I had and that's why I surprised with this error.

Here is what my Maven pom.xml looks like:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

Here is an example output of mvn -version:

$ mvn -version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-12T04:58:10+08:00
Maven home: C:\apache-maven-3.2.3
Java version: 1.6.0_37, vendor: Sun Microsystems Inc.
Java home: C:\jdk1.6.0_37\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

You can see that Maven in my machine was using JDK 1.6.0_37 and that's why when I was building my Maven project with JDK 1.7 as the target it was failing.

Once I updated the pom.xml to use target 1.6 the build was started working.

Because Java 7 was not required for my project, I just had to change the Maven compile plugin as follows:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

But, if you have to build your Java project with JDK 1.7 then just install and update your JAVA_HOME variable and this error will go away.

This solution is really nice as you can solve the problem immediately. Btw, if you want to learn about Maven itself, I suggest you take a look at the Maven Fundamentals by Bryan Hansen course on Pluralsight, one of the better courses to start with Maven.


How to set Java Version for Maven in Windows?


Though, you would need a Pluralsight membership to get access to this course, this cost around $29 per month or $299 annually (14% discount). 

If you don't have Pluralsight membership, I suggest you get one because it allows you to access their 6000+ online courses on all latest topics like Java, Spring, Maven, ANT, Gradle, and other Java-related stuff.

You will also have access to their interactive quizzes, exercises, and latest certification material. To me, Pluarlsight is like Netflix for Software Developers and Since learning is an important part of our job, Plurlasight membership is a great way to stay ahead of your competition.

They also provide a 10-day free trial without any commitment, which is a great way to not just access this course for free but also to check the quality of courses before joining Pluralsight.


Invalid target release: 1.8

This error comes if your maven compiler plugin has <target>1.8</target> but JAVA_HOME variable in your machine is referring to a Java version which is lower than JDK 1.8 e.g. JDK 1.6 or JDK 1.7.


The Maven file initially might look like:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>


In order to solve this error either change your target to match with the Java version, your JAVA_HOME variable is referring to or install a new JDK and updated the JAVA_HOME variable.

If you don't know then see this article to find out exact steps to change the value of JAVA_HOME environment variable in Windows and Linux.

Btw, If you are working in the restricted environment like in a big company where Software is deployed automatically and can't add or edit an environment variable, you can still change them in the local shell as shown in my earlier article How to set a specific Java version for Maven in Windows.

How to fix invalid target release: 1.7, 1.8, 1.9, or 1.10 Error in Maven Build



Invalid target release: 1.9

This error will come if your maven compiler plugin has <target>1.9</target> but JAVA_HOME variable in your machine is referring to a Java version which is lower than JDK 1.9 e.g. JDK 1.8 or JDK 1.7.

As explained in the previous section either change the target into pom.xml or install a new JDK and updated the JAVA_HOME environment variable to point out new JDK bin directory.

Since Maven uses JAVA_HOME, it will not solve the problem until you update this environment variable, even if you have installed the correct version of Java.

Similarly, you will get Invalid target release: 1.10 if you compile using <target>1.10</target> and your JAVA_HOME is referring to JDK 9 or JDK 8.

Now you can generalize this problem and solve depending upon which version of Java you have specified in your pom.xml and which version of Java is installed on your machine and referred by the JAVA_HOME environment variable.


Further Learning
Maven Crash Course
Maven Fundamentals by Bryan Hansen
Java Maven: 102 The Truth About Building Java Programs


Other Maven articles you may like
  • What is the difference between Maven, ANT, and Jenkins? (answer)
  • How to increase the heap size of Maven? (steps)
  • How to fix Maven Eclipse Dependency search not working issue? (solution)
  • How to install Maven in Windows 10? (steps)
  • How to build a Java project using ANT? (article)
  • How to create or modify build.xm in ANT? (tutorial)
  • Top 5 Apache Maven Books for Free (books)
  • 10 Points about Maven Java Developer should know (maven)
  • 10 Maven Plugins Every Java Developers should know (plugins)
  • 6 Free Maven and Jenkins Courses for Java developers (courses)
  • How to set a specific Java version for Maven (tutorial)
Thanks for reading this article so far. If you manage to solve your problem by following these tips then please share with your friends and colleagues. If you are still facing issues then please drop a note and we may be able to solve your problem together.

No comments :

Post a Comment