0

I'm been going through the Maven Framework. While going through multi-module projects, I read that cyclic dependency is not acceptable among the modules. So I thought of a scenario, something like...

   root ----------
    - pom.xml    |
                 |
                 |--- moduleA
                 |  - pom.xml (moduleB has been added as a dependancy)
                 |
                 |--- moduleB
                    - pom.xml
                    
                    

Assume that moduleA has a property class AppProperty and a Main class which invokes another class B available in moduleB

Main class available in moduleA :-

someValue = AppProperty.get(propKey);

    //some logic
    
B mb = new B();
B.process(...);

Class B of moduleB :-

process(...) {
someOtherValue = AppProperty.get(someKey)

    // some other logic
}

Now Main will not throw any compile-time errors as its dependancies have been resolved because moduleB has been added as a dependancy in moduleA'a pom.xml. But for class B that is not the case as its invoking AppProperty class which is available in moduleA only. I cannot add moduleA's dependancy in moduleB's pom as that would lead to cyclic dependancy (if I understand it correctly).

I understand that ideally it is advised to maintain codes in an acyclic manner, but what if because of some reason removing cyclic dependancy is just not feasible? In such a scenario, is there any way to handle cyclic dependancies without actively changing the existing code logic?

  • Having cyclic dependencies can not handles by any build tool means in the end you can build your code ... that's it... you have to solve that circle... no way out ....which means change the code...or better moving things into separate module.. – khmarbaise Nov 09 '20 at 18:40

1 Answers1

1

You cannot build a project with cyclic dependencies. You need to build B before A before B, which is kind of a contradiction.

But problems like yours are easy to solve:

  • If the problem is just the class AppProperty or a few others, just move them from A to B.
  • If you have some common classes for A and B, create a helper module C and use it as dependency in A and B.
  • If A and B call each other all of the time, they should probably be just one module. So merge them to one module.
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142