简体   繁体   中英

Handling interdependencies in maven multi-module project

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM