How to resolve cyclic dependency in Maven?

不想你离开。 提交于 2019-11-26 07:36:21

问题


How can we resolve a Maven cyclic dependency?

Suppose A is the parent project and B and C are child projects. If B is dependent on C and C is dependent on B, is there any way to resolve the cyclic dependency other than having a different project.


回答1:


Maven does not allow cyclic dependencies between projects, because otherwise it is not clear which project to build first. So you need to get rid of this cycle. One solution is the one you already mentioned, to create another project. Another one would be to just move some classes from B to C or vice versa when this helps. Or sometimes it is correct to merge project B and C to one project if there is no need to have two of them.

But without knowing and analyzing why your projects depend on each other it's kinda difficult to suggest the best solution.

So I suggest you can use tools like JDepend or the InteliJ analyse tool to find your problematic classes and based on them find a better design for your software.

Most of the time, I create something like an interface module and and implementation module, which gets rid of most cycles.




回答2:


Creating a new project is indeed one solution.

Dependency Inversion is the second possible solution.

Refer to here for the Acyclic Dependency Principle.

And here for the Dependency Inversion Principle.



来源:https://stackoverflow.com/questions/16468525/how-to-resolve-cyclic-dependency-in-maven

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