【问题标题】:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin未能执行目标 org.apache.maven.plugins:maven-compiler-plugin
【发布时间】:2013-05-30 01:29:03
【问题描述】:

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

[ERROR] 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project test: Compilation failure: Compilation failure:

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>

如何解决?

【问题讨论】:

  • Compilation failure: Compilation failure:。这是你的提示。
  • 编译失败。意味着你的 java 代码不会编译。
  • 如果 netbeans 编译它,那么它很可能是你依赖的范围。 IDE 将在其构建路径中提供所有范围,因此即使您的测试/提供的依赖项也会显示,但是当使用 maven 编译时,只有编译依赖项可用。
  • @rustock,Maven 确实下载了依赖项。您应该可以在~/.m2/repository/... 中找到它们。但是,下载它们并将它们放在类路径中是两件事。您可以运行 mvn -X package 来获取 maven 实际执行的详细信息,但是当您运行 maven 构建时,它只会将 compile 范围内的 jar(及其传递依赖项)添加到 javac 命令的类路径中。因此,即使您在本地存储库中有 jar,它也不一定在编译命令的类路径中。
  • 不知道,没有足够的信息来回答这个问题

标签: java maven netbeans


【解决方案1】:

您的问题很可能是依赖项的&lt;scope&gt;。在 IDE 内部时,所有范围都将添加到项目的构建路径中。但是,在运行mvn 时,只有适当的范围被添加到javac 命令的类路径中。例如,如果您在 /src/main/java 中有一个类,该类从您的一个依赖项中导入了一个类,范围为 &lt;scope&gt;test&lt;/scope&gt;,则 IDE 将能够构建项目,但 mvn 会以您遇到的方式失败。

【讨论】:

  • 那么在这种情况下应该怎么做呢?鉴于此,我的项目突然停止建设。
  • @rai.skumar 为您的依赖项使用适当的范围。您可以对所有内容使用&lt;scope&gt;compile&lt;/scope&gt;,这应该让它编译,但这是一个非常糟糕的设计。最好只阅读documentation 以了解范围的含义并确定您需要进行哪些更改。
【解决方案2】:

我在尝试下载和编译 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:

我希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 2020-09-02
    • 2021-03-11
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2019-04-09
    • 2018-10-05
    相关资源
    最近更新 更多