5

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

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

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>

如何解决?

4

2 回答 2

7

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

于 2013-06-03T23:09:44.207 回答
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:

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

于 2014-07-22T19:50:15.843 回答