java 为什么“mvn compile”需要“test-jar”依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4786881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:01:44  来源:igfitidea点击:

why is "test-jar" dependency required for "mvn compile"

javamaven-2maven-3

提问by Steve

I'm having trouble using test-jardependencies in a multi-module project. For example, when I declare that the cleartk-syntaxmodule depends on the cleartk-tokenmodule's test-jarlike this (the full code is here):

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>

I get the following error if I run mvn compileusing maven 2:

如果我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

If I use maven 3 I get the error:

如果我使用 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

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

在后一种情况下,我特别困惑,因为我认为它应该寻找 typetest-jar而非 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.

使用 maven 2 或 maven 3,我可以通过运行mvn compile package -DskipTests. 使用 maven 3,我还可以通过运行mvn compile test-compile.

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

但是为什么 maven 2 或 maven 3test-jar在这个compile阶段寻找依赖呢?不应该等到test-compile阶段寻找这种依赖关系吗?

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.

更新:答案是在我的编译阶段使用的 maven-exec-plugin需要对 scope:test 中的工件进行依赖解析。我创建了一个功能请求来删除 scope:test dependency

采纳答案by Scott Carey

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 compilebreaks but mvn test-compileworks.

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

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.

似乎编译阶段正在反应器中寻找 test-jar 工件,然后再进行 repo,但它不应该,因为依赖项在测试范围内。测试范围工件应该在测试编译期间解决,而不是编译。

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.

因此,我认为可以通过将 maven-compiler-plugin 的 testCompile 目标映射到 compile 阶段而不是默认的 test-compile 阶段来解决这个问题。

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

我将它添加到我的 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>

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.

但这也行不通,因为 compile 和 test-compile 之间的五个阶段还没有运行,也没有设置测试类路径之类的东西。

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

我想真正的解决方法,直到这个错误解决方法是使用test-compile到位compile

回答by Tobias Meyer

In my case the root cause was that the module which should be used as a dependency in testscope with type test-jardid not include the required maven-jar-pluginconfiguration. Without the snippet below no test jar will be deployed when you call mvn deployon the respective module.

在我的情况下,根本原因是应该用作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>

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

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

回答by Steve

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

于是认真的调试了一下,发现问题好像是exec:java插件、test-jar依赖和mvn compile.

In short, if you attach exec:javato an execution phase, mvn compilestarts looking for test-jardependencies at compile time. If you remove the <executions>element from the exec:javaplugin declaration, mvn compileworks fine again.

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

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

我提交了一份bug报告的exec:java插件在这里,虽然我真的不能告诉错误是否exec:javatest-jarmvn compile因此,或许bug会在其他地方如果移动/人的数字时指出:

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

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:

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

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

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

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

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

回答by WeMakeSoftware

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

我正在使用 maven2。我想答案就在 Maven 生命周期管理中。默认生命周期的第一步是验证,它确实“验证项目是否正确并且所有必要的信息都可用”。(参见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.

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