为什么是“test-jar”? “mvncompile”所需的依赖项

发布于 2024-10-13 09:35:10 字数 2406 浏览 3 评论 0 原文

我在多模块项目中使用 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>

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

[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,我可以通过运行mvncompilepackage-DskipTests来编译它。使用maven 3,我还可以通过运行mvncompiletest-compile来编译它。

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

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

I'm having trouble using test-jar dependencies in a multi-module project. For example, when I declare that the cleartk-syntax module depends on the cleartk-token module's test-jar like this (the full code is 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>

I get the following error if I run mvn compile using 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

If I use maven 3 I get the error:

[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

In the latter case, I'm particularly confused because I would have thought it should be looking for an artifact of type test-jar not of type jar.

With maven 2 or maven 3, I can get it to compile by running mvn compile package -DskipTests. With maven 3, I can also get it to compile by running mvn compile test-compile.

But why is either maven 2 or maven 3 looking for a test-jar dependency during the compile phase? Shouldn't it wait until the test-compile phase to look for such dependencies?

Update: The answer was that the maven-exec-plugin, used during my compile phase, requires dependency resolution of artifacts in scope:test. I've created a feature request to remove the scope:test dependency.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵强ㄟ 2024-10-20 09:35:10

就我而言,根本原因是应在 test 范围内使用 test-jar 类型的依赖项使用的模块不包含所需的 maven-jar -插件配置。如果没有下面的代码片段,当您在相应模块上调用 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 了解更多详细信息。

In my case the root cause was that the module which should be used as a dependency in test scope with type test-jar did not include the required maven-jar-plugin configuration. Without the snippet below no test jar will be deployed when you call mvn deploy on the respective module.

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

See https://maven.apache.org/guides/mini/guide-attached-tests.html for more details.

请别遗忘我 2024-10-20 09:35:10

对我来说这看起来确实是一个错误。

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

编译阶段似乎正在反应器中查找测试 jar 工件,然后查找 repo,但它不应该这样做,因为依赖项位于测试范围内。测试范围工件应该在测试编译期间解决,而不是编译期间。

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

但这也不起作用,因为编译和测试编译之间的五个阶段尚未运行并设置类似的内容测试类路径。

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

This looks like a definite bug to me.

I have the same problem and tested Maven 3.0.1 and 3.0.2. Validate doesn't fail, only the compile step fails. With Maven 3 mvn compile breaks but mvn test-compile works.

It appears that the compile phase is looking for test-jar artifacts in the reactor and then repo, but it shouldn't since the dependency is in test scope. Test scope artifacts should be resolved during test-compile, not compile.

As a result, I thought this could be worked around by mapping the maven-compiler-plugin's testCompile goal to the compile phase, instead of the default test-compile phase.

I added this to my pom, right next to the part that adds the test-jar creation in the upstream pom:

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

But that won't work either because the five phases between compile and test-compile haven't run and set up things like the test classpath.

I guess the real workaround until this bug is fixed is to use test-compile in place of compile.

又怨 2024-10-20 09:35:10

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

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

我在这里提交了 exec:java 插件的错误报告,尽管我无法真正判断错误是否在 exec:javatest-jar< /code> 或 mvncompile 因此,如果/当有人发现这一点时,该错误可能会被转移到其他地方:

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

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

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

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

So I did some serious debugging, and discovered that the problem seems to be an interaction between the exec:java plugin, test-jar dependencies and mvn compile.

In short, if you attach exec:java to an execution phase, mvn compile starts looking for test-jar dependencies at compile time. If you remove the <executions> element from the exec:java plugin declaration, mvn compile works fine again.

I filed a bug report for the exec:java plugin here, though I can't really tell whether the bug is in exec:java, test-jar or mvn compile so perhaps the bug will be moved somewhere else if/when someone figures that out:

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

Update: It's not really a bug, the maven-exec-plugin is documented as requiring test dependencies here:

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

That doesn't mean it wouldn't make a great feature. ;-)

祁梦 2024-10-20 09:35:10

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

所以maven只是尽力去获取后面执行所需要的所有依赖。

I am using maven2. I guess the answer is in the maven lifecycle management.
The first step of a default lifecycle is validate, which does 'validate the project is correct and all necessary information is available.' (see http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html ).

So maven just tries its best to get all the needed dependencies for the later execution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文