为什么“mvn编译”需要“test-jar”依赖

时间:2011-01-24 20:35:23

标签: java maven-2 maven-3

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

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

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

[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

在后一种情况下,我特别困惑,因为我认为应该寻找test-jar类型jar类型的工件。

使用maven 2或maven 3,我可以通过运行mvn compile package -DskipTests来编译它。使用maven 3,我也可以通过运行mvn compile test-compile来编译它。

但是为什么maven 2或maven 3在test-jar阶段期间寻找compile依赖?它不应该等到test-compile阶段才能找到这样的依赖关系吗?

更新:答案是在编译阶段requires dependency resolution of artifacts in scope:test使用的maven-exec-plugin。我创建了a feature request to remove the scope:test dependency

5 个答案:

答案 0 :(得分:10)

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

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

看起来编译阶段正在寻找反应堆中的test-jar工件,然后是repo,但它不应该因为依赖项在测试范围内。测试范围工件应该在测试编译期间解决,而不是编译。

因此,我认为可以通过将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>

但这不会起作用,因为编译和测试编译之间的五个阶段都没有运行并设置了类似测试类路径的东西。

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

答案 1 :(得分:8)

在我的情况下,根本原因是应该用作类型为test的{​​{1}}范围中的依赖项的模块不包含所需的test-jar配置。如果没有下面的代码段,当您在相应模块上调用maven-jar-plugin时,将不会部署测试jar。

mvn deploy

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

答案 2 :(得分:1)

所以我做了一些严肃的调试,发现问题似乎是exec:java插件,test-jar依赖项和mvn compile之间的互动。

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

我在这里提交了exec:java插件的错误报告,但我无法确定错误是在exec:javatest-jar还是mvn compile,所以也许如果有人想出来的话,bug会被移到其他地方:

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

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

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

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

答案 3 :(得分:0)

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

所以maven只是尽力为后来的执行获取所有必需的依赖项。

答案 4 :(得分:0)

对于我来说,这可以解决该问题:-DskipTests

相关问题