Maven构建编译错误:无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.1:在项目Maven上编译(default-compile)

时间:2017-03-01 06:56:18

标签: java maven build maven-compiler-plugin

我有一个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设置: build path

和编译设置:

Compliance level

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

更新

添加属性以设置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类中提供编译错误的代码片段:

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();

            }
        }
    });
}

22 个答案:

答案 0 :(得分:33)

将此添加到POM为我修复了它。请根据需要调整版本

...
</dependencies>
<build>
    <plugins>
        <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>
    </plugins>
</build>

答案 1 :(得分:9)

我不认为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中编译。

答案 2 :(得分:2)

在我的情况下,它是由与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

答案 3 :(得分:2)

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

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

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

答案 4 :(得分:2)

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

并确保这些事情:

  • maven在系统路径
  • 所有maven依赖项均可在`.m2 / repository`
  • 中使用
  • java在系统路径


答案 5 :(得分:2)

<maven.compiler.release>(不是 sourcetarget

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

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

答案 6 :(得分:1)

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

示例如下:

enter image description here

答案 7 :(得分:1)

通过在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。

答案 8 :(得分:1)

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

答案 9 :(得分:1)

如果您的本地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>

它解决了我的问题。

答案 10 :(得分:1)

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

我通过添加以下内容使其起作用:

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

我尝试了这种方法,因为似乎/ src / test / java目录被认为是Java类,这就是为什么它与/ src / test / java同时编译的原因。所以我的直觉对我来说是正确的。

也许对其他人也是如此,所以只需尝试一下。

答案 11 :(得分:1)

转到存储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,然后“全新安装”项目。

答案 12 :(得分:1)

如果能够从终端成功编译mvn compile项目,但不能从Eclipse编译成功,请检查“窗口”>“首选项”>“已安装的JRE”,请确保已选择JDK下的JRE(检查以下路径)。图片中有2个不同的JRE),因为Maven需要JDK进行编译,因此您需要添加它。

Installed JREs

答案 13 :(得分:1)

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>

,并确保您的行家指向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"

答案 14 :(得分:1)

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

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

答案 15 :(得分:1)

在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. 更新<plugin></plugin>下的pom.xml。

  2. 更新您的项目。

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

答案 16 :(得分:1)

由于java版本不匹配,因为它出现了,所以我已经纠正了它,我能够构建war file.hope它会帮助某人

{{1}}

答案 17 :(得分:0)

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

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

答案 18 :(得分:0)

这是一个令人讨厌的错误,有时会弹出一次,记下一些有助于您的步骤:

从日食角度写答案,因为基本逻辑仍将保留 无论是通过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

答案 19 :(得分:0)

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

答案 20 :(得分:0)

您应将代码添加到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>

答案 21 :(得分:0)

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

现在,单击“以> Maven测试运行”。

此错误一定会得到解决。