Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin

44,997

Solution 1

Your issue is most likely the <scope> of the dependency. When inside of the IDE, all scopes are added to the build path of the project. However, when running mvn only the appropriate scopes are added to the classpath for the javac command. For example, if you had a class in /src/main/java that imported a class from one of your dependencies scoped to <scope>test</scope>, the IDE would be able to build the project, but mvn would fail in the manner you are experiencing.

Solution 2

I encountered a similar error when trying to download and compile the aws kinesis consumer library (kcl)

[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ amazon-kinesis-client ---
[INFO] Compiling 81 source files to /opt/speedshiftmedia/aws.kcl/1.1.0/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.822s
[INFO] Finished at: Tue Jul 22 12:31:39 PDT 2014
[INFO] Final Memory: 4M/56M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project amazon-kinesis-client: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] javac: invalid target release: 1.7

It turns out that while I had updated my version of Java from 1.6 to 1.7, I had not updated Javac from 1.6 to 1.7.

To do so I used the following command on ubuntu

sudo update-alternatives --config javac

And then choose the desired version at the prompt.

There are 2 choices for the alternative javac (providing /usr/bin/javac).



Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-amd64/bin/javac   1061      auto mode
   1            /usr/lib/jvm/java-6-openjdk-amd64/bin/javac   1061      manual mode
   2            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1051      manual mode

Press enter to keep the current choice[*], or type selection number:

I hope this helps someone else.

Share:
44,997
rustock
Author by

rustock

Updated on July 09, 2022

Comments

  • rustock
    rustock almost 2 years

    I'm trying compile using mvn install but I'm getting this error:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project test: Compilation failure: Compilation failure:

    pom.xml file:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.mycompany</groupId>
      <artifactId>test</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>test</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>com.mycompany.test.App</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
      </plugins>
    </build>
    
      <dependencies>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.25</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    

    How to fix it?

  • rai.skumar
    rai.skumar over 10 years
    then what should be done in this case ? My project has suddenly stopped building given this as reason.
  • Lucas
    Lucas over 10 years
    @rai.skumar Use proper scopes for your dependencies. You could just use <scope>compile</scope> for everything, which should let it compile, but that is a VERY poor design. Better to just read the documentation on what the scopes mean and determine which changes you need to make.