编程知识 cdmana.com

Interviewer: how to solve the Maven jar package conflict?

 Insert picture description here
Let's first introduce 2 A concept

Depend on the transfer

 Insert picture description here
Suppose we have a multi module project now , Dependency is shown in the figure , We are st-web Introduced in the module st-dal When you depend on ,st-common-lib This dependency will also be introduced by us , This is dependency passing , The following table lists scope Changes that occur in the dependency process , The column heading is the dependent module , Modules that each behavior depends on

compile test provided runtime
compile compile - - runtime
test test - - test
provided provided - provided provided
runtime runtime - - runtime

Relying on Arbitration

Dependency arbitration is when the project introduces jar package ,groupId ( The company domain name is reversed ) and artifactId ( Function command ) equally , however version Dissimilarity , Which one should be chosen version? It's also often called dependency conflict

The shortest path principle

Let's say that our current project dependencies are shown in the figure ? that maven Will choose st-common-lib What about the version of ?
The answer is 1.1 This version ,st-web To st-common-lib(1.1) The distance to 1, To st-web To st-common-lib(1.0) The distance to 2, Choose the short distance , The shortest path principle
 Insert picture description here
How to look at the distance relationship of dependence ? remember , Execute the following command to print out the global dependency tree , The hierarchy is particularly clear

mvn dependency:tree > show.txt 

Declare the principle of priority

Project dependency is shown in the figure , The path is the same ? Will choose st-common-lib Which version of ? It depends on you in pom Which dependency is declared first in the file , If in pom.xml in ,st-remote-invoker Write it at the front , Will use 1.0 This version , If st-dal Write it at the front , Will use 1.1 This version
 Insert picture description here
Sometimes the corresponding package is introduced , But the class was not found , Maybe it's because of the version conflict , Wrong version selected

Dependency exclusion

Get rid of indirectly introduced jar package

If you don't want to use spring boot Provided by default log, Want to integrate third-party log when
Or in the second example of relying on arbitration , Just want to use st-common-lib Of 1.1 edition , You can put the 1.0 The version excludes

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>

版权声明
本文为[Java knowledge hall]所创,转载请带上原文链接,感谢

Scroll to Top