无法使用 Maven 编译简单的 Java 10 / Java 11 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49398894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to compile simple Java 10 / Java 11 project with Maven
提问by ZhekaKozlov
I have a trivial Maven project:
我有一个微不足道的 Maven 项目:
src
└── main
└── java
└── module-info.java
pom.xml
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 build the project via mvn -X install -DskipTests=true
, it fails:
当我通过 构建项目时mvn -X install -DskipTests=true
,它失败了:
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 a way to fix this?
有没有办法来解决这个问题?
采纳答案by ZhekaKozlov
UPDATE
更新
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:
maven-compiler-plugin
依赖于不支持 Java 10(和 Java 11)的旧版 ASM。但是,可以明确指定正确的 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
你可以在https://search.maven.org/search?q=g:org.ow2.asm%20AND%20a:asm&core=gav找到最新的
回答by Naman
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.
截至 2018 年 7 月 30 日,为了解决上述问题,可以将 maven 中使用的 java 版本配置为任何高达 JDK/11 的版本,并使用maven-compiler-plugin:3.8.0
指定 9、10、11 的版本,而无需任何显式依赖项。
<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.
注意:- 在这个版本中,源/目标的默认值已从 1.5 提升到 1.6。——发行说明。
Edit [30.12.2018]
编辑 [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.
事实上,您也可以maven-compiler-plugin
在针对 JDK/12 编译代码时使用相同的版本。
More details and a sample configuration in how to Compile and execute a JDK preview feature with Maven.
有关如何使用 Maven 编译和执行 JDK 预览功能的更多详细信息和示例配置。
回答by MiguelMunoz
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.
将您的 maven-compiler-plugin 提升到 3.8.0 似乎是必要的,但还不够。如果您仍然遇到问题,如果您从命令行运行,您还应该确保您的 JAVA_HOME 环境变量设置为 Java 10(或 11)。(您得到的错误消息不会告诉您这一点。)或者,如果您从 IDE 运行,您需要确保它设置为使用您当前的 JDK 运行 maven。
回答by Yan Khonski
Specify maven.compiler.source and target versions.
指定 maven.compiler.source 和目标版本。
1) Maven version which supports jdk you use. In my case JDK 11 and maven 3.6.0.
1) Maven 版本,支持您使用的 jdk。就我而言,JDK 11 和 maven 3.6.0。
2) pom.xml
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 :)
作为替代方案,您可以完全指定 maven 编译器插件。请参阅以前的答案。在我的例子中它更短:)
<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.
3) 重建项目以避免在您的 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_HOMEenvironment variables.
4)如果还是不行。在 Intellij Idea 中,我更喜欢使用终端而不是使用操作系统的终端。然后在 Idea 中转到文件 -> 设置 -> 构建工具 -> maven。我使用从 apache 下载的 maven(默认情况下,Idea 使用捆绑的 maven)。然后重新启动 Idea 并mvn clean install
再次运行。还要确保你有正确的Path、MAVEN_HOME、JAVA_HOME环境变量。
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:
我制作了一些快速入门项目,我在其他项目中重复使用了这些项目,请随时检查:
回答by KeyMaker00
It might not exactly be the same error, but I had a similar one.
这可能不是完全相同的错误,但我有一个类似的错误。
Check Maven Java Version
检查 Maven Java 版本
Since Maven is also runnig with Java, check first with which version your Maven is running on:
由于 Maven 也使用 Java 运行,因此首先检查您的 Maven 正在运行的版本:
mvn --version | grep -i java
It returns:
它返回:
Java version 1.8.0_151, vendor: Oracle Corporation, runtime: C:\tools\jdk\openjdk1.8
Java 版本 1.8.0_151,供应商:Oracle Corporation,运行时: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
:
上面我的 Maven 正在运行Java Version 1.8.0_151
. 所以即使我指定 maven 编译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]
[错误] 无法在项目 efa-example-commons-task 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile): Fatal error compiling: invalid target release: 11 -> [帮助 1]
How to set specific java version to Maven
如何将特定的 Java 版本设置为 Maven
The logical thing to do is to set a higher Java Version to Maven (e.g. Java version 11 instead 1.8).
合乎逻辑的做法是将更高的 Java 版本设置为 Maven(例如 Java 版本 11 而不是 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).
Maven 使用环境变量JAVA_HOME
来查找要运行的 Java 版本。因此,将此变量更改为您要针对其编译的 JDK(例如 OpenJDK 11)。
Sanity check
完整性检查
Then run again mvn --version
to make sure the configuration has been taken care of:
然后再次运行mvn --version
以确保配置已得到处理:
mvn --version | grep -i java
yields
产量
Java version: 11.0.2, vendor: Oracle Corporation, runtime: C:\tools\jdk\openjdk11
Java 版本:11.0.2,供应商:Oracle Corporation,运行时:C:\tools\jdk\openjdk11
Which is much better and correct to compile code written with the Java 11 specifications.
编译使用 Java 11 规范编写的代码更好和正确。
回答by Tudor
Alright so for me nothing worked.
I was using spring boot withhibernate. The spring boot version was ~2.0.1 and I would keep get this error and null pointer exception upon compilation. The issue was with hibernate that needed a version bump. But after that I had some other issues that seemed like the annotation processor was not recognised so I decided to just bump spring from 2.0.1 to 2.1.7-release and everything worked as expected.
好吧,对我来说没有任何效果。
我正在使用带有休眠功能的弹簧靴。spring boot版本是~2.0.1,编译时我会不断收到这个错误和空指针异常。问题在于需要版本碰撞的休眠。但在那之后,我遇到了一些其他问题,似乎无法识别注释处理器,所以我决定将 spring 从 2.0.1 升级到 2.1.7 版本,一切都按预期进行。
You still need to add the above plugin tough
Hope it helps!
你仍然需要添加上面的插件tough
希望它有帮助!
回答by Abhishek
If you are using spring boot then add these tags in pom.xml.
如果你使用的是 spring boot,那么在 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.
您也可以在<maven.compiler.release>
标签中将 java 版本更改为 11 或 13 。
Just add below tags in pom.xml
只需在 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.
您也可以将 11 更改为 10、13 以更改 Java 版本。我正在使用最新的 java 13。这个对我有用。