且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决Maven中的循环依赖?

更新时间:2022-06-25 06:10:54

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.