Home:ALL Converter>How to resolve cyclic dependency in Maven?

How to resolve cyclic dependency in Maven?

Ask Time:2013-05-10T02:24:23         Author:user2367460

Json Formatter

How can we resolve a Maven cyclic dependency?

Suppose A is the parent project and B and C are child projects. If B is dependent on C and C is dependent on B, is there any way to resolve the cyclic dependency other than having a different project.

Author:user2367460,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16468525/how-to-resolve-cyclic-dependency-in-maven
mszalbach :

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. 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.\n\nBut without knowing and analyzing why your projects depend on each other it's kinda difficult to suggest the best solution.\n\nSo I suggest you can use tools like JDepend or the InteliJ analyse tool to find your problematic classes and based on them find a better design for your software.\n\nMost of the time, I create something like an interface module and and implementation module, which gets rid of most cycles. \nCheck out the answer from Dormouse in this thread and search for Dependency Inversion Principle to get more sources on this topic.",
2013-05-09T20:03:43
Dormouse :

Creating a new project is indeed one solution.\n\nDependency Inversion is the second possible solution.\n\nRefer to here for the Acyclic Dependency Principle.\n\nAnd here for the Dependency Inversion Principle.",
2014-01-20T15:38:31
Naveen :

Lets say A project depends on B and B depends on A.\n\nCreating new project and move all common classes. This is the first preferred option. If not go with option 2.\nTry to make one dependency as runtime dependency(Example , A is compile time dependent on B and B is runtime dependent on A). This will help to resolve which jar to build first. In this case, B jar should be build first and then A. Here, runtime means API's can be invoked through reflection.\n",
2021-07-26T20:30:27
yy