What are dependency/versioning conflicts?

  • MaventheDepend on the mechanismCan cause Jar package conflicts.

For example, in the project, two Jar packages, A and B, are used.

Now A needs to depend on another Jar package C, and B also needs to depend on C.

But A depends on version 1.0 of C, and B depends on version 2.0 of C.

At this point, Maven will download both 1.0 C and 2.0 C into your project, so that different versions of C exist in your project.

Maven uses the shortest path first principle (that is, depending on the shortest dependency path found) to decide which version of Jar to use while another useless Jar is not used. This is called dependency conflict.

Most of the time, dependency conflicts probably won’t cause an exception to the system, because Maven always selects a Jar package to use.

However, it is not ruled out that under certain conditions, exceptions like a class that cannot be found may occur.

Therefore, as long as there is a dependency conflict, it is best to solve it, do not leave a hidden danger to the system.

The solution

The solution to the dependency conflict is to use the Maven provided tag < Exclusion >, which needs to be placed inside the tag like this:

< the dependency > < groupId > org. Apache. Logging. Log4j < / groupId > < artifactId > log4j - core < / artifactId > < version > 2.10.0 < / version > <exclusions> <exclusion> <artifactId>log4j-api</artifactId> <groupId>org.apache.logging.log4j</groupId> </exclusion> </exclusions> </dependency>Copy the code

For example, log4J-core itself relies on log4J-API, but since some other modules also rely on log4j-API and the two log4J-API versions are different, we use the tag to exclude the log4j-api that log4j-core relies on. This way Maven will not download the log4J-API on which log4J-core depends, ensuring that there is only one version of log4J-API in our project.

This article uses the article synchronization assistant to synchronize