Intellij maven项目编译致命错误:无效标志:--release

126

我正试图在Intellij中使用Spring-boot和Maven,但遇到了错误,请帮帮我:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project spring-rest: Fatal error compiling: invalid flag: --release -> [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
Process finished with exit code 1

我的pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?> <project
   xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <groupId>com.example</groupId>
       <artifactId>spring-rest</artifactId>
       <version>3.1.3.RELEASE</version>
       <packaging>war</packaging>

       <name>spring-rest</name>
       <description>Demo project for Spring Boot</description>

       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.0.0.RELEASE</version>
           <relativePath/> <!-- lookup parent from repository -->
       </parent>

       <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
           <java.version>1.8</java.version>
       </properties>

       <dependencies>

           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-security</artifactId>
               <version>2.0.0.RELEASE</version>
           </dependency>

         <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
         </dependency>

           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-data-jpa</artifactId>
           </dependency>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>

           <dependency>
               <groupId>org.hibernate.javax.persistence</groupId>
               <artifactId>hibernate-jpa-2.1-api</artifactId>
               <version>RELEASE</version>
           </dependency>


           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <scope>runtime</scope>
           </dependency>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-test</artifactId>
               <scope>test</scope>
           </dependency>
       </dependencies>

       <build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>

               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.7.0</version>
                   <configuration>
                       <source>1.8</source>
                       <target>1.8</target>
                       <release>8</release>
                       <verbose>true</verbose>
                   </configuration>
               </plugin>
           </plugins>
       </build>

   </project>
12个回答

122

在你的pom.xml文件中,只需从maven-compiler-plugin配置中删除标签<release>8</release>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <verbose>true</verbose>
    </configuration>
</plugin>

14
对我有用-谢谢-只是为了补充一个原因-看起来"release"是为了Java 9的向后兼容而添加的,但它并不完全处理向前兼容性。注:该内容是关于某个 Maven 编译插件的讨论。 - Dazed
15
如果您安装了多个Java版本,请验证您正在使用Java 9+版本运行Maven:mvn --version。还要确保您的IDE使用适当的JDK版本。 - radistao
3
正确的...使用“--release”选项的环境设置的最低要求是:Maven:3.6.0;JDK:9+。 - garykwwong
1
@radistao 谢谢,我已经安装了最新的Maven,但是$JAVA_HOME中的Java 8被添加到系统路径中。我将其更改为11,现在“release”可以正常工作了。 - luke
3
仅供参考,一旦更改了JAVA_HOME,必须重新启动IntelliJ。 - gaurav kumar
请先导航到@Krisna Vedula的答案。这个答案修复了JDK8及以下版本的问题,但并不知道正确的原因。可能你的pom.xml完全正确,只是使用了错误的JAVA_HOME。 - Martin Mucha

42

这与您系统上运行的JDK版本有关。根据maven-compiler-plugin文档,自1.9版本以来支持release标志,因此如果您使用较早(1.8及以下)的JDK版本,则必须将其注释或从POM中删除。

查看Maven文档中maven-compiler-plugin的release标签 在此处


5
我遇到了这个问题,我忘记将JAVA_HOME环境变量更改为指向我的Java 11 JDK。(export JAVA_HOME=$(/usr/libexec/java_home -v11)) - Andrew Rueckert

18

我也遇到了这个问题,请检查您的JAVA_HOME环境变量。 因为我使用的是JAVA 11,所以我将版本设置为11。 我的系统JAVA_HOME被设置为java 8,添加了JAVA_HOME用户环境变量并将其设置为java 11路径后,问题得到解决。


14

打开终端以检查Maven是否使用了正确的Java版本

运行:mvn -v

如果您看到旧版Java,则可能会出现问题。

在Linux上,建议删除Maven,例如使用Fedora包管理器dnf remove maven或检查您的PATH是否有Maven。

从此处下载一个:https://maven.apache.org/download.cgi

将下载的文件提取到您的主目录中,例如:~/maven

~/maven/bin添加/编辑到您的PATH中

重新应用.bash_profile(或重新登录)

运行mvn -v

现在您应该在输出中看到正确的Java版本


在我的情况下,我同时使用jenv和sdkman来管理Java版本。jenv的Java版本已正确设置为jre 17,但sdkman仍然使用jre8。一旦我通过sdkman切换到jre17,"无效的发布"错误就消失了。 - user674669

4

另一种方法是直接在属性标签中设置编译器标志,而不是使用编译器插件的<configuration>标签:

<properties>
       <maven.compiler.source>8</maven.compiler.source>
       <maven.compiler.target>8</maven.compiler.target>
 <!--  Use the release flag only if you are using Java 9+  -->
 <!--  <maven.compiler.release>8</maven.compiler.release>  -->
       <!-- verbose is useful for debugging purposes -->
       <maven.compiler.verbose>true</maven.compiler.verbose> 
</properties>

如官方页面所述:
请参考 https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

4

我在使用Java v8时遇到了这个错误,后来通过升级到Java v11、更新JAVA_HOME以及更新我的IntelliJ SDK设置来解决了问题。


2
这主要是由于Java版本不匹配。为项目管理多个Java版本可能很麻烦。
我正在同时处理需要不同Java版本的项目,并使用jenv来切换Java版本。 更多详细信息在此处

2
在cmd中使用mvn -v命令查找Java版本。
在您的情况下,您已经在pom.xml中声明了Java 8,在环境变量的JAVA_HOME中使用相同的版本,这将解决问题。

0
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:testCompile (default-testCompile) on project selenium-for-beginners: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:testCompile failed: multiple points -> [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/PluginExecutionException

尝试进行 Selenium 初学者项目时,如果遇到这个问题,请尝试更改 pom.xml 文件中的 Maven 插件。使用“Compile Using the --release javac option(JDK 9+)”插件。这应该可以解决问题。

0

在环境属性中设置$MAVEN_HOME和JAVA_HOME

  1. 检查Maven版本(C:> mvn-version)

  2. 检查Java版本 (C:> java-version)

在pom.xml文件中添加以下代码片段并更改Maven的版本号

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version> // replace the maven version number 
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <verbose>true</verbose>
    </configuration>
</plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接