简体   繁体   English

解决 gradle 中的循环依赖

[英]Resolve circular dependency in gradle

I recently started developing a java project which has some sub projects within it.我最近开始开发一个 java 项目,其中包含一些子项目。 All of them are gradle.它们都是 gradle。 So let's say there are two projects A and B which is already implemented.因此,假设有两个项目 A 和 B 已经实施。 And I'm going to introduce another graldle project C.我要介绍另一个gradle项目C。 And the dependencies are like this.依赖关系是这样的。

  • A has dependencies on B A 依赖于 B
  • B has dependencies on C B 依赖于 C
  • C has dependencies on A C 依赖于 A

So I need to implement this project C without cyclic dependencies error as it is given when I tried to build the project with gradle.所以我需要在没有循环依赖错误的情况下实现这个项目 C,因为它是在我尝试使用 gradle 构建项目时给出的。 I saw some answers that Interface is a solution for this.我看到一些答案,Interface 是一个解决方案。 But in my case project A and B are large projects and I can't event think how to introduce a Interface for them.但在我的情况下,项目 A 和 B 是大型项目,我想不出如何为它们引入接口。 Only thing I can do is introducing interfaces for project C.我唯一能做的就是为项目 C 引入接口。 So is there a way to solve my problem with these cases?那么有没有办法解决我在这些情况下的问题? If there isn't what is the way to have such an one?如果没有这样的方法是什么? And please note that these A,B,C projects are individual projects so those can't combine as one.请注意,这些 A、B、C 项目是单独的项目,因此不能合并为一个项目。

Foreword前言

There is no magic that will let you compile your project when there's a cycle in your dependency graph.当你的依赖图中有一个循环时,没有什么魔法可以让你编译你的项目。 You'll need to do some refactoring to eliminate the cycle.您需要进行一些重构以消除循环。

The way you deal with circular dependencies is split the modules to smaller ones and repeat that until you have eliminated the cycle.处理循环依赖的方式是将模块拆分为更小的模块,然后重复此操作,直到消除循环。

Algorithm算法

1) Start with extracting the parts of A that are used by C to a separate module (let's call it D): 1)首先将C使用的A部分提取到一个单独的模块(我们称之为D):

A -> B -> C
|         |
|         |
 --> D <--

If D does not depend on any other module you're done.如果 D 不依赖于任何其他模块,您就完成了。 If it does you need to continue splitting.如果是,则需要继续拆分。

2) Let's say D stil depends on B: 2)假设D仍然依赖于B:

A -> B -> C
|    ^    |
|    |    |
 --> D <--

You need to analogically extract common parts from B (to let's call it E):您需要从 B 中类比提取公共部分(我们称之为 E):

A -> B -> C
|    |    |
|    v    |
|    E    |
|    ^    |
|    |    |
 --> D <--

Once again if E has no dependencies causing a cycle - you're done.再一次,如果 E 没有导致循环的依赖项 - 你就完成了。 If not - continue.如果没有 - 继续。

3) Let's say E stil depends on C: 3)假设E仍然依赖于C:

A -> B -> C --
|    |    ^   |
|    v    |   |
|    E ---    |
|    ^        |
|    |        |
 --> D <------

What do we do?我们做什么? Obvioulsy split C (extract F):明显拆分 C(提取 F):

A -> B -> C --
|    |    |   |
|    v    v   |
|    E -> F   |
|    ^        |
|    |        |
 --> D <------

Afterword后记

Note that it might not be that easy if at all doable (within a sane amount of time and/or budget), so given the context it might be preferable to just duplicate the code in A that C relies on.请注意,如果完全可行(在合理的时间和/或预算内),它可能并不那么容易,因此考虑到上下文,最好只复制 C 所依赖的 A 中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM