2011-01-24 47 views
13

我在多模块项目中使用test-jar依赖关系时遇到问题。例如,当我宣布cleartk-syntax模块依赖于cleartk-token模块的test-jar像这样(完整代码here):为什么“mvn compile”需要“test-jar”依赖关系

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

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

<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

如果我用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在compile阶段寻找test-jar依赖关系?它不应该等到test-compile阶段才能寻找这样的依赖关系吗?

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

回答

8

这看起来像是一个明确的bug。

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

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

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

我说这个我POM,旁边,增加了上游POM测试-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> 
    </execution> 
    </plugin> 

但是,这不会工作,要么是因为编译和测试之间的五个阶段-compile没有运行并设置类似测试类路径的东西。

我猜真正的解决方法,直到这个bug被修复是使用test-compile代替compile

0

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

因此,maven只是尽力为后面的执行获取所有需要的依赖关系。

+0

但是,如果是这样的话,为什么`mvn compile package`工作? – Steve 2011-01-24 22:54:25

+0

你已经提到它只在你提供-DskipTests = true变量时才有效。这可以是内部优化maven的东西吗? – WeMakeSoftware 2011-01-25 08:36:10

+0

它没有使用-DskipTests。我只是补充说,因为我只是试图编译,而不是测试。 – Steve 2011-01-25 14:44:58

1

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

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

我提交了一份bug报告为exec:java插件在这里,虽然我真的不能告诉的bug是否exec:javatest-jarmvn compile所以或许bug会在其他地方如果/移动,当有人数字了这一点:

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

更新:这不是一个真正的错误,行家-EXEC-插件记录为需要测试的依赖性在这里:

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

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

7

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

<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

相关问题