In Maven, dependencies can be passed. Suppose there are three projects, namely project A, project B and project C. Suppose C depends on B and B depends on A, then we can easily deduce that project C also depends on A according to the characteristics of Maven project dependencies.
What is dependency conflict?
Everyone has to use a toolkit, and since everyone imports dependency passing, conflicts arise when they find that the toolkit versions they import are not the same
eg: Spring-webmvc-5.3.3 relies on Spring-beans-5.3.3, and spring-AOP-5.0.2 relies on Spring-beans-5.0.2, but spring beans-5.0.2 is added to the project. We want to add Spring-Beans-5.3.3 to the project. This creates dependency conflict
How do you resolve dependency conflicts
1. Use the dependency mediation principles provided by Maven
1) The first declaration takes precedence
The dependencies are defined in the POM file based on the first declared dependencies. In fact, depending on the order in which coordinates are imported to determine which dependencies to use, it is not easy to use, not recommended
2) The closest path takes precedence
Define a dependency in a POM file, whichever path is closer.
In this case, both Spring-AOP and Spring-webMVC are passed to The Spring-beans. If you write the spring-beans dependencies directly to the POM file, the project will not use the other dependencies passed to the spring-beans. This is because defining spring-beans directly in the POM itself is closer than other dependencies passing through the path
2. Eliminate dependencies
You can use the exclusions label to exclude passed dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.23.</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
Copy the code
3. Lock the version
Direct version locking method is used to determine the version of the dependent JAR package. After version locking, the locked version is added to the project regardless of the declaration order or dependency path
Use the dependencyManagement TAB to lock the dependent version. In the dependencies TAB, declare the maven coordinates that need to be imported
1) Lock the dependent version in the dependencyManagement tag
<! <dependencyManagement> <dependencies> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.33..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.33..RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
Copy the code
** Note: The use of the dependencyManagement tag on the POM file to lock the version of the dependent JARS does not actually import the jars into the project, only the versions of the jars are locked. You also need to declare in the Dependencies tag which JAR packages are used in your project.
2) Declare the Maven coordinates to import in the Dependencies tag
<dependencies> <! - because it has already been locked in dependencyManagement label spring - bean and spring - the version of the context, you just need to import groupdId and artifactId here, </groupId> </groupId> </groupId> </groupId> </groupId> </artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> </dependencies>Copy the code
eg:
Classification: [maven]