编程知识 cdmana.com

Maven dependency conflict resolution

Maven Rely on conflict resolution

 Insert picture description here

Yes Maven If you don't know the concept, you can see my article : https://blog.csdn.net/pjh88/article/details/108520721

Before we talk about dependency conflicts, let's talk about dependency transitivity

stay maven Dependency is transitive , For example, we have A,B,C Three projects , among A rely on B,B rely on C, From recursion we can see that A rely on C

 Insert picture description here

give an example

Let's say we have web Import... In project spring-webmvc Of jar package , That is, our project depends on spring-webmvc, Actually spring-webmvc And rely on other things jar package , such as sping-aop、spring-beans, So our web The project is also indirectly dependent on sping-aop、spring-beans

 Insert picture description here

What is dependency conflict

This is due to the existence of dependency transitivity

such as spring-webmvc rely on spirng-beans-4.2.4, and spring-aop rely on spring-beans-5.0.2, But found spirng-beans-4.2.4 Added to the project , And we hope spring-beans-5.0.2 Join the project . This creates a dependency conflict .

 Insert picture description here

In our development, of course, we don't allow this to happen , What version are we going to use , He has to use what version

 Insert picture description here

Solution

  1. Use maven The principle of relying on mediation provided
    The principle of priority of the first declarant
    The principle of priority for those close to the road
  2. Exclude dependence
  3. Lock version

1. Use maven The principle of relying on mediation provided

The principle of priority of the first declarant

stay pom Define dependencies in the file , Subject to the declared dependency . In fact, it is based on the order of coordinate import to determine which passed dependency will be used finally .

 Insert picture description here

As you can see from the picture above ,spring-aop and spring-webmvc It's all passed on spring-beans, But because spring-aop in front , So the end use of spring-beans By spring-aop Handed over , and spring-webmvc Handed over spring-beans Is ignored

The principle of priority for those close to the road

stay pom File definition depends on , The one with the shortest path shall prevail .
Or the above situation ,spring-aop and spring-webmvc They'll pass it on spirng-beans, If you put spring-beans It's written directly to pom In file , Then the project will no longer use other dependencies to pass in spring-beans, Because I am directly in the pom In the definition of spring-beans It's closer than the path that other dependencies pass through

Exclude dependence

have access to exclusions Tags exclude the passed dependencies

When not in use
 Insert picture description here After use

 Insert picture description here

As you can see from the picture above ,spring-webmvc Handed over 4.2.4 Version of spring-beans It's been ruled out , What is used in the project is spring-aop Handed over 5.0.2 Version of spring-beans.

Version lock

Use dependencyManagement Tags lock dependent versions

Be careful dependencyManagement What is specified in is only corresponding to jar The version of the package , It's not going to put the designated jar Import into project , To import the corresponding jar The bag still needs to be used dependencies label
 Insert picture description here
In the use of dependence You don't need to specify the version number when labeling

 Insert picture description here
 Insert picture description here

That's all Maven Rely on conflict resolution , If you can help, please also like to pay attention to support , If you have any questions, please comment , This blog focuses on data structure, algorithms and java Development , operating system , computer network , Friends who think my article is helpful can pay attention to me , If you have any questions, please comment on your personal message , Meeting is fate , See you from high

 Insert picture description here

版权声明
本文为[A monkey who talks nonsense]所创,转载请带上原文链接,感谢

Scroll to Top