简体   繁体   English

处理maven多模块项目中的相互依赖

[英]Handling interdependencies in maven multi-module project

I'm been going through the Maven Framework .我一直在浏览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假设moduleA有一个属性类AppProperty和一个调用moduleB可用的另一个类BMain

Main class available in moduleA :- Main可用类moduleA : -

someValue = AppProperty.get(propKey);

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

Class B of moduleB :- moduleB B类:-

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 .现在Main不会抛出任何compile-time errors因为它的依赖项已经解决,因为moduleB已作为依赖项添加到moduleA'a pom.xml But for class B that is not the case as its invoking AppProperty class which is available in moduleA only.但是对于B类,情况并非如此,因为它的调用AppProperty类仅在moduleA可用。 I cannot add moduleA 's dependancy in moduleB 's pom as that would lead to cyclic dependancy (if I understand it correctly).我不能在moduleB的 pom 中添加moduleA的依赖,因为这会导致循环依赖(如果我理解正确的话)。

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.您需要在 B 之前先构建 B,这有点矛盾。

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.如果问题只是类AppProperty或其他几个,只需将它们从 A 移到 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.如果您有 A 和 B 的一些公共类,请创建一个辅助模块 C 并将其用作 A 和 B 中的依赖项。
  • If A and B call each other all of the time, they should probably be just one module.如果 A 和 B 一直互相调用,它们应该只是一个模块。 So merge them to one module.因此,将它们合并为一个模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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