为什么"test-jar" "MVN编译"所需的依赖关系. [英] why is "test-jar" dependency required for "mvn compile"
问题描述
在多模块项目中使用test-jar
依赖项时遇到麻烦.例如,当我声明cleartk-syntax
模块依赖于cleartk-token
模块的test-jar
时(完整代码为这里):
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>
如果我使用maven 2运行mvn compile
,则会收到以下错误消息:
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
如果我使用Maven 3,则会收到错误消息:
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
在后一种情况下,我特别困惑,因为我认为它应该寻找类型为test-jar
而不是类型为jar
的工件.
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
.
使用maven 2或maven 3,我可以通过运行mvn compile package -DskipTests
使其进行编译.使用maven 3,我还可以通过运行mvn compile test-compile
来对其进行编译.
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在compile
阶段寻找test-jar
依赖项?它不应该等到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?
更新:答案是在我的编译阶段一项功能请求,以删除scope:test依赖项.
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.
推荐答案
对我来说,这似乎是一个确定的错误.
This looks like a definite bug to me.
我遇到了同样的问题,并测试了Maven 3.0.1和3.0.2.验证不会失败,只有编译步骤会失败.使用Maven 3,mvn compile
会中断,但mvn test-compile
会起作用.
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.
看来,编译阶段正在反应器中寻找测试jar工件,然后在仓库中进行回购,但不应这样做,因为依赖项在测试范围内.测试范围工件应在测试编译期间解决,而不是在编译时解决.
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.
因此,我认为可以通过将maven-compiler-plugin的testCompile目标映射到编译阶段而不是默认的test-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.
我将其添加到pom中,紧接在上游pom中添加了test-jar的部分旁边:
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.
我猜直到解决此错误之前,真正的解决方法是使用test-compile
代替compile
.
I guess the real workaround until this bug is fixed is to use test-compile
in place of compile
.
这篇关于为什么"test-jar" "MVN编译"所需的依赖关系.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!