Resolving Maven circular dependencies between test, testhelper, and project-under-test

流过昼夜 提交于 2019-12-01 17:58:12

The problem that you need to solve is the dependency from Atesthelper to A, then everything else will work fine: A depends on Atesthelper, B depends on Atesthelper and both A and B will contain both sources and tests. Atesthelper will be included with scope test in both A and B. That's your target state.

How do you get there? You need to extract the items that Atesthelper is depending on into a separate projects. Typically, these are interfaces or other common functionality, which should be put into a separate project anyway - let's call it ACommon. So your target layout should look like this:

ACommon <- Atesthelper
       ^    ^
       |   /
         A (and also B)

What kind of functionality is Atesthelper depending on in A? Can you move it to a separate project (ACommon)?

If I understand you right, your primary goal is to reuse test classes. A_t and Atesthelper are not Maven projects, they are tests.

Make Maven to create a jar from your src/test/java. See http://maven.apache.org/guides/mini/guide-attached-tests.html

After build of A you will get A.jar and A-tests.jar. A-tests.jar will include Atesthelper and A_t.

Set dependency of B to A-tests.jar (<type>test-jar</type>).

You didn't paste your POMs and I'm not sure if I understand you correctly, but I'll try to help you how I've got it. This is quite common case that usually should be solved like this:

Your Atesthelper artifact (probably jar packaging), that supports testing, should have all its testing-related stuff in the src/main directory (so classes in src/main/java, resources in src/main/resources, etc.), not src/test! All its dependencies, so A, but also stuff like JUnit, EasyMock (all stuff that testing-support classes need) you declare with compile scope, which is the default one of course. Don't use test scope here! Finally, in A and B artifacts, declare Atesthelper as dependency of test scope.

That solution works great for me for many years. It is clean and straigt about dependencies (they're transitive in contrast to <type>test-jar</type>-based solution if you know what I mean). Anyway, just use it. You'll be happy ;).

The cleanest way to solve this issue is to keep the projects separated. There are frameworks like Maven that encourage you to put Classes and Test Classes into one single project. Eclipse PlugIn projects for example have separate test projects. Making A_t a sub-project of A sounds like the best solution, since you can reuse whatever dependencies you want in that test project without worrying about the impact on A.

This scenario will arise more often and you might not want to bother your project design with dependencies of tests. Those tests should be independent and not influence your class design.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!