Unable to compile a simple Java 10 / Java 11 project with Maven

I have a trivial Maven project:

src └── main └── java └── module-info.java pom.xml 

pom.xml:

 <groupId>org.example</groupId> <artifactId>example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>example</name> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <release>10</release> </configuration> </plugin> </plugins> </build> 

When I create a project via mvn -X install -DskipTests=true , it fails:

 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project example: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile failed. at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345) at org.apache.maven.cli.MavenCli.main(MavenCli.java:191) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile failed. at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208) ... 20 more Caused by: java.lang.IllegalArgumentException at org.objectweb.asm.ClassReader.<init>(Unknown Source) at org.objectweb.asm.ClassReader.<init>(Unknown Source) at org.objectweb.asm.ClassReader.<init>(Unknown Source) at org.codehaus.plexus.languages.java.jpms.AsmModuleInfoParser.parse(AsmModuleInfoParser.java:80) at org.codehaus.plexus.languages.java.jpms.AsmModuleInfoParser.getModuleDescriptor(AsmModuleInfoParser.java:54) at org.codehaus.plexus.languages.java.jpms.LocationManager.resolvePaths(LocationManager.java:83) at org.apache.maven.plugin.compiler.TestCompilerMojo.preparePaths(TestCompilerMojo.java:281) at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:762) at org.apache.maven.plugin.compiler.TestCompilerMojo.execute(TestCompilerMojo.java:176) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) ... 21 more 

Is there any way to fix this?

+110
source share
6 answers

UPDATE

The answer is now out of date. See this answer .


maven-compiler-plugin depends on the old version of ASM, which does not yet support Java 10 (and Java 11). However, you can explicitly specify the correct 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 version at https://search.maven.org/search?q=g:org.ow2.asm%20AND%20a:asm&core=gav

+128
source

Alternatively, as of July 30, 2018, to fix the above problem, you can configure the Java version used in Maven to any version up to JDK / 11 and use maven-compiler-plugin:3.8.0 to indicate the release is 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 : - In this version, the default value for the source / target has been increased from 1.5 to 1.6. - release notes.


Edit [12/30/2018]

In fact, you can use the same version of maven-compiler-plugin when compiling code for JDK / 12.

More details and configuration example on how to compile JDK12 preview function with Maven .

+124
source

Upgrading your maven-compiler-plugin to 3.8.0 seems necessary, but not enough. If you still have problems, you should also make sure that the JAVA_HOME environment variable is set to Java 10 (or 11) if you are running from the command line. (The error message you get will not tell you this.) Or, if you are working with an IDE, you need to make sure that it is configured to run maven with your current JDK.

+21
source

This may not be the same mistake, but I had a similar one.

Check Maven Java Version

Since Maven also works with Java, first check which version your Maven is running on:

 mvn --version | grep -i java 

Returns:

Java version 1.8.0_151, vendor: Oracle Corporation, runtime: C: \ tools \ jdk \ openjdk1.8

Incompatible version

Here above, my maven runs with Java Version 1.8.0_151 . So even if I tell 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> 

This will logically output this error:

[ERROR] Failed to fulfill the target org.apache.maven.plugins: Maven-compiler-plugin: 3.8.0: compile (default-compile) for the project efa-example-commons-task: Fatal error compilation: invalid target release: 11 → [Help1]

How to install a specific version of Java for Maven

The logical thing to do is install a higher version of Java in Maven (e.g. Java version 11 instead of 1.8).

Maven uses the JAVA_HOME environment variable to find the version of Java to run. Therefore, change this variable to the JDK with which you want to compile (for example, OpenJDK 11).

Health Check

Then run mvn --version again to make sure the configuration has been taken care of:

 mvn --version | grep -i java 

gives

Java version: 11.0.2, vendor: Oracle Corporation, runtime: C: \ tools \ jdk \ openjdk11

Which is much better and more correct for compiling code written with Java 11 specifications.

+6
source

Specify maven.compiler.source and target versions.

1) Maven version that supports the JDK that you are using. 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> 

Alternatively, you can completely specify the maven compiler plugin. See previous answers. In short, 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 compilation errors in your IDE.

4) If it still does not work. In Intellij Idea, I prefer to use a terminal instead of a terminal from the OS. Then in Idea go to file -> settings -> build tools -> maven. I work with Maven, which I downloaded from Apache (Idea uses batch Maven by default). Then restart Idea and run mvn clean install again. Also make sure that you have the correct environment variables Path , MAVEN_HOME , JAVA_HOME .

I also saw this one line, but it does not work.

 <maven.compiler.release>11</maven.compiler.release> 
+2
source

Okay, so nothing worked for me.
I used spring boot with hibernation. The spring boot version was ~ 2.0.1, and I kept getting this error and null pointer exception when compiling. The problem was with Hibernate, which needed a upgrade. But after that, I had some other problems that did not seem to handle the processor annotations, so I decided to just raise the spring from version 2.0.1 to version 2.1.7, and everything worked as expected.

You still need to add the above plugin hard
Hope this helps!

0
source

Source: https://habr.com/ru/post/1276031/


All Articles