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

Intellij maven project Fatal error compiling: invalid flag: --release

我正在尝试从 Spring-boot,Intellij 中的 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>

在您的 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>

此配置对我有用,适用于 java 11。(IntelliJ 版本 2019.1) java.version是11,声明在pom.xml的属性部分。 Maven 版本应为 3.6 或更高版本。

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <fork>false</fork>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>UTF-8</encoding>
                <compilerArgs>
                    <arg>-parameters</arg>
                </compilerArgs>
                <release>${java.version}</release>
                <forceJavacCompilerUse>true</forceJavacCompilerUse>
            </configuration>
            <executions>
                <!-- Replacing default-compile as it is treated specially by maven -->
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <!-- Replacing default-testCompile as it is treated specially by maven -->
                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>java-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>java-test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
    </plugin>

这与您系统上 JDK 运行 的版本有关。根据 maven-compiler-plugin 的文档,从 1.9 开始支持发布标志,因此如果您使用 JDK 的先前(1.8 及以下)版本,则必须来自 POM commented/removed。

查看 Maven 文档以获取 maven-compiler-plugin 的发布标签here

打开终端检查 Maven 使用的 Java

版本是否正确

运行: mvn -v

如果您看到旧的 Java 版本,那可能就是问题所在。

在 Linux,我建议删除 Maven,例如使用 Fedora 包管理器 dnf remove maven 或检查你的 PATH for maven。

从这里下载一个:https://maven.apache.org/download.cgi

提取下载到您的主路径,例如~/maven

Add/Edit ~/maven/bin 到你的 PATH

重新申请。bash_profile(或重新登录)

运行 mvn -v

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

另一种方法是直接在属性标签内设置编译器标志,而不是使用编译器插件的 <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

我也遇到过这个问题,检查你的JAVA_HOME环境变量。 当我使用 JAVA 11 时,我将 release 的值设置为 11。 我的系统 JAVA_HOME 设置为 java 8,在将 JAVA_HOME 用户环境变量添加到 java 11 路径后,问题得到解决。

这主要是因为 java 版本不匹配。为项目管理多个 java 版本可能会很痛苦。

我正在同时处理需要不同版本 java 的项目,并且一直在使用 jenv 切换 java 版本。

More details here