我可以避免一个边缘作为测试依赖项的依赖循环吗?
考虑一个带有模块 DummyCore
和 TestFramework
的 testCycle
父级。
TestFramework
依赖于 DummyCore
,并且 DummyCore
对 TestFramework
具有测试依赖性。
独立maven构建和测试各个模块没有问题。但是 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
重现:
wget http://dp4j.sf.net/debug/testCycle.zip
unzip testCycle.zip
cd testCycle; mvn test
我的期望是 maven 会构建 DummyCore
src,然后编译测试编译TestFramework
src,它不依赖于DummyCore
。在此阶段,它将编译 DummyCore
src + 测试和 TestFramework
src。最后它也会编译 DummyCore
测试。有没有办法告诉maven这样做? 如果没有,您将如何解决这个问题?
将 DummyCore
中的 tests
移动到依赖于 DummyCore
和 TestFramework
的自己的模块中?我这样做只是为了让专家满意。
Consider a testCycle
parent with modules DummyCore
and TestFramework
.
TestFramework
depends on DummyCore
, and DummyCore
has a test dedepency on TestFramework
.
Building and testing each module independently maven has no problems. But mvn test
the parents testCycle
results in:
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
To reproduce:
wget http://dp4j.sf.net/debug/testCycle.zip
unzip testCycle.zip
cd testCycle; mvn test
My expectation was that maven would build DummyCore
src and then coming to compile the tests will compile TestFramework
src, which doesn't depend on DummyCore
. At this stage it would have compiled DummyCore
src + tests, and TestFramework
src. Finally it will compile DummyCore
tests too. Is there a way to tell maven to do this?
If not, how would you work around this?
Move the tests
in DummyCore
into a module of its own that depends on DummyCore
and TestFramework
? I'd be doing that just to satisfy maven.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法使用 Maven 或任何其他构建工具解决此冲突。这不是构建工具问题,而是架构缺陷,只能通过重构来解决。
我立即想到两个选项:
1)创建一个名为“test_common”的新模块,其中包含 TestFramework 和 DummyCore 都需要的内容。使 test_common 成为这两个模块的依赖项。
2)将TestFramework需要的东西从DummyCore移到TestFramework中。那么 TestFramework 不依赖任何东西,而 DummyCore 依赖于 TestFramework。
有很多方法可以解决这个问题,但是无论语言或构建工具如何,循环模块间依赖都是一个很重要的问题。
You can't resolve this conflict with Maven or with any other build tool. It's not a build tool issue, it is an architectural flaw and can only be addressed through refactoring.
Two options come immediately to mind:
1) Create a new module called "test_common" that contains the stuff that both TestFramework need and DummyCore need. The make test_common a dependency of both of those modules.
2) Move the stuff that TestFramework needs from DummyCore into TestFramework. Then TestFramework depends on nothing and DummyCore depends on TestFramework.
There are many ways to solve this, but circular inter-module dependencies are a big time NO-NO regardless of language or build tool.
然而我同意循环依赖是一种糟糕的设计,有一种情况我仍然尝试做一个,它正是正如OP所要求的:测试。
我有基于我的模型的 DSL 测试类,这使我能够编写类似的内容
。 DSL 对象对于测试我的 Book 对象所在的
domain
模块以及使用我的domain 的其他模块都很有用模块。
因此,我尝试创建一个
domain-test
模块以及现有的domain
模块,但domain
中的单元测试取决于domain-test< /code> DSL 类
domain-test
中的 DSL 类依赖于domain
... 循环来了。
打破这个循环的唯一方法是将单元测试从
domain
模块中提取出来:domain-unit-tests
>取决于>域
+域测试
域测试
>取决于domain
这对我来说在单独的 Maven 模块中分离代码和测试听起来非常糟糕。也许我应该成长,或者也许我应该改变我的工具...
解决方案
将所有内容保留在同一个 Maven 模块中,但创建一个仅包含 DSL 类的“测试 jar”。
需要的 2 件事
我将使用 maven-jar-plugin 的“过滤器”功能来仅导出我需要的内容。最简单的方法是将所需的类隔离在仅包含这些类的包中。
要使用测试 jar,您必须在依赖项声明中添加
type
标记:IntelliJ 警告
在使用 IntelliJ 时,我对该解决方案有一个警告:
IDE 不采用
过滤器考虑在内,所有类都在类路径中。这是一个已知的 IntelliJ 错误 (https://youtrack.jetbrains.com/issue/IDEA-134943 )目前尚未解决
Yet I would agree that cyle dependency is bad design, there is one case I still tried to make one and it is precisely as OP asked: testing.
I have DSL test classes based on my model which makes me able to write things like
There DSL objects are usefull both to test my
domain
module where my Book object is and other modules that consumes mydomain
module.So I tried creating a
domain-test
module along with the existingdomain
module butdomain
depends ondomain-test
DSL classesdomain-test
depends ondomain
... Here comes the cycle.
The only way to break this cycle is to extract the unit tests out of the
domain
module:domain-unit-tests
> depends on >domain
+domain-test
domain-test
> depends ondomain
This sounds terribly bad to me to separate code and tests in separated maven module. Maybe I should grow or maybe I should change my tools ...
Solution
Keep everything in the same maven module but create a 'test-jar' with only the DSL classes.
2 things needed
I will use the "filter" feature of the maven-jar-plugin to export only what I need. The easiest way to do so is to isolate the needed class in packages that only contains only these classes.
To use a test-jar, you must add the
type
tag in the dependency declaration :IntelliJ caveat
I have one caveat with that solution, when using IntelliJ:
The IDE does not take the
<include>
filter into account and all the classes are in the classpath.This is a known IntelliJ bug (https://youtrack.jetbrains.com/issue/IDEA-134943) unresolved so far