1

我的 Maven 项目有多个 Maven 模块。其中两个模块(产品和功能)相互依赖。当我将模块作为依赖项包含在 pom 文件中时,一个 ! 标记出现在模块上。在运行 maven install 时出现此错误。

The projects in the reactor contain a cyclic reference: Edge between 
'Vertex{label='com.catalog:feature:0.0.1-SNAPSHOT'}' and 
'Vertex{label='com.catalog:product:0.0.1-SNAPSHOT'}' introduces to 
cycle in the graph com.catalog:product:0.0.1-SNAPSHOT --> 
com.catalog:feature:0.0.1-SNAPSHOT --> com.catalog:product:0.0.1-
SNAPSHOT @

如果不添加依赖项,产品将无法访问功能模块中定义的功能,反之亦然。

父 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.catalog</groupId>
    <artifactId>catalog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.enterprise</artifactId>
            <version>4.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.1.Final</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency> 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

   </dependencies>

   <modules>
       <module>category</module>
       <module>resource</module>
       <module>hibernate</module>
       <module>product</module>
       <module>helper</module>
       <module>feature</module>
   </modules>

产品模块 pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.catalog</groupId>
        <artifactId>catalog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>product</artifactId>

    <name>product</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.catalog</groupId>
            <artifactId>hibernate</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.catalog</groupId>
            <artifactId>category</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.catalog</groupId>
            <artifactId>helper</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.catalog</groupId>
            <artifactId>feature</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

功能模块 pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.catalog</groupId>
        <artifactId>catalog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.catalog</groupId>
    <artifactId>feature</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feature</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.catalog</groupId>
            <artifactId>hibernate</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.catalog</groupId>
            <artifactId>product</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>
4

2 回答 2

8

设计气味。

重构你的模块。基本上,两个模块中需要的所有东西都应该进入一个共同的依赖关系。你的依赖树中不能有循环,它就是行不通的。

更新:由于这个答案偶尔会得到新的支持,我想提一下:“依赖树”中的“树”指的是图论中的树,这意味着根据定义你不能有循环:)

于 2017-06-29T08:00:36.460 回答
0

它可能不适用于您的特定情况,但纠正周期性依赖关系的另一个选择是使用事件。

首先,将您的模块设置在没有循环依赖关系的树中。将您的事件类放在树的顶部。然后发布并监听任何模块中的事件。这允许您拥有相互通信而不相互依赖的模块。

有关如何在 Spring 中发布和侦听事件,请参阅:

https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2

于 2018-03-28T18:05:41.417 回答