【问题标题】:Intellij maven project Fatal error compiling: invalid flag: --releaseIntellij maven项目致命错误编译:无效标志:--release
【发布时间】:2018-08-12 20:11:47
【问题描述】:

我正在尝试从 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>

【问题讨论】:

    标签: java maven spring-boot intellij-idea maven-compiler-plugin


    【解决方案1】:

    在您的pom.xml 文件中,只需从您的maven-compiler-plugin 配置中删除标签&lt;release&gt;8&lt;/release&gt;:

    <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 9 向后兼容性添加了“发布”maven.apache.org/plugins/maven-compiler-plugin/…。虽然不能很好地处理前向兼容性。
    • 如果安装了多个 java 版本:验证您正在使用 java 9+ 版本运行 maven:mvn --version。还要确保您的 IDE 使用正确的 JDK 版本。
    • 对 ... env 的最低要求。使用“--release”选项的设置是:Maven:3.6.0; JDK:9+
    • @radistao 谢谢,我有最新的 maven,但 $JAVA_HOME 中的 java 8 添加到系统路径。我把它挂到 11,现在“发布”工作。
    • 仅供参考,一旦更改了JAVA_HOME必须重新启动他们的intellij。
    【解决方案2】:

    这与您系统上运行的 JDK 版本有关。根据 maven-compiler-plugin 的文档,从 1.9 开始支持发布标志,因此如果您使用的是 JDK 的早期(1.8 及以下)版本,则必须从 POM 中注释/删除。

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

    【讨论】:

    • 我遇到了这个问题,我忘记将 JAVA_HOME 环境变量更改为指向我的 java 11 JDK。 (export JAVA_HOME=$(/usr/libexec/java_home -v11))
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      运行:mvn -v

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

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

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

      解压下载到您的主路径,例如~/maven。

      将~/maven/bin 添加/编辑到您的路径

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

      运行mvn -v

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

      【讨论】:

        【解决方案5】:

        另一种方法是直接在属性标签内设置编译器标志,而不是使用编译器插件的&lt;configuration&gt; 标签:

        <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

        【讨论】:

          【解决方案6】:

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

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

          More details here

          【讨论】:

            【解决方案7】:

            此配置对我有用,适用于 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>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-08-09
              • 2015-06-14
              • 2017-10-29
              • 2015-10-14
              • 2018-02-17
              • 2019-10-30
              • 1970-01-01
              相关资源
              最近更新 更多