19

test-jar在多模块项目中使用依赖项时遇到问题。例如,当我声明cleartk-syntax模块依赖于cleartk-token模块时test-jar(完整代码在这里):

<modelVersion>4.0.0</modelVersion>
<groupId>org.cleartk</groupId>
<artifactId>cleartk-syntax</artifactId>
<version>0.5.0-SNAPSHOT</version>
<name>cleartk-syntax</name>
...
<dependencies>
    ...
    <dependency>
        <groupId>org.cleartk</groupId>
        <artifactId>cleartk-token</artifactId>
        <version>0.7.0-SNAPSHOT</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>

如果我mvn compile使用 maven 2 运行,我会收到以下错误:

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.

Missing:
----------
1) org.cleartk:cleartk-token:test-jar:tests:0.7.0-SNAPSHOT

如果我使用 maven 3,我会收到错误消息:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.654s
[INFO] Finished at: Mon Jan 24 21:19:17 CET 2011
[INFO] Final Memory: 16M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project cleartk-syntax: Could not resolve
dependencies for project org.cleartk:cleartk-syntax:jar:0.5.0-SNAPSHOT: Could
not find artifact org.cleartk:cleartk-token:jar:tests:0.7.0-SNAPSHOT

在后一种情况下,我特别困惑,因为我认为它应该寻找 type 的 artifact 而test-jar不是 type jar

使用 maven 2 或 maven 3,我可以通过运行mvn compile package -DskipTests. 使用 maven 3,我还可以通过运行mvn compile test-compile.

但是为什么 maven 2 或 maven 3 在该阶段寻找test-jar依赖项compile?难道不应该等到test-compile寻找这种依赖的阶段吗?

更新:答案是在我的编译阶段使用的 maven-exec-plugin需要对 scope:test 中的工件进行依赖解析。我创建了一个功能请求来删除 scope:test 依赖项

4

4 回答 4

14

在我的情况下,根本原因是应该用作test类型范围内的依赖项的模块test-jar不包括所需的maven-jar-plugin配置。mvn deploy如果没有下面的代码片段,当您调用相应的模块时,将不会部署任何测试 jar 。

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

有关更多详细信息,请参阅https://maven.apache.org/guides/mini/guide-attached-tests.html

于 2016-01-21T17:19:16.680 回答
11

这对我来说似乎是一个明确的错误。

我有同样的问题并测试了 Maven 3.0.1 和 3.0.2。验证不会失败,只有编译步骤失败。使用 Maven 3mvn compile中断但mvn test-compile有效。

似乎编译阶段正在反应堆中寻找测试罐工件,然后是回购,但它不应该因为依赖项在测试范围内。测试范围工件应该在测试编译期间解决,而不是编译。

因此,我认为这可以通过将 maven-compiler-plugin 的 testCompile 目标映射到编译阶段而不是默认的测试编译阶段来解决。

我将此添加到我的 pom 中,就在在上游 pom 中添加 test-jar 创建的部分旁边:

  <!-- there is a bug in maven causing it to resolve test-jar types
       at compile time rather than test-compile. Move the compilation 
       of the test classes earlier in the build cycle -->
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-testCompile</id>
        <phase>compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

但这也行不通,因为 compile 和 test-compile 之间的五个阶段还没有运行并设置诸如测试类路径之类的东西。

我想在修复此错误之前真正的解决方法是使用test-compile.compile

于 2011-01-25T23:47:08.717 回答
1

所以我做了一些认真的调试,发现问题似乎是exec:java插件、test-jar依赖项和mvn compile.

简而言之,如果您附加exec:java到执行阶段,则在编译时mvn compile开始寻找依赖关系。test-jar如果您从插件声明中删除该<executions>元素,则再次正常工作。exec:javamvn compile

我在这里为该exec:java插件提交了一个错误报告,尽管我无法确定该错误是否在 中exec:javatest-jar或者mvn compile如果/当有人发现时,该错误可能会被移到其他地方:

http://jira.codehaus.org/browse/MEXEC-91

更新:这不是一个真正的错误,maven-exec-plugin 在此处记录为需要测试依赖项:

http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html

这并不意味着它不会成为一个很棒的功能。;-)

于 2011-01-26T08:07:36.603 回答
0

我正在使用maven2。我想答案在 Maven 生命周期管理中。默认生命周期的第一步是验证,它确实“验证项目是否正确并且所有必要的信息都可用”。(见http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)。

因此,maven 只是尽力获取所有需要的依赖项以供以后执行。

于 2011-01-24T21:36:04.557 回答