多模块项目中的Maven测试依赖关系(Maven test dependency in multi module project)

编程入门 行业动态 更新时间:2024-10-27 00:27:15
多模块项目中的Maven测试依赖关系(Maven test dependency in multi module project)

我使用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?

最满意答案

我怀疑你正在努力做什么,但是我会假设你想重新使用你为另一个项目(module1)创建的测试。 如使用附件测试指南底部的说明所述:

请注意,本指南的以前版本建议使用<classifier>tests</classifier>而不是<type>test-jar</type> 。 虽然这在某些情况下目前有效,但在调用安装之前的生命周期阶段时,在测试JAR模块和任何消费者的反应堆构建期间,它无法正常工作。 在这种情况下,Maven将不会从反应堆构建的输出中解析测试JAR,而是从本地/远程存储库中解析出。 显然,存储库中的JAR可能已经过时或完全丢失,导致构建失败(参见MNG-2045 )。

因此,首先,在JAR中打包编译的测试并部署它们以进行一般重用,请按照以下步骤配置maven-jar-plugin :

<project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

然后,像往常一样安装/部署测试JAR工件(使用mvn install或mvn deploy )。

最后,要使用测试JAR,应该使用指定类型的test-jar指定一个依赖关系:

<project> ... <dependencies> <dependency> <groupId>com.myco.app</groupId> <artifactId>foo</artifactId> <version>1.0-SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency> </dependencies> ... </project>

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:

Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).

So, first, to package up compiled tests in a JAR and deploy them for general reuse, configure the maven-jar-plugin as follows:

<project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

Then, install/deploy the test JAR artifact as usual (using mvn install or mvn deploy).

Finally, to use the test JAR, you should specify a dependency with a specified type of test-jar:

<project> ... <dependencies> <dependency> <groupId>com.myco.app</groupId> <artifactId>foo</artifactId> <version>1.0-SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency> </dependencies> ... </project>

更多推荐

本文发布于:2023-08-05 21:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多模   关系   测试   项目   Maven

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!