Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving or Compiling Circular Dependency in Maven [duplicate]

Tags:

I have a bit of a funny problem and instead of looking for a solution to it, I'm looking for solutions.

Project Alice has a pom.xml. In it the pom says she gets packaged as a jar and although she is a strong woman, she is dependent on Bob. Project Bob, being a complementarian, says he depends on Alice. Ergo a circular dependency.

Of course, running mvn compile on Alice says "Alice is missing Bob". And Bob, that true romantic, if you try and compile him, he misses Alice too.

Since neither will comply without the other present, I'm looking for ways to resolve this.

There is only two ways I know how to resolve this:

  1. Marry them and make them one maven project.
  2. Break their co-dependency

Besides the fact that I don't want to promote incest, would making a parent pom and making Alice and Bob siblings solve this?

Any other solutions?

like image 947
Lan Avatar asked Nov 21 '14 23:11

Lan


People also ask

How do you resolve circular dependency issues?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

How do you resolve cyclic reference in Maven?

Maven does not allow cyclic dependencies between projects, because otherwise it is not clear which project to build first. So you need to get rid of this cycle. One solution is the one you already mentioned, to create another project.

What is circular dependency error and how do you handle it?

When you see the circular dependency detected error displayed in your Google spreadsheet, this means that your formula is referring to a range that contains the formula itself, or in other words when the formula input, is dependent on the output.


1 Answers

Figure out what it is that Alice and Bob desperately need from each other, and introduce that - let's call it Charlie - as its own separate POM. Then, have Alice and Bob depend on Charlie.

The big thing to note here is that circular dependencies arise often due to certain modules encompassing more than it needs to. Given that Alice needs Bob and Bob needs Alice, there is something that could be split off from within those two modules and introduced as a third one.

This is probably not the most attractive solution, but it's the cleanest. You then introduce more modularity into your system, and some more opportunities for module refactoring.

like image 193
Makoto Avatar answered Oct 16 '22 07:10

Makoto