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