Maven build 编译错误: 未能在项目 Maven 上执行目标 org.apache.Maven.plugins: Maven- 编译器-插件: 3.1: 编译(默认-编译)

我有一个分叉的 Maven 项目,并从 Git 回购克隆到我的蚀。它是基于 Java8构建的。我要做的第一件事就是

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

无论我构建项目多少次,我都会得到相同的错误。即使在清理项目并从 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

下面是从 MainUITab 类提供编译错误的代码片段:

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


}
}
});
}
899451 次浏览

I don't think that IDE is relevant here. After all you're running a Maven and Maven doesn't have a source that will allow to compile the diamond operators. So, I think you should configure maven-compiler-plugin itself.

You can read about this here. But in general try to add the following properties:

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

and see whether it compiles now in Maven only.

The error occurred because the code is not for the default compiler used there. Paste this code in effective POM before the root element ends, after declaring dependencies, to change the compiler used. Adjust version as you need.

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

In my case, it was caused from an incompatibility with OpenJDK 9 (which I haven't investigated).

If you don't need JDK 9, a temporary work-around would be to purge it from your machine:

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

I had the same problem and I Changed this

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

here 1.7 is my JDK version.it was solved.

Buddy,
If every this is up-to-date, even then you are having this problem, then
try running this command from the terminal directly instead of running from eclipse.
$ mvn clean install

and make sure these things:

  • maven is in system path
  • all maven dependencies are avaialble at `.m2/repository`
  • java is in system path


for it was comming because of java version mismatch ,so I have corrected it and i am able to build the war file.hope it will help someone

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

Doing this in pom.xml file and after updating the project, the problem is gone.

<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. Update the pom.xml under <plugin></plugin>.

  2. Update your project.

  3. Then clean maven and install maven.

The below pom.xml configuration is making the build successful and make sure project buildpath JRE System library should point to Java8.

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

Jdk 9 and 10 solution

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

and make sure your maven is pointing to JDK 10 or 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"

You should add the code into pom.xml like:

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

In case if you are able to compile mvn compile the project successful from terminal but not from Eclipse check out Window > Preferences >Installed JREs, make sure you have selected JRE that is under JDK (check out the paths of 2 different JRE's in pic), as Maven needs JDK to compile you need to add it.

Installed JREs

Go to the file location where the POM is stored and open cmd. Then type "mvn --v" to check the maven version and java runtime provided. Check runtime attribute and if it is "C:\Program Files\Java\jre1.8.0_191" or even close to a JRE, go to environment variables and add a new "system variable" called "JAVA_HOME" with a value "C:\Program Files\Java\jdk1.8.0_191".

Reopen the cmd and then "clean install" the project.

make sure java home path is correct. for my case, java home path is wrong in pom file.

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

I am not able to run my own build on the other suggestions here. I even tried different versions of maven-compiler-plugin: 3.1, 3.7.0, etc.

I made it work adding this:

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

I tried this approach because it seems like the /src/test/java directory is considered a java class that is why it is compiled the same time as /src/test/java. So my hunch were right in my case.

Maybe it is to others too, so just try this one.

This is an irritating error that pops up once in a while , jotting down some steps which help:

Writing answer from eclipse perspective as base logic will remain the same whether done by Intellij or command line

  1. Rt click your project -> Maven -> Update project -> Select Force update -> Click OK
  2. Under properties tag , add :
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
  1. In some instances, you will start seeing error as we tried force update saying , failure to transfer X dependency from Y path , resolutions will not be reattempted , bla bla bla

    In such case quickly fix it by cd to .m2/repository folder and run following command :

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

My issues was that I was running mvn compile from a child project directory instead of the parent project.

If your local jdk version is 11 or 9, 10, and your project's java source/target version is 1.8, and you are using org.projectlombok:lombok package, then you can try to update its version to 1.16.22 or 1.18.12, like this:

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

It just solved my issue.

I had the same problem, the solution was set JAVA_HOME in environment variables.

Worked by adding this in 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>

Even you have SDK 13 or 14.

Add the following in your pom.xml and do CTRL+S, it will automatically build the project.

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

Now click on Run as >Maven Test.

This error will definitely get solved.

<maven.compiler.release> (not source & target)

Several of the other Answers show <maven.compiler.source> & <maven.compiler.target>. Both of these are now supplanted by the simpler single element: <maven.compiler.release>.

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

So this:

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

…becomes:

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

See Question, “maven.compiler.release” as an replacement for source and target?

Go to: File -> Project Structure -> Project -> Switch Project SDK & Project language level to Java version 1.8

Here is example:

enter image description here

For me it seemed to be all the answers on this page, plus the right JDK. I added the properties:

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

and the JDK 1.8 plug-in pointing to my open JDK from Redhat:

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

and ran

mvn clean verify

and got build success. Yay.

Edit: I think this is actually what made the difference: https://mkyong.com/eclipse/eclipse-ide-no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-rather-than-a-jdk/

In my case, I set the JAVA_HOME correctly but still had problem for hours! Finally I found out that I had settings.xml in .m2 directory and the java home was set there and pointed to another path. I ran the following command to find out what java path the maven is using:

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

Then I corrected the java home path in the settings.xml in .m2 directory and problem solved!

Note: if you have your project under File->Project Structure setup for one specific version of Java and you have your Pom.xml setup for a different version of Java, you will get this error. For example, running Java 8 under Project Structure and Java 11 in your pom.xml.

I have tried to solve it manually many times, but every time error occurs in maven.

Last I left it to maven to solve this,

First use Maven clean,

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

Second use Maven Install,

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

Third use Maven Update,

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

For Java 11 this plugin should work.

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

In my case, it was because I had set the <packaging> to war instead of jar in my pom.xml while using javafx.

<packaging>jar</packaging>

Try to restart you IDE and exec mvn clean. And setting jdk full path in maven.

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