49

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
user2367460
  • 499
  • 1
  • 4
  • 3
  • 1
    You have ruled out creating a new project, which is what I would do... Any reason you don't want to do that? It would be the best solution. – NilsH May 09 '13 at 18:30
  • If you can't create a new project, can you move all the dependent functionnality only to B or C ? – Mehdi Oct 09 '18 at 19:30
  • What maven does is elevate the "cyclical dependency bad" to project level, which is very limiting. If you have a true cyclical dependency at code (class) level in java, yeah sure that is bad... Or just dumb. But at project level??? Come on. That's just limiting yourself. Having cross-dependency between modules is not "perfect" but it has a use case. So long as it doesn't translate to any bad runtime cases. Some classes might need code for legacy reasons, etc. A silly maven convention framing the minds of gullible, lazy programmers. – niken Jun 15 '20 at 18:35

4 Answers4

43

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.

But without knowing and analyzing why your projects depend on each other it's kinda difficult to suggest the best solution.

So 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.

Most of the time, I create something like an interface module and and implementation module, which gets rid of most cycles. Check out the answer from Dormouse in this thread and search for Dependency Inversion Principle to get more sources on this topic.

mszalbach
  • 10,612
  • 1
  • 41
  • 53
  • 3
    A cheap trick for locating dependencies is to remove the one you're having trouble with and see where the compiler complains. – nclark Nov 08 '18 at 19:41
  • Hi @mszallbach, could you explain more about how you use the Interface module and Implementation Module to get rid of the cycles? – Nathalie Apr 21 '20 at 02:14
  • 1
    @Nathalie check the answer from Dormouse and the corresponding Wiki link to Dependency Inversion Principle. When you search for Dependency Inversion Principle there should be plenty of sources. – mszalbach Apr 21 '20 at 17:32
11

Creating a new project is indeed one solution.

Dependency Inversion is the second possible solution.

Refer to here for the Acyclic Dependency Principle.

And here for the Dependency Inversion Principle.

Dormouse
  • 1,617
  • 1
  • 23
  • 33
1

Lets say A project depends on B and B depends on A.

  1. Creating new project and move all common classes. This is the first preferred option. If not go with option 2.
  2. Try 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.
Naveen
  • 21
  • 3
0

I will introduce the way how I fixed those issues of circular dependencies.

I have two modules that includes each other. I removed dependencies on each side and setup RabbitMQ messaging system and create direct exchange. So when I need something from module1 I send message to RabbitMQ and get response and process data from module2. I think this is the better way of integrating components and it's loose integration.

Heril Muratovic
  • 1,940
  • 6
  • 25
  • 46