【问题标题】:Getting around "introduces to cycle in the graph" maven exception绕过“在图中引入循环”maven异常
【发布时间】:2016-06-20 20:57:05
【问题描述】:

我的应用程序中有模块 A 和模块 B。模块 A 具有模块 B 的依赖关系,如下所示。

模块 A pom.xml

<dependencies>
    <dependency>
        <groupId>group.id</groupId>
        <artifactId>B</artifactId>
        <type>jar</type>
    </dependency>
</dependencies>

现在模块 B 需要访问模块 A 中定义的一个(一个)类。我尝试通过在模块 B 的 pom 中定义对模块 A 的依赖来做到这一点,如下所示,我得到了周期性异常。

  The projects in the reactor contain a cyclic reference: Edge between
 `'Vertex{label='A'}' and 'Vertex{label='B'}' introduces to cycle in the graph A --> B --> A 

问题:我该如何解决这个问题?我只需要从模块 B 访问模块 A 中的几个类。

模块 B pom.xml

<dependencies>
    <dependency>
        <groupId>group.id</groupId>
        <artifactId>A</artifactId>
        <type>jar</type>
    </dependency>
<dependencies>

【问题讨论】:

    标签: java maven web-applications build


    【解决方案1】:

    在您的情况下,最可取的解决方案是添加一个进一步的common 模块来提供这些通用(确实)类。

    然后你会从:

    project
      |_____A
      |_____B
    
    A (containing class C1 and C2) ---> B
    B ----> (C1 and C2) via A           
              |_________________not possible, cyclic dependency !!
    

    致以下:

    project
      |_____A-module
      |_____B-module
      |_____common-module (C1, C2 classes)
    
    A (without C1 and C2) ---> B
    B ----> (C1 and C2) via common
              |_________________ now possible, no cyclic dependency any longer
    

    因此,A 也将传递地依赖于 common(但如果您愿意,您也可以使其明确)。

    为了在树形视图中更清晰,您需要:

    common
      ├─ B
         └─ A
    

    这样做还可以改进未来的更改、可维护性和测试,提供更好的关注点分离,即在模块级别应用Single Responsibility Principle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2011-01-01
      • 1970-01-01
      • 2018-04-12
      • 2013-07-20
      • 2019-03-10
      相关资源
      最近更新 更多