【发布时间】:2023-03-04 11:29:01
【问题描述】:
我有两个模块 A 和 B。实际上 B 是 A 的插件。
B 在编译时依赖于 A。 A 不依赖于 B。
在 A 运行时我想将 B 添加到类路径中,所以在 A 的 pom.xml 中我添加以下依赖项
pom.xml
<dependency>
<groupId>my_group</groupId>
<artifactId>my_Plugin</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
Maven 进程因循环依赖错误而失败
[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='A'}' and 'Vertex{label='B'}' introduces to cycle in the graph B-->A-->B -> [Help 1]
[错误]
为什么运行时依赖会影响编译时间?
【问题讨论】:
-
有一种很好的方法可以为此类项目安排构建。如果你重构共享到另一个模块(C)的代码,项目B将不再需要在编译时依赖A——A和B都可以依赖C。那么让A依赖B就没有问题了在运行时。考虑这一点的一个好方法是考虑编写一个库 - 一堆可共享的代码 - 和一个应用程序,它是组装库的集合。应用程序项目实际上根本不需要包含任何代码。
-
一种解决方案是将循环依赖项添加到配置文件中。这样当你构建模块 A 时 Maven 就不会抱怨循环依赖了。