在多模块maven项目中的模块之间共享src / test类 [英] Sharing src/test classes between modules in a multi-module maven project

查看:217
本文介绍了在多模块maven项目中的模块之间共享src / test类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块Maven项目。为了这个例子,考虑两个模块:

I have a multi-module Maven project. For the sake of this example, consider two modules:


  • data

  • 消费者

  • data
  • consumer

模块 consumer 将模块 data 作为依赖项。

Module consumer has module data as a dependency.

模块 data 声明了一堆核心类。在使用它们的 src / test 下进行测试。这些测试需要一些冗长的对象创建,所以我有一个类,其中包含一些实用方法来创建这些对象。此实用程序类( SampleDataHelper )位于 src / test 层次结构中。

Module data declares a bunch of core classes. There are tests under src/test that use them. These tests require some long-winded object creation, so I have a class with some utility methods in it to create these objects. This utility class (SampleDataHelper) is in the src/test hierarchy.

我还在使用者模块中进行了一些测试,需要创建一些这些冗长的对象。我想在我的<$ c中的测试中使用我的 SampleDataHelper 类(在 data src / test 中定义) $ c>消费者src / test 树。不幸的是,即使数据消费者的依赖消费者无法看到数据src / test 下存在的类。

I also have some tests in the consumer module that need to create some of these long-winded objects. I want to use my SampleDataHelper class (defined in data src/test) in tests that reside in my consumer src/test tree. Unfortunately, even though data is a dependency of consumer, consumer can't see the classes that exist under data src/test.

为了解决这个问题,我想我可能会创建另一个模块( data-test ),并将 SampleDataHelper 移动到<强> 的src / main 即可。然后我将 data-test 作为测试范围依赖于数据。不幸的是,这引入了循环依赖: data 使用 data-test ,但是 data-test 还需要数据

To combat this, I thought I might create another module (data-test), and move SampleDataHelper to it under src/main. Then I would include data-test as a test scope dependency of data. Unfortunately, this introduces a circular dependency: data uses data-test, but data-test also requires data.

我提出的唯一解决方案是在 data src下放置 SampleDataHelper / main test 包下,希望没有真正的应用程序代码可以调用它。

The only solution I've come up with is to place SampleDataHelper under data src/main under a test package and hope that no real application code ever calls it.

如何在模块之间共享我的 SampleDataHelper 类,而将它放在 src / main ?

推荐答案

您的Consumer项目取决于您的Data项目,因此我们很高兴Data必须在Consumer之前构建。因此,使用建议的技术在评论中,我会确保您的数据项目包含您希望共享的所有测试代码,并配置POM以生成测试JAR:

Your Consumer project depends upon your Data project, therefore we are happy that Data must be built prior to Consumer. As a result, using the techniques suggested in the comments, I would ensure your Data project contains all the test code that you wish to share and configure the POM to produce a test JAR:

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

您的Consumer项目将依赖于正常的Data JAR工件以及额外的 test-jar 工件,当然还有测试范围:

Your Consumer project would then depend upon both the normal Data JAR artifact, plus the additional test-jar artifact, with test scope of course:

<dependency>
  <groupId>com.foo</groupId>
  <artifactId>data</artifactId>
  <version>1.0</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

我在很多场合都使用过这种方法而且效果很好。

I've used this approach on many occasions and it works well.

这篇关于在多模块maven项目中的模块之间共享src / test类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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