我们遇到了一个非常奇怪的问题,特别是因为相同的解决方案适用于项目的其他部分。
我们正在使用 Maven 3
我们在模块 A 中有一个包含单元测试和一些类(即模拟)的模块。 模块 B,依赖于 A 的源和测试源。
所以我们做了以下工作:
在模块A中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
然后在模块 B 的 pom 中我们放入:
<!--Source dependency-->
<dependency>
<groupId>myGroup</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!--Test dependency-->
<dependency>
<groupId>myGroup</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
因此,当我们构建项目时,一切正常,据我所知,构建运行“安装”阶段,因此一切正常是有道理的。 现在,我们尝试进行发布,在模块 B 的编译阶段,我们总是得到:
Failed to execute goal on project B: Could not resolve dependencies for project myGroup:B:war:1.5.0.0: Failure to find myGroup:A:jar:tests:1.5.0.0 in "Link to our artifactory" was cached in the local repository, resolution will not be reattempted until the update interval of "company name" has elapsed or updates are forced
从我在构建日志中看到的情况来看,它发生在“maven-compiler-plugin”执行期间,那么为什么它会尝试获取仅在测试范围内依赖的测试 jar?
这不是我们第一次在项目中使用此方法,而且它一直有效(到目前为止我们已经发布了多个版本)。
最佳答案
一些背景:Maven 使用两个类路径(编译和测试),因此,您将看到 Maven 编译插件被调用两次(一次是生产代码,一次是测试)。 p>
上面的错误说明Maven找不到依赖myGroup:A:jar:tests:1.5.0.0
任何地方。如果这种情况发生在第一个编译阶段(生产代码),那么这意味着测试代码泄漏到生产(“编译”)类路径中,这是不好的。
如果在编译测试时发生这种情况,则意味着您在某个地方有拼写错误。
从上面的代码中我可以看到:<type>test-jar</type>
是错的。 <type>
总是jar
。为了得到你想要的,你需要设置classifier
:
<!--Test dependency-->
<dependency>
<groupId>myGroup</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
也就是说,这是导出模拟和测试实用程序的不同解决方案:Where should I put test support code for a Java library
关于java - 发布阶段的Maven测试依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756263/