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

时间:2018-03-05 08:09:14

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

我正在尝试使用Spring-boot,Maven in Intellij 请帮帮我,我收到错误:

[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>

6 个答案:

答案 0 :(得分:19)

在您的pom.xml文件中,只需从<release>8</release>配置中删除标记maven-compiler-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>
        <verbose>true</verbose>
    </configuration>
</plugin>

答案 1 :(得分:5)

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

在Maven文档中查看有关maven-compiler-plugin here的发行版标签

答案 2 :(得分:1)

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

运行:mvn -v

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

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

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

将下载的内容提取到您的家庭路径中,例如~/maven

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

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

运行mvn -v

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

对于Java 11而言,此配置适用于我。(IntelliJ版本2019.1) java.version为11,在pom.xml的属性部分中声明。

<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>

答案 5 :(得分:0)

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