多模块项目中的 Maven 测试依赖关系
我使用 Maven 构建一个多模块项目。我的模块 2 依赖于编译范围内的模块 1 src 以及测试范围内的模块 1 测试。
模块 2 -
<dependency>
<groupId>blah</groupId>
<artifactId>MODULE1</artifactId>
<version>blah</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
这很好用。假设我的模块 3 依赖于 Module1 src 并在编译时进行测试。
模块 3 -
<dependency>
<groupId>blah</groupId>
<artifactId>MODULE1</artifactId>
<version>blah</version>
<classifier>tests</classifier>
<scope>compile</scope>
</dependency>
当我运行 mvn clean install
时,我的构建运行到模块 3,在模块 3 处失败,因为它无法解析模块 1 测试依赖项。然后我单独在模块 3 上执行 mvn install
,返回并在我的父 pom 上运行 mvn install
以使其构建。我该如何解决这个问题?
I use maven to build a multi module project. My module 2 depends on Module 1 src at compile scope and module 1 tests in test scope.
Module 2 -
<dependency>
<groupId>blah</groupId>
<artifactId>MODULE1</artifactId>
<version>blah</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
This works fine. Say my module 3 depends on Module1 src and tests at compile time.
Module 3 -
<dependency>
<groupId>blah</groupId>
<artifactId>MODULE1</artifactId>
<version>blah</version>
<classifier>tests</classifier>
<scope>compile</scope>
</dependency>
When I run mvn clean install
, my build runs till module 3, fails at module 3 as it couldn't resolve the module 1 test dependency. Then I do a mvn install
on module 3 alone, go back and run mvn install
on my parent pom to make it build. How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对您想要执行的操作有疑问,但我假设您想在另一个项目(module1)中重用您为项目(module1)创建的测试。正如 使用附加测试指南:
因此,首先,要将已编译的测试打包在 JAR 中并部署它们以供一般重用,请按如下方式配置
maven-jar-plugin
:然后,照常安装/部署测试 JAR 工件(使用 <代码>mvn安装或
mvn部署
)。最后,要使用测试 JAR,您应该指定具有指定类型的
test-jar
的依赖项:I have a doubt about what you are trying to do but but I'll assume you want to reuse the tests that you have created for a project (module1) in another. As explained in the note at the bottom of the Guide to using attached tests:
So, first, to package up compiled tests in a JAR and deploy them for general reuse, configure the
maven-jar-plugin
as follows:Then, install/deploy the test JAR artifact as usual (using
mvn install
ormvn deploy
).Finally, to use the test JAR, you should specify a dependency with a specified type of
test-jar
:关于我对帕斯卡问题的评论,我认为我已经找到了一个合理的答案:
正如您在这里看到的,这里的主要区别是
标签。我将创建测试 jar,它将在测试的编译阶段可用,而不仅仅是在打包阶段之后。
对我有用。
Regarding to my comment to Pascals question i think i have found a stuitable answer :
The main difference here as you see here is the
<phase>
tag.I will create the test-jar and it will be available in the compile phase of the tests and not only after the package phase.
Works for me.