多模块项目中的Maven测试依赖项 [英] Maven test dependency in multi module project

查看:187
本文介绍了多模块项目中的Maven测试依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用maven构建一个多模块项目.我的模块2取决于编译范围内的模块1 src和测试范围内的模块1测试.

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.

模块2-

   <dependency>
       <groupId>blah</groupId>
       <artifactId>MODULE1</artifactId>
       <version>blah</version>
       <classifier>tests</classifier>
       <scope>test</scope>
   </dependency>

这很好.说我的模块3依赖Module1 src并在编译时进行测试.

This works fine. Say my module 3 depends on Module1 src and tests at compile time.

模块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使其构建.我该如何解决?

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)创建的测试在另一个.如《 使用附加测试指南:

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:

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

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

因此,首先,要在JAR中打包编译的测试并将其部署以供一般重用,请按以下方式配置maven-jar-plugin:

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>

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

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

最后,要使用测试JAR,您应指定具有指定类型test-jar的依赖项:

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>

这篇关于多模块项目中的Maven测试依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆