如何解决gradle多项目构建中的循环依赖 [英] How to solve circular dependency in gradle multi-project build

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

问题描述

请考虑以下情况.我有两个gradle(子)项目,分别称为"A"和"B". A定义了B引用的一些类/接口.因此B对A具有编译依赖性.现在A是一个Web服务器,应在类路径上以B开头. 如何通过gradle实现这一目标?

Consider the following situation. I have two gradle (sub-)projects called "A" and "B". A defines some classes/interfaces that are being referenced by B. So B has a compile dependency to A. Now A is a web server that should be started with B on the classpath. How do you achieve that with gradle?

当然不可能将B作为编译依赖项添加到A,因为这将意味着A和B之间的循环依赖项.即使将B作为运行时依赖项添加到A也不起作用,因为然后在B引用了类的状态下编译错误来自A的不存在. 但是为什么?

Of course it is not possible to add B as compile dependency to A because that would mean a circular dependency between A and B. Even adding B as runtime dependency to A did not work because then compile errors in B state that referenced classes from A do not exist. But why?

一种解决方案是将代码从B移到A,但我真的很想分离该代码,因为稍后可能会有B的另一种实现,我想在A中轻松交换(例如,通过在运行时类路径中交换jar)

One solution would be to move code from B into A but I really would like to separate that code because there might be another implementation of B later that I want to swap easily in A (e.g. by exchanging the jar in runtime classpath).

我正在考虑的另一个解决方案是将B引用的类从A分离到一个新模块中,并使A和B都依赖于该新模块.这听起来很有效,但这意味着将持久层从A移到感觉不对的新模块.

Another solution I was thinking about is to separate classes from A referenced by B into a new module and make both A and B depend on that new module. This sounds valid but that would imply to move persistence layer from A to that new module which feels wrong.

其他信息:A是具有持久层,Web服务等的Spring Boot Web应用程序,B产生JAR.

Additional information: A is a Spring boot web application with persistence layer, web services etc, B produces a JAR.

推荐答案

当您尝试获取依赖注入时,循环依赖是一个众所周知的问题.在这种情况下,您可以在模块级别上找到类似的东西

Circular dependencies are a well known problem when you try to get Dependency Injection. In this case, you have something similar but at module level

我认为可以解决问题的唯一方法是使用通用代码(可能是B引用的A接口)创建第三个模块C

The only way I see you can solve your issue is by creating a third module C with the common code (probably the A interfaces referenced by B)

这样,您可以编译C(没有任何依赖项),A(取决于C)和B(取决于C)并使用以下命令启动A B在其类路径中

This way you can compile C (it doesn't have any dependencies), A (it depends on C) and B (it depends on C) and launch A with B in its classpath

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

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