繁体   English   中英

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

[英]Maven build Compilation error : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven

我有一个 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 类编译错误的代码片段:

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 中,以更改使用的编译器。 根据需要调整版本。

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

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

您可以在此处阅读相关内容。 但通常尝试添加以下属性:

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

并查看它现在是否仅在 Maven 中编译。

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

它刚刚解决了我的问题。

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

这是一个令人不快的错误,偶尔会弹出,记下一些有帮助的步骤:

无论是由 Intellij 还是命令行完成,从 Eclipse 角度编写答案作为基本逻辑都将保持不变

  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

就我而言,这是由于与 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

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

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

并确保这些事情:

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


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

已安装的 JRE

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

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

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

因为它是由于 java 版本不匹配而出现的,所以我已经纠正了它,我能够构建战争文件。希望它会帮助某人

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

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

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

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

我使它工作添加这个:

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

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

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

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

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

<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”作为源和目标的替代品?

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

这是示例:

在此处输入图像描述

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

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

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"

在您的 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 测试。

这个错误肯定会得到解决。

在运行IntelliJ时,我遇到了类似的问题。 就我而言,解决方案是在项目设置中设置正确的SDK。

文件>项目结构>项目设置> Project SDK

默认情况下,它设置为使用Java 11,但是我的项目却改为使用Java 8。

在此处输入图片说明

错误:无法在项目上执行目标org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile(default-testCompile)

IntelliJ Idea 2019

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

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

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

和 JDK 1.8 插件指向我从 Redhat 中打开的 JDK:

    <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-而不是-a-jdk/

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

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

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

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

我曾多次尝试手动解决它,但每次在 maven 中都会出现错误。

最后我把它留给 maven 来解决这个问题,

首先使用Maven clean,

Right-click on project -> Run as -> Maven Clean

第二次使用 Maven 安装,

Right-click on project -> Run as -> Maven Install

第三次使用 Maven 更新,

Right-click on project -> Maven -> Update Project

对于 Java 11,这个插件应该可以工作。

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>

就我而言,这是因为我在使用 javafx 时在 pom.xml 中将<packaging>设置为war而不是jar

<packaging>jar</packaging>

尝试重新启动 IDE 并执行 mvn clean。 并在 maven 中设置 jdk 完整路径。

<properties>
    <java.version>17</java.version>
    <JAVA_HOME>/home/jdk-17</JAVA_HOME>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>17</source>
                <target>17</target>
                <verbose>true</verbose>
                <fork>true</fork>
                <executable>${JAVA_HOME}/bin/javac</executable>
            </configuration>

        </plugin>


    </plugins>

</build>

2个小时真的很痛苦。。。

这是对我有用的(在创建新项目时):

  • 检查您安装的 Java 版本(我使用的是 Java 11。旧版本可能与某些 Maven 版本不兼容)
  • 环境变量 - 检查您是否应用了正确的 Java 版本
  • 使用https://start.spring.io/ ,而不是 IntelliJ 的初始化器
  • 在“工件”字段中命名您的项目。 不要使用任何大写字母。 不要重命名任何其他字段弹簧.io
  • 不要重命名 unpacked.zip 文件的目录名称
  • IntelliJ:Ctrl+Alt+S 并检查您是否使用正确的 Java 版本
  • 如果应用了任何更改 -> 重新加载 Maven -> 清理 -> 安装

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM