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

Ste*_*eve 15 java maven-2 maven-3

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>
Run Code Online (Sandbox Code Playgroud)

如果我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
Run Code Online (Sandbox Code Playgroud)

如果我使用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
Run Code Online (Sandbox Code Playgroud)

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

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

但是为什么maven 2或maven 3 test-jarcompile阶段期间寻找依赖?它不应该等到test-compile阶段寻找这种依赖吗?

更新:答案是在编译阶段使用的maven-exec-plugin 需要在范围内依赖解析工件:test.我已经创建了一个删除范围的功能请求:测试依赖项.

Sco*_*rey 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>
Run Code Online (Sandbox Code Playgroud)

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

我想真正的解决方法,直到修复这个错误是用来test-compile代替compile.


小智 10

在我的情况下,根本原因是应该在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>
Run Code Online (Sandbox Code Playgroud)

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