roger_that
roger_that

Reputation: 9791

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

I have a maven project forked and cloned from a git repo onto my eclipse. It is build on Java 8. The first thing i do is perform a

mvn clean install

But I get following failure message:

[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

However, below is the build path details of the project which clearly has JRE - 8 set up: build path

and the compilation set up:

Compliance level

No matter how many times I build the project, i get the same error. Even after cleaning the project and refreshing it from eclipse doesn't help. Please guide.

UPDATE:

After adding the properties to set maven compiler plugin

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

,

am getting the below error:

[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

And here is the code snippet giving the compilation error from MainUITabbed class:

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

Upvotes: 141

Views: 1255645

Answers (30)

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

I guest what is happening, is that the parent pom includes <maven.compiler.release>${jdk.version}</maven.compiler.release> because the release flag is only supported in JDK 9 and higher.

Consider adding <maven.compiler.release/> to the application pom seems to unset the default property and the compiler will succeed with JDK 8

Upvotes: 0

Sushovan Mallick
Sushovan Mallick

Reputation: 43

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.

Upvotes: 2

vsingh
vsingh

Reputation: 6769

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"

Upvotes: 1

Mihai Vlasceanu
Mihai Vlasceanu

Reputation: 310

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.

Upvotes: 2

Gerry
Gerry

Reputation: 1246

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/

Upvotes: 0

Aamer
Aamer

Reputation: 622

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>

Upvotes: 0

zy_sun
zy_sun

Reputation: 453

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>

Upvotes: 1

crix
crix

Reputation: 21

I solved with adding this to the active profile in settings.xml

<properties>
   <JAVA_1_8_HOME>c:\Program Files\Java\jdk-1.8\</JAVA_1_8_HOME>
</properties>

Upvotes: 0

Richard
Richard

Reputation: 8955

Try upgrade your version of Maven, that's what fixed this for me.

export M2_HOME=/.../java/Maven/apache-maven-3.9.0
echo | mvn -v

Older versions of mvn don't work with later versions of java

Upvotes: 0

p-wel
p-wel

Reputation: 51

That was really painful 2 hours...

Here's what worked for me (while creating a new project):

  • Check what Java version you've got installed (I've used Java 11. Older versions may not be compatible with some Maven versions)
  • Environment variables - check if you've got proper Java version applied
  • Use https://start.spring.io/, instead of IntelliJ's initializator
  • Name your project in "Artifact" field. Do not use any uppercase letters. Do not rename any other fields spring.io
  • Do not rename a directory name of unpacked .zip file
  • IntelliJ: Ctrl+Alt+S and check if you are using proper Java version
  • If any changes applied -> Reload Maven -> Clean -> Install

Upvotes: 0

Charlo Poitras
Charlo Poitras

Reputation: 318

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>

Upvotes: 0

Mahbub Ul Islam
Mahbub Ul Islam

Reputation: 1059

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

Upvotes: 0

user1853517
user1853517

Reputation:

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.

Upvotes: 1

Amin
Amin

Reputation: 148

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!

Upvotes: 1

Oleh Tatsiun
Oleh Tatsiun

Reputation: 939

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

Here is example:

enter image description here

Upvotes: 2

Janac Meena
Janac Meena

Reputation: 3587

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

Upvotes: 2

user7639880
user7639880

Reputation: 1769

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>

Upvotes: 176

Basil Bourque
Basil Bourque

Reputation: 339837

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

Upvotes: 6

archit agarwal
archit agarwal

Reputation: 91

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.

Upvotes: 1

Rasool Ghafari
Rasool Ghafari

Reputation: 4278

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

Upvotes: 2

Mark Bramnik
Mark Bramnik

Reputation: 42541

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.

Upvotes: 54

Feiyu Zhou
Feiyu Zhou

Reputation: 4554

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.

Upvotes: 14

user3251882
user3251882

Reputation: 960

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

Upvotes: 7

pvma
pvma

Reputation: 433

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.

Upvotes: 2

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>

Upvotes: 7

Rohit Bhatnagar
Rohit Bhatnagar

Reputation: 31

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.

Upvotes: 3

sql_dummy
sql_dummy

Reputation: 745

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

Upvotes: 4

growdu
growdu

Reputation: 101

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>

Upvotes: 10

Srinivas Avasarala
Srinivas Avasarala

Reputation: 11

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

Upvotes: 1

Rajib Naik From CGI
Rajib Naik From CGI

Reputation: 31

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>

Upvotes: 2

Related Questions