首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么"mvn编译“需要"test-jar”依赖?

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

Stack Overflow用户
提问于 2011-01-24 20:35:22
回答 4查看 13.8K关注 0票数 21

在多模块项目中使用test-jar依赖关系有困难。例如,当我声明cleartk-syntax模块依赖于cleartk-token模块的test-jar时(完整的代码是here):

代码语言:javascript
复制
<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,将得到以下错误:

代码语言:javascript
复制
[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,就会得到错误:

代码语言:javascript
复制
[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阶段才寻找这样的依赖关系吗?

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-01-25 23:47:08

在我看来,这确实是个错误。

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

编译阶段似乎在寻找反应堆中的test-jar工件,然后再进行repo,但是它不应该这样做,因为依赖关系在测试范围内。测试范围工件应该在测试编译期间解析,而不是编译。

因此,我认为可以将maven编译器插件的testCompile目标映射到编译阶段,而不是默认的测试编译阶段。

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

代码语言:javascript
复制
  <!-- 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>

但这也不起作用,因为编译和测试编译之间的五个阶段还没有运行和设置像测试类路径这样的东西。

我想,在修复这个bug之前,真正的解决办法是使用test-compile代替compile

票数 12
EN

Stack Overflow用户

发布于 2016-01-21 17:19:16

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

代码语言:javascript
复制
<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

票数 16
EN

Stack Overflow用户

发布于 2011-01-26 08:07:36

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

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

我在这里为exec:java插件提交了一份bug报告,但我无法真正判断该bug是在exec:javatest-jar还是mvn compile中,因此,如果/当有人发现该错误时,该bug可能会转移到其他地方:

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

更新:实际上不是一个bug,这里记录了maven-exec需要测试依赖项:

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

这并不意味着它不会成为一个伟大的特征。;-)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4786881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档