如何解决Maven中的循环依赖关系? [英] How to resolve cyclic dependency in Maven?

查看:858
本文介绍了如何解决Maven中的循环依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何解决Maven循环依赖关系?

How can we resolve a Maven cyclic dependency?

假设A是父项目,而B和C是子项目.如果B依赖于C并且C依赖于B,那么除了拥有不同的项目之外,还有什么方法可以解决循环依赖关系.

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.

推荐答案

Maven不允许项目之间存在循环依赖关系,因为否则,尚不清楚首先要构建哪个项目.因此,您需要摆脱这个循环.一个解决方案就是您已经提到的创建另一个项目的解决方案.另一个办法是,在有帮助的情况下,仅将某些类从B移到C,反之亦然.或者有时候,如果不需要将项目B和C合并到一个项目中,那是正确的.

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.

因此,我建议您可以使用 JDepend 之类的工具或

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.

大多数时候,我会创建诸如接口模块和实现模块之类的东西,从而消除了大多数周期. 在此线程中查看Dormouse的答案,并搜索Dependency Inversion Principle,以获取有关此主题的更多资源.

Most of the time, I create something like an interface module and and implementation module, which gets rid of most cycles. Check out the answer from Dormouse in this thread and search for Dependency Inversion Principle to get more sources on this topic.

这篇关于如何解决Maven中的循环依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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