1

my maven project has multiple maven modules. Two of those modules (product and feature) are dependent on each other. When I include the modules as dependencies in the pom files, an ! mark appears on the modules. On running maven install I get this error.

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 @

Without adding the dependencies Product can't access functions defined in Feature module and vice versa.

The parent 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>

Product module 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>

Feature module 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>
Sid
  • 435
  • 4
  • 9
  • 19
  • Previously to build `Feature` you need to build `Product`. Previously to build `Product` you need to build `Feature`. Do you seed the cyclic dependency there? You will have to refactor those modules and either make a single module or extract the dependent code into a 3rd independent module – Alberto S. Jun 29 '17 at 07:13
  • 1
    Outcome of bad design! Create utility module and add its dependency on other modules. – z21 Jun 29 '17 at 07:14
  • @z21 Both modules have their own hibernate entity classes. Product and Feature are needing access to each other's entities. Are you suggesting I put all the entities in a separate module? As an alternate, what if I create Product and Feature as packages in the same project instead of maven modules, would that be a better design? – Sid Jun 29 '17 at 07:20

2 Answers2

9

Design smell.

Refactor your modules. Basically everything which is needed in both modules should go into a common dependency. You cannot have cycles in your dependency tree, it just won't work.

Update: As this answer is occasionally getting a new upvote, I wanted to mention: the "tree" in "dependency tree" refers to a tree from graph theory, which means that by definition you cannot have cycles :)

D. Kovács
  • 1,232
  • 13
  • 25
0

It may not work for your particular situation but another option to rectify cyclical dependencies is to use events.

First, set your modules up in a tree with no cyclical dependencies. Put your Event classes at the top of the tree. Then publish and listen for events in any of the modules. This allows you to have modules that communicate with each other without depending on each other.

For how to publish and listen for events in Spring see:

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

Collin Krawll
  • 2,210
  • 17
  • 15