Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting cyclic dependencies with Maven

Tags:

java

maven-2

In my application I'm using an external library (Batik 1.7) that is made up of several modules. The modules have multiple cyclic dependencies between themselves. This doesn't influence the build, but some tools (e.g. the M2Eclipse Dependency Graph, or the Dependencies report) will not work anymore.

Is there a good way to diagnose what cycles are there, and an easy way to get rid of them?

Update: The problem is with the POMs, as e.g. batik-bridge is dependent on batik-gvt, which is in turn depend upon batik-bridge.

I think I can solve this by excluding some of the dependencies manually, but I'm not sure what to exclude. What I'd like to have is a clear overview of the cycles in the graph.

like image 495
Lóránt Pintér Avatar asked Oct 22 '09 15:10

Lóránt Pintér


People also ask

How do you identify cyclic dependency?

Analyze cyclic dependenciesFrom the main menu, select Code | Analyze Code | Cyclic Dependencies. In the Specify Cyclic Dependency Analysis Scope dialog, select the scope of files that you want to analyze. Select the Include test sources option if you want to analyze your test code together with the production code.

Does maven allow circular dependencies?

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.

How do you resolve cyclic reference in Maven?

So you need to get rid of this cycle. One solution is the one you already mentioned, to create another project. Another one would be to just move some classes from B to C or vice versa when this helps. Or sometimes it is correct to merge project B and C to one project if there is no need to have two of them.

How do you deal with cyclic dependency?

When we have a circular dependency, it's likely we have a design problem and that the responsibilities are not well separated. We should try to redesign the components properly so that their hierarchy is well designed and there is no need for circular dependencies.


1 Answers

Try running this from the command line in the root of your topmost project:

mvn dependency:tree
like image 147
serg10 Avatar answered Sep 22 '22 21:09

serg10