我可以避免一个边缘是测试依赖的依赖循环吗?

时间:2011-05-17 17:10:25

标签: inheritance maven dependency-management circular-dependency pom.xml

考虑使用模块testCycleDummyCore的{​​{1}}父级。

TestFramework取决于TestFramework,而DummyCoreDummyCore上具有测试依赖性。

独立构建和测试每个模块maven没有问题。但TestFramework父母mvn test导致:

testCycle

重现:

    The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.mysimpatico:TestFramework:1.0-SNAPSHOT'}' and 'Vertex{label='org.apache:DummyCore:1.0-SNAPSHOT'}' introduces to cycle in the graph org.apache:DummyCore:1.0-SNAPSHOT --> com.mysimpatico:TestFramework:1.0-SNAPSHOT --> org.apache:DummyCore:1.0-SNAPSHOT -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException

我的期望是maven会构建wget http://dp4j.sf.net/debug/testCycle.zip unzip testCycle.zip cd testCycle; mvn test src,然后编译测试会编译DummyCore src,这不依赖于TestFramework。在这个阶段,它将编译DummyCore src +测试和DummyCore src。最后它还将编译TestFramework测试。有没有办法告诉maven这样做? 如果没有,你会如何解决这个问题?

DummyCore中的tests移动到自己的模块中,该模块取决于DummyCoreDummyCore?我这样做只是为了满足maven。

2 个答案:

答案 0 :(得分:4)

您无法使用Maven或任何其他构建工具解决此冲突。这不是构建工具问题,它是一个架构缺陷,只能通过refactoring解决。

立即想到两个选项:

1)创建一个名为“test_common”的新模块,其中包含TestFramework需要和DummyCore都需要的东西。 make test_common是这两个模块的依赖关系。

2)将TestFramework需要的东西从DummyCore移到TestFramework中。然后,TestFramework不依赖于任何东西,DummyCore依赖于TestFramework。

有很多方法可以解决这个问题,但无论语言或构建工具如何,循环模块间依赖都是NO-NO的重要时刻。

答案 1 :(得分:0)

但是,我同意cyle依赖关系是错误的设计,我仍然尝试提出一种情况,而这正是OP所要求的:测试。 我有基于模型的DSL测试类,这使我能够编写类似

的东西
Book book = new Book()
assertThat(book).hasNoAuthor(); // "hasNoAuthor()" is AssertJ base assertions
verify(book, never()).addAuthor(anyAuthor()); // "anyAuthor()" is mockito based matchers

有DSL对象可用于测试我的Book对象所在的domain模块以及消耗我的domain模块的其他模块。

所以我尝试创建一个domain-test模块以及现有的domain模块,但是

    domain中的
  • 单元测试取决于domain-test DSL类
  • domain-test中的
  • DSL类取决于domain

...周期来了。

打破这一周期的唯一方法是从domain模块中提取单元测试:

domain-unit-tests>取决于> domain + domain-test

domain-test>取决于domain

在分离的Maven模块中分离代码和测试对我来说听起来非常糟糕。也许我应该成长,或者也许我应该改变自己的工具...

解决方案

将所有内容保留在同一个Maven模块中,但仅使用DSL类创建一个“ test-jar”。

需要2件事

  1. 将所有需要的类放在独立的软件包中

我将使用maven-jar-plugin的“过滤器”功能仅导出我需要的内容。最简单的方法是将所需的类隔离在仅包含这些类的程序包中。

  1. 创建一个测试罐:
<plugin>
  <!-- Build a test jar with test classes that other module might use -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.2.0</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
      <configuration>
        <includes>
          <include>com/myapp/testdsl/**</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

要使用测试罐,必须在依赖项声明中添加type标签:

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>domain</artifactId>
  <version>2.1</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

IntelliJ警告

使用IntelliJ时,我对该解决方案有一个警告:

IDE不会考虑<include>过滤器,并且所有类都在类路径中。 这是迄今为止尚未解决的已知IntelliJ错误(https://youtrack.jetbrains.com/issue/IDEA-134943