Chances are that it just runs fine with Java 11. Hint: You can speed up multi-module Maven projects by using parallel builds, e.g. mvn -T 4 compile compiles all modules in parallel on 4 CPU cores.
1 for LTS) requires Java 6 thus Maven jobs must be launched with a JDK >= 6. Jenkins >= 1.612 (1.625. 1 for LTS) requires Java 7 thus Maven jobs must be launched with a JDK >= 7.
Before moving on, we can check the default JDK version of Maven. Running the mvn -v command will show the Java version in which Maven runs.
As of 30Jul, 2018 to fix the above issue, one can configure the java version used within maven to any up to JDK/11 and make use of the maven-compiler-plugin:3.8.0
to specify a release of either 9,10,11 without any explicit dependencies.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!--or <release>10</release>-->
</configuration>
</plugin>
Note:- The default value for source/target has been lifted from 1.5 to 1.6 with this version. -- release notes.
Edit [30.12.2018]
In fact, you can make use of the same version of maven-compiler-plugin
while compiling the code against JDK/12 as well.
More details and a sample configuration in how to Compile and execute a JDK preview feature with Maven.
The answer is now obsolete. See this answer.
maven-compiler-plugin
depends on the old version of ASM which does not support Java 10 (and Java 11) yet. However, it is possible to explicitly specify the right version of ASM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.2</version> <!-- Use newer version of ASM -->
</dependency>
</dependencies>
</plugin>
You can find the latest at https://search.maven.org/search?q=g:org.ow2.asm%20AND%20a:asm&core=gav
It might not exactly be the same error, but I had a similar one.
Check Maven Java Version
Since Maven is also runnig with Java, check first with which version your Maven is running on:
mvn --version | grep -i java
It returns:
Java version 1.8.0_151, vendor: Oracle Corporation, runtime: C:\tools\jdk\openjdk1.8
Incompatible version
Here above my maven is running with Java Version 1.8.0_151
.
So even if I specify maven to compile with Java 11
:
<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
It will logically print out this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project efa-example-commons-task: Fatal error compiling: invalid target release: 11 -> [Help 1]
How to set specific java version to Maven
The logical thing to do is to set a higher Java Version to Maven (e.g. Java version 11 instead 1.8).
Maven make use of the environment variable JAVA_HOME
to find the Java Version to run. So change this variable to the JDK you want to compile against (e.g. OpenJDK 11).
Sanity check
Then run again mvn --version
to make sure the configuration has been taken care of:
mvn --version | grep -i java
yields
Java version: 11.0.2, vendor: Oracle Corporation, runtime: C:\tools\jdk\openjdk11
Which is much better and correct to compile code written with the Java 11 specifications.
Specify maven.compiler.source and target versions.
1) Maven version which supports jdk you use. In my case JDK 11 and maven 3.6.0.
2) pom.xml
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
As an alternative, you can fully specify maven compiler plugin. See previous answers. It is shorter in my example :)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
3) rebuild the project to avoid compile errors in your IDE.
4) If it still does not work. In Intellij Idea I prefer using terminal instead of using terminal from OS. Then in Idea go to file -> settings -> build tools -> maven. I work with maven I downloaded from apache (by default Idea uses bundled maven). Restart Idea then and run mvn clean install
again. Also make sure you have correct Path, MAVEN_HOME, JAVA_HOME environment variables.
I also saw this one-liner, but it does not work.
<maven.compiler.release>11</maven.compiler.release>
I made some quick starter projects, which I re-use in other my projects, feel free to check:
Boosting your maven-compiler-plugin
to 3.8.0
seems to be necessary but not sufficient. If you're still having problems, you should also make sure your JAVA_HOME
environment variable is set to Java 10 (or 11) if you're running from the command line. (The error message you get won't tell you this.) Or if you're running from an IDE, you need to make sure it is set to run maven with your current JDK.
If you are using spring boot then add these tags in pom.xml.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
and
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
`<maven.compiler.release>`10</maven.compiler.release>
</properties>
You can change java version to 11 or 13 as well in <maven.compiler.release>
tag.
Just add below tags in pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.release>11</maven.compiler.release>
</properties>
You can change the 11 to 10, 13 as well to change java version. I am using java 13 which is latest. It works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With