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

查看:146
本文介绍了如何解决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.

所以我建议你可以使用像 JDependInteliJ 分析工具来查找有问题的类,并根据它们为您的软件找到更好的设计.

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天全站免登陆