maven jar in conflict resolution

Maven jar package conflicts in the development process is relatively common and headaches, we need to know the principles of the jar package conflicts, in order to solve the problem better jar package conflicts. This article from the jar package conflicts of principle and solve two elaborates solutions in Maven jar package conflicts.

A, Maven causes of conflict in the jar package
MAVEN project operation, if the report the following error:

By Caused: java.lang.NoSuchMethodError
Caused by: java.lang.ClassNotFoundException

in all likelihood caused by Maven jar package conflicts. Then the jar package conflicts is how to generate?

First, we need to understand the transitive dependent jar package.

1, rely on transmitting
when we need to rely on the A, A, will be introduced in pom.xml jar package; the jar package may be incorporated in A and B depend jar package, so that the pom.xml parsing Maven when, in turn, will be a, B jar package all incorporated in.

For example:
Import and native Hystrix Guava in a jar Spring Boot application:

<!--原生Guava API-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>

<-! hystrix dependent (including the dependence on the Guava) ->
<dependency>
<the groupId> org.springframework.cloud </ the groupId>
<the artifactId> Starter-Spring-Cloud-Netflix-Hystrix </ the artifactId>
<Version> 1.4.4.RELEASE </ Version>
</ dependency>

using Maven Helper widget package obtained jar imported project dependency tree:

As can be seen from FIG Hystrix JAR package depends Guava contain references: Hystrix -> Guava, so the introduction of the time-dependent Hystrix, will come Guava also introduces dependencies.

2, jar package to principles of conflict
that jar package is how to generate conflict?
Suppose the following dependencies:

A-> B-> C-> D1 (log 15.0): A B contained in dependence of B contained in dependence of C, C is included in dependence of D1, D1 is assumed to be log jar package, version 15.0

E-> F-> D2 (log 16.0): E contained in dependence on the F, F comprising the dependence on D2, D2 is assumed to be the same log jar package, version 16.0

When introduced pom.xml file A, the two dependencies E, according to the principle of transfer Maven dependence, D1, D2 will be introduced, and D1, D2 are the same D-dependent different versions.
When the method1 () method, while D1 is 15.0 (method1 D may be increased after the upgrade) we call in D2, may not have this method, so when you load the JVM A, D1 dependent, can not find method1 method, will be reported NoSuchMethodError error, this time produced a jar package conflicts.

Note:
If at the time of call method2 () method, D1, D2 contain this method (and upgraded version of the D2 does not change this method, so that even if there are multiple versions D, there is no problem of conflicting versions.)

for example:

Use Maven Helper plug-analysis results: Guava dependencies this conflict.
Before we introduced Guava native jar package, the version number is 20.0; and now prompt Guava conflict, and the conflict position jar package Hystrix located, so you can guess Hystrix contains references to different versions of Guava jar package.

In order to verify our conjecture, using Maven Helper plug-in to print out Hystrix dependent jar tree:

We can see: Hystrix jar in depends Guava jar package is 15.0, while the native Guava jar package before we introduced in pom.xml version is 20.0, 15.0 and 20.0 so that Guava will have two versions, so jar package conflicts occurred.

Two, the Maven jar package solution to the conflict
Maven pom.xml file is parsed, the same package will only keep a jar, then the face of multiple versions of the jar package, you need to how to solve it?

1, Maven default strategy
Shortest Path First

Maven face when D1 and D2, will be selected by default shortest path to the jar package, namely D2. E-> F-> D2 ratio of A-> B-> C-> D1 a short path.

The first priority statement

If the path is the same, such as: A-> B-> C1, E-> F-> C2, dependent on two path lengths are 2, then select the first statement.

2, remove the dependency: an exclusion for dependent dependent jar package
(1) we can use the jar package Maven Helper plug-in Dependency Analyzer analysis of the conflict, and then click execlude in the corresponding jar package marked red version of the above, you can the jar package excluded.

Later refresh conflict will disappear.

(2) manual exclusion
, or manually using <exclusion> tag in pom.xml packet collisions to exclude jar (using the above method execlude Maven Helper widget is actually equivalent to the method):

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>1.4.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>

mvn分析包冲突命令:

mvn dependency: Tree

3 version lock principle: generally used in the parent project inherited project
normally projects are multi-module projects, such as moduleA and moduleB co-dependent X of this dependence, then you can X extracted, and set its version number so dependent on X when the upgrade does not require respectively moduleA and moduleB modules rely X upgrade, to avoid too many places (moduleC, moduleD ....) reference X dependent forget to upgrade when the cause of conflict jar package, which is the actual project development in the more common method.

First, the definition of a parent pom.xml, will depend on the public in the pom.xml declared:

<properties>
<spring.version>spring4.2.4</spring.version>
<properties>

<the dependencyManagement>
<Dependencies>
<dependency>
<the groupId> org.springframework </ the groupId>
<the artifactId> Spring-Beans </ the artifactId>
<Version> spring.versio $ {} </ Version>
</ dependency>
</ Dependencies>
</ dependencyManagement>

such as when referring ModuleA and moduleB Spring-beans jar package, directly dependent on the parent pom.xml public can be defined:
ModuleA spring-bean using a jar in its pom.xml (no longer defined version):

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
</dependencies>

moduleB在其pom.xml使用spring-bean的jar包如上类似:

<the Dependencies>
<dependency>
<groupId> org.springframework </ groupId>
<artifactId> the Spring-Beans </ artifactId>
</ dependency>
</ the Dependencies>

These are the daily development Maven to resolve the conflict several small programs, of course, the actual development issues jar package conflicts may be much more complicated than this, we need to deal with specific issues.

 

Added: Use About Maven Helper plugin

Configuring Maven Helper plug-in

        Because in preparing to speak with Maven Helper Maven plug-ins when, in e-learning, found little information, I got all my research configuration for everyone to share! ! IDEA (I use version 2017.2)

       

IDEA Setting: Open Software: File -> Setting -> Plugins

Not open within IDEA: Configure -> Project Defaults -> Settings -> Plugins

 

 

If the download fails a red pop-up prompt, say long time to load, download the jar file is not down, I provide a jar file

                Link: https: //pan.baidu.com/s/1wz40Sbmxo-BT-kLCVUnezQ

                Password: 0lkq

                I share in Baidu cloud disk jar file, below an operation, adding that this jar file in the IDEA (MavenHelper plug-in)

 

 

 

This time I reached this figure on it, the software will restart after application, this time we need POM.xml Maven project

 

If the message went red check Maven Helper whether to start, restart the program! Until Step 4 appears! There are three viewing options after Dependency Analyzer can use Maven Helper, is to open the Dependency Analyzer view the pom file (after the file open, file the following will be more of such a tab), enter Dependency Analyzer view, respectively; Text can be added dependent xml is conflicts (conflicts), All Dependencies as list (a list View all dependent), All Dependencies as tree (tree view all dependent). And this also supports the search page. very convenient! And using the plug-in can quickly execute maven command. As shown below:

 

During the installation of the following error may occur, because the version is not compatible plug-ins and idea, another plug-in version just fine.

 

 Please indicate the source. https://blog.csdn.net/qq_33541575/article/details/80211122

                                                                                                       

Guess you like

Origin www.cnblogs.com/kexianting/p/11686266.html