无法执行目标org.apache.maven.plugins:maven-compiler-plugin

时间:2013-06-03 19:36:06

标签: java maven netbeans

我正在尝试使用mvn install进行编译,但我收到此错误:

  

[错误]无法执行目标org.apache.maven.plugins:maven-compiler-plugin:2.0.2:项目测试时编译(default-compile):编译失败:编译失败:

pom.xml文件:

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

如何解决?

2 个答案:

答案 0 :(得分:7)

您的问题很可能是依赖项的<scope>。在IDE内部时,所有范围都将添加到项目的构建路径中。但是,在运行mvn时,只会将适当的作用域添加到javac命令的类路径中。例如,如果您在/src/main/java中有一个类从一个作用域<scope>test</scope>的依赖项导入了一个类,那么IDE将能够构建项目,但mvn将失败你正在经历的方式。

答案 1 :(得分:1)

我在尝试下载和编译aws kinesis使用者库(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

事实证明,虽然我已将我的Java版本从1.6升级到1.7,但我没有将Javac从1.6升级到1.7。

为此,我在ubuntu上使用了以下命令

sudo update-alternatives --config javac

然后在提示符下选择所需的版本。

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:

我希望这有助于其他人。