Multiple versions of the same dependency in Maven

前端 未结 6 2000
抹茶落季
抹茶落季 2020-11-29 06:19

Is it possible to declare multiple versions of the same dependency in a Maven repo?

I need these dependencies all at once:

    

        
6条回答
  •  醉酒成梦
    2020-11-29 06:53

    Here is the relevant part of the Maven documentation, which explains how Maven chooses the version of a dependency when there is more than one possibility:

    Dependency mediation - this determines what version of an artifact will be chosen when multiple versions are encountered as dependencies. Maven picks the "nearest definition". That is, it uses the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins. "nearest definition" means that the version used will be the closest one to your project in the tree of dependencies. Consider this tree of dependencies:

    A
    ├── B
    │   └── C
    │       └── D 2.0
    └── E
        └── D 1.0
    

    In text, dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0, as shown here:

    A
    ├── B
    │   └── C
    │       └── D 2.0
    ├── E
    │   └── D 1.0
    │
    └── D 2.0      
    

提交回复
热议问题