【问题标题】:Maven build Compilation error : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project MavenMaven 构建编译错误:无法在项目 Maven 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)
【发布时间】:2017-07-20 09:23:37
【问题描述】:

我有一个 Maven 项目从 git repo 分叉并克隆到我的 eclipse 上。它是基于 Java 8 构建的。我要做的第一件事是执行

mvn clean install

但我收到以下失败消息:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Maven ---
[INFO] Deleting /Users/vshukla/git/Prism/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/vshukla/git/Prism/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 66 source files to /Users/vshukla/git/Prism/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/vshukla/git/Prism/src/main/java/PrenPost/scanUtility.java:[54,52] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/AppUtility.java:[87,86] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[ERROR] /Users/vshukla/git/Prism/src/main/java/PrenPost/ComparisionUtility.java:[58,52] diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
[INFO] 3 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.266 s
[INFO] Finished at: 2017-03-01T12:11:27+05:30
[INFO] Final Memory: 13M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven: Compilation failure: Compilation failure:
[ERROR] /Users/vshukla/git/Prism/src/main/java/PrenPost/scanUtility.java:[54,52] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/AppUtility.java:[87,86] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] /Users/vshukla/git/Prism/src/main/java/PrenPost/ComparisionUtility.java:[58,52] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] -> [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/MojoFailureException

但是,下面是明确设置了 JRE-8 的项目的构建路径详细信息:

和编译设置:

无论我构建项目多少次,我都会遇到相同的错误。即使在清理项目并从 eclipse 中刷新它也无济于事。请指导。

更新:

添加属性后设置maven编译器插件

<properties>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
</properties>

我收到以下错误:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Maven ---
[INFO] Deleting /Users/vshukla/git/Prism/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/vshukla/git/Prism/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 66 source files to /Users/vshukla/git/Prism/target/classes
[INFO] -------------------------------------------------------------
[WARNING] COMPILATION WARNING : 
[INFO] -------------------------------------------------------------
[WARNING] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java: /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java uses or overrides a deprecated API.
[WARNING] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java: Recompile with -Xlint:deprecation for details.
[WARNING] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java: Some input files use unchecked or unsafe operations.
[WARNING] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java: Recompile with -Xlint:unchecked for details.
[INFO] 4 warnings 
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java:[26,22] package com.apple.eawt does not exist
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java:[93,41] cannot find symbol
  symbol: class Application
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java:[93,67] cannot find symbol
  symbol: variable Application
[INFO] 3 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.591 s
[INFO] Finished at: 2017-03-01T13:09:47+05:30
[INFO] Final Memory: 21M/347M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven: Compilation failure: Compilation failure:
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java:[26,22] package com.apple.eawt does not exist
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java:[93,41] cannot find symbol
[ERROR] symbol: class Application
[ERROR] /Users/vshukla/git/Prism/src/main/java/main/MainUITabbed.java:[93,67] cannot find symbol
[ERROR] symbol: variable Application
[ERROR] -> [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/MojoFailureException

这里是从 MainUITabbed 类给出编译错误的代码 sn-p:

import com.apple.eawt.Application;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {


                Application application = Application.getApplication(); // line 93

                ImageIcon imgicon = new ImageIcon(getClass().getResource(
                        "ICON.jpg"));
                Image img = imgicon.getImage();


                application.setDockIconImage(img);

                MainUITabbed frame = new MainUITabbed();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    });
}

【问题讨论】:

  • 你的 pom.xml 中有没有依赖:&lt;dependency&gt; &lt;groupId&gt;com.apple&lt;/groupId&gt; &lt;artifactId&gt;AppleJavaExtensions&lt;/artifactId&gt; &lt;version&gt;1.4&lt;/version&gt; &lt;/dependency&gt;
  • 没有。让我试试。
  • 太棒了。有效。但是怎么做?为什么会出现这个错误?
  • 因为你显然在使用那个库。为了让 maven 知道将其中的类添加到类路径中,您必须相应地定义依赖项。
  • 这里的 AppleJavaExtensions 是什么?那是你的 java 类名

标签: java maven build maven-compiler-plugin


【解决方案1】:

发生错误是因为代码不适用于那里使用的默认编译器。在声明依赖关系之后,在根元素结束之前将此代码粘贴到有效的 POM 中,以更改使用的编译器。根据需要调整版本。

<dependencies>
...
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

【讨论】:

  • 另外请确保 eclipse.ini 文件具有您设置为项目 JDK 的正确 JRE 版本
  • sourcetarget 这些是 JDK 版本吗?
  • 将此添加到我的 pom 文件中对我有用...但我仍然不明白为什么。使用 java 12,我可以通过命令行对我的简单项目进行清理包。但是当我尝试通过 maven 做同样的事情时,我得到了这篇文章中描述的错误。当您说“代码不适用于那里使用的默认编译器”时,您是指maven的代码,还是项目本身的代码?我同样在 Eclipse 中将我的 java 编译器设置为版本 12,所以据我所知,它应该使用相同的 java 版本......
【解决方案2】:

我不认为 IDE 在这里是相关的。毕竟,您正在运行一个 Maven,而 Maven 没有允许编译菱形运算符的源代码。 所以,我认为你应该配置 maven-compiler-plugin 本身。

您可以阅读有关此here 的信息。但一般尝试添加以下属性:

<properties>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
</properties>

看看它现在是否只在 Maven 中编译。

【讨论】:

  • 这非常有效。无需添加任何其他内容来修复错误。
【解决方案3】:

这是一个令人恼火的错误,偶尔会出现,记下一些有帮助的步骤:

从 Eclipse 的角度写答案作为基本逻辑仍然是 无论是 Intellij 还是命令行都一样

  1. Rt 点击你的项目 -> Maven -> 更新项目 -> 选择强制 更新->点击确定
  2. 在属性标签下,添加:
<maven.compiler.source>1.8</maven.compiler.source> 
<maven.compiler.target>1.8</maven.compiler.target>
  1. 在某些情况下,当我们尝试强制更新时,您会开始看到错误: 无法从 Y 路径转移 X 依赖,分辨率不会 再试一次,bla bla bla

    在这种情况下,通过 cd 快速修复它到 .m2/repository 文件夹并运行以下命令:

for /r %i in (*.lastUpdated) do del %i

【讨论】:

    【解决方案4】:

    如果你本地的jdk版本是11或者9、10,而你的项目的java源/目标版本是1.8,并且你正在使用org.projectlombok:lombok包,那么你可以尝试更新它的版本到1.16.22或者1.18。 12,像这样:

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.22</version>
            </dependency>
    

    它刚刚解决了我的问题。

    【讨论】:

    • 也解决了我的问题。谢谢
    【解决方案5】:

    您应该将代码添加到 pom.xml 中,例如:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    

    【讨论】:

    • 为我解决了。但我不知道为什么。
    【解决方案6】:

    在我的情况下,这是由于与 OpenJDK 9 不兼容(我没有调查过)造成的。

    如果您不需要 JDK 9,临时解决方法是将其从您的计算机中清除:

    sudo apt-get remove --purge openjdk-9-jdk openjdk-9-jre 
    sudo apt-get remove --purge openjdk-9-jdk-headless openjdk-9-jre-headless
    

    【讨论】:

    • 确实是这个问题。谢谢
    【解决方案7】:

    确保 java home 路径正确。就我而言,pom 文件中的 java 主路径错误。

      <properties>
            <java.home>/usr/java/jdk1.8.0_45/bin/javac</java.home>
      </properties>
    
    
    
    
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.5.1</version>
                    <configuration>
                          <verbose>true</verbose>
                          <fork>true</fork>
                          <executable>${java.home}</executable>
                       <compilerVersion>1.8</compilerVersion>
                       <source>1.8</source>
                       <target>1.8</target>
                     </configuration>
                 </plugin>
    

    【讨论】:

      【解决方案8】:

      朋友,
      如果每一个都是最新的,即使你遇到了这个问题,那么
      尝试直接从终端运行此命令,而不是从 eclipse 运行。
      $ mvn clean install

      并确保以下几点:

      • maven 在系统路径中
      • 所有 maven 依赖项都可以在 `.m2/repository` 获得
      • java 在系统路径中


      【讨论】:

        【解决方案9】:

        如果您能够从终端编译 mvn compile 项目成功,但不能从 Eclipse 检查 Window > Preferences >Installed JREs,请确保您选择了 JDK 下的 JRE(检查 2 个不同的路径JRE在图片中),因为Maven需要JDK来编译你需要添加它。

        【讨论】:

          【解决方案10】:

          我有同样的问题,我改变了这个

          <configuration>
              <source>1.7</source>
              <target>1.7</target>
           </configuration>
          

          这里 1.7 是我的 JDK 版本。它已经解决了。

          【讨论】:

            【解决方案11】:

            因为 java 版本不匹配,所以我已经更正了它,我能够构建 war 文件。希望它对某人有所帮助

                <maven.compiler.source>1.7</maven.compiler.source>
                <maven.compiler.target>1.7</maven.compiler.target>
            

            【讨论】:

              【解决方案12】:

              在 pom.xml 文件中执行此操作,更新项目后,问题就消失了。

              <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <version>2.4</version>
                          <configuration>
                              <source>1.8</source>
                              <target>1.8</target>
              </configuration>
              </plugin>
              
              1. 更新&lt;plugin&gt;&lt;/plugin&gt;下的pom.xml。

              2. 更新您的项目。

              3. 然后清理 maven 并安装 maven。

              【讨论】:

                【解决方案13】:

                转到存储 POM 的文件位置并打开 cmd. 然后键入“mvn --v”以检查提供的 maven 版本和 java 运行时。 检查运行时属性,如果它是“C:\Program Files\Java\jre1.8.0_191”或什至接近 JRE,请转到环境变量并添加一个名为“JAVA_HOME”的新“系统变量”,其值为“C: \Program Files\Java\jdk1.8.0_191"。

                重新打开 cmd,然后“全新安装”项目。

                【讨论】:

                  【解决方案14】:

                  我无法根据此处的其他建议运行自己的构建。我什至尝试了不同版本的 maven-compiler-plugin:3.1、3.7.0 等。

                  我让它工作了添加这个:

                  <testSourceDirectory>/src/test/java</testSourceDirectory>
                  

                  我尝试了这种方法,因为 /src/test/java 目录似乎被认为是一个 java 类,这就是它与 /src/test/java 同时编译的原因。所以我的预感是对的。

                  也许对其他人也是如此,所以试试这个吧。

                  【讨论】:

                  • 它帮助我在 Windows 10 上成功构建了 swagger 插件
                  【解决方案15】:

                  我也遇到了同样的问题,解决方法是在环境变量中设置JAVA_HOME。

                  【讨论】:

                    【解决方案16】:

                    通过在 pom.xml 中添加这个来工作:

                    <properties>
                            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                            <maven.compiler.source>1.8</maven.compiler.source>
                            <maven.compiler.target>1.8</maven.compiler.target>
                    </properties>
                    

                    即使您拥有 SDK 13 或 14。

                    【讨论】:

                      【解决方案17】:

                      &lt;maven.compiler.release&gt;(不是source & target

                      其他几个答案显示&lt;maven.compiler.source&gt;&lt;maven.compiler.target&gt;。这两个现在都被更简单的单个元素所取代:&lt;maven.compiler.release&gt;

                      <maven.compiler.release>15</maven.compiler.release>
                      

                      所以这个:

                        <!--old-school-->
                        <properties>
                          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                          <maven.compiler.source>15</maven.compiler.source>
                          <maven.compiler.target>15</maven.compiler.target>
                        </properties>
                      

                      …变成:

                        <!--modern-->
                        <properties>
                          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                          <maven.compiler.release>15</maven.compiler.release>
                        </properties>
                      

                      请参阅问题,“maven.compiler.release” as an replacement for source and target?

                      【讨论】:

                        【解决方案18】:

                        转到:文件 -> 项目结构 -> 项目 -> 将项目 SDK 和项目语言级别切换到 Java 版本 1.8

                        示例如下:

                        【讨论】:

                          【解决方案19】:

                          以下 pom.xml 配置使构建成功,并确保项目构建路径 JRE 系统库应指向 Java8。

                          org.apache.maven.pluginsmaven-compiler-plugin3.7.0 1.81.8

                          【讨论】:

                          • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 【如何写出好的答案? ](stackoverflow.com/help/how-to-answer)
                          【解决方案20】:

                          JDK 9 和 10 解决方案

                          <plugins>
                                  <plugin>
                                      <groupId>org.apache.maven.plugins</groupId>
                                      <artifactId>maven-compiler-plugin</artifactId>
                                      <version>${maven-compiler.version}</version>
                                      <configuration>
                                          <source>${java.version}</source>
                                          <target>${java.version}</target>
                                          <debug>true</debug>
                                      </configuration>
                                      <dependencies>
                                          <dependency>
                                              <groupId>org.ow2.asm</groupId>
                                              <artifactId>asm</artifactId>
                                              <version>6.2</version>
                                          </dependency>
                                      </dependencies>
                                  </plugin>
                          

                          并确保您的 maven 指向 JDK 10 或 9。 mvn -v

                          Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T14:49:05-05:00)
                          Maven home: C:\devplay\apache-maven-3.5.3\bin\..
                          Java version: 10.0.1, vendor: Oracle Corporation
                          Java home: C:\Program Files\Java\jdk-10.0.1
                          Default locale: en_US, platform encoding: Cp1252
                          OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
                          

                          【讨论】:

                            【解决方案21】:

                            在您的 pom.xml 中添加以下内容并按 CTRL+S,它将自动构建项目。

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

                            现在点击 Run as >Maven 测试。

                            这个错误肯定会解决的。

                            【讨论】:

                            • org.apache.maven.pluginsmaven-compiler-plugin3.5.11.81.8
                            【解决方案22】:

                            我的问题是我从子项目目录而不是父项目运行mvn compile

                            【讨论】:

                              【解决方案23】:

                              对我来说,这似乎是这个页面上的所有答案,加上正确的 JDK。我添加了属性:

                                  <maven.compiler.source>1.8</maven.compiler.source>
                                  <maven.compiler.target>1.8</maven.compiler.target>
                              

                              以及从 Redhat 指向我打开的 JDK 的 JDK 1.8 插件:

                                  <plugin>
                                      <groupId>org.apache.maven.plugins</groupId>
                                      <artifactId>maven-compiler-plugin</artifactId>
                                      <configuration>
                                          <fork>true</fork>
                                          <executable>C:\java\java-1.8.0-openjdk\bin\javac.exe</executable>
                                          <source>1.8</source>
                                          <target>1.8</target>
                                      </configuration>
                                  </plugin>
                              

                              跑了

                              mvn clean verify
                              

                              并获得构建成功。耶。

                              编辑: 我认为这实际上是造成差异的原因:https://mkyong.com/eclipse/eclipse-ide-no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-rather-than-a-jdk/

                              【讨论】:

                                【解决方案24】:

                                在我的情况下,我正确设置了 JAVA_HOME,但几个小时仍然有问题! 最后我发现我在 .m2 目录中有 settings.xml 并且 java home 设置在那里并指向另一个路径。我运行了以下命令来找出 maven 正在使用的 java 路径:

                                mvn clean install -X | grep -i "executable"
                                

                                然后我更正了.m2目录下settings.xml中的java home路径问题解决了!

                                【讨论】:

                                  【解决方案25】:

                                  注意:如果您的项目在 File->Project Structure 下为一个特定版本的 Java 设置,并且您的 Pom.xml 为不同版本的 Java 设置,您将收到此错误。例如,在项目结构下运行 Java 8,在 pom.xml 中运行 Java 11。

                                  【讨论】:

                                    猜你喜欢
                                    • 2017-06-23
                                    • 1970-01-01
                                    • 2018-11-13
                                    • 2022-01-04
                                    • 2018-03-04
                                    • 2016-08-06
                                    • 1970-01-01
                                    • 2017-02-18
                                    • 2020-07-29
                                    相关资源
                                    最近更新 更多