解决gradle中的循环依赖 [英] Resolve circular dependency in gradle

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

问题描述

我最近开始开发一个Java项目,该项目中包含一些子项目.他们都是摇摇欲坠的.假设有两个已经实施的项目A和B.而且我将介绍另一个摇摇欲坠的项目C.依赖关系是这样的.

I recently started developing a java project which has some sub projects within it. All of them are gradle. So let's say there are two projects A and B which is already implemented. And I'm going to introduce another graldle project C. And the dependencies are like this.

  • A依赖于B
  • B对C有依赖关系
  • C依赖于A

因此,我需要实现此项目C且没有循环依赖项错误,这是我尝试使用gradle构建项目时给出的错误.我看到一些答案,认为Interface是为此的解决方案.但是在我的情况下,项目A和B是大型项目,因此我无法想到如何为它们引入接口.我唯一能做的就是为项目C引入接口.那么有没有办法解决这些情况下的问题呢?如果没有,拥有这样一个人的方式是什么?并且请注意,这些A,B,C项目是单独的项目,因此它们不能合并为一个项目.

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. I saw some answers that Interface is a solution for this. But in my case project A and B are large projects and I can't event think how to introduce a Interface for them. Only thing I can do is introducing interfaces for project 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.

推荐答案

前言

在依赖图中出现循环时,没有任何魔术可以让您编译项目.您需要进行一些重构以消除循环.

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.

1)首先将C所使用的A部分提取到一个单独的模块中(我们称其为D):

1) Start with extracting the parts of A that are used by C to a separate module (let's call it D):

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

如果D不依赖于任何其他模块,那么您就完成了.如果需要,则需要继续拆分.

If D does not depend on any other module you're done. If it does you need to continue splitting.

2)假设D stil取决于B:

2) Let's say D stil depends on B:

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

您需要类似地从B中提取通用部分(我们称其为E):

You need to analogically extract common parts from B (to let's call it E):

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

再次,如果E没有依赖关系导致循环-您就完成了.如果没有,请继续.

Once again if E has no dependencies causing a cycle - you're done. If not - continue.

3)假设E stil取决于C:

3) Let's say E stil depends on C:

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

我们该怎么办?清淡的裂片C(提取F):

What do we do? Obvioulsy split C (extract F):

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

后记

请注意,如果根本不可能(在相当长的时间和/或预算内)可行,那么可能就不那么容易了,因此,鉴于上下文,最好只复制C所依赖的A中的代码.

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.

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

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