This is the 23rd day of my participation in Gwen Challenge

The business scenario

When using Maven for project package and dependency management, you can run into dependency version conflicts

Suppose there are A/B/C dependencies in the project, where A/B is directly introduced in the POM, for example:

<dependency>
    <groupId>com.A</groupId>
    <artifactId>A</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>com.B</groupId>
    <artifactId>B</artifactId>
    <version>2.0</version>
</dependency>
Copy the code

C dependencies are required by both A and B, but they reference different versions, with A using version 2.0 of C and B using version 5.0 of C

This is fine if there are no disruptive updates between versions of C

If C adds a new method in version 5.0 and B uses it, Spring Boot will throw an exception:

Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: C.Test.test(Test.java:38) The following method did not exist: C.Test.test()LC/Test; The method's class, C. test, is available from The following locations: jar:file:/app/libs/C-2.0.jar! / C/Test. The class jar: file: / app/libs/C - 5.0. The jar! /C/ test. class The class hierarchy was loaded from The following locations: c.est: file:/app/libs/C-2.0.jarCopy the code

The error message is already clear, isn’t it?

So what we need to do is to exclude the dependency of 2.0 version C in A

plan

Manually modify

If our dependencies are clear, just go to the POM file and add exclusions to the code:

<dependency>
    <groupId>com.A</groupId>
    <artifactId>A</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.C</groupId>
            <artifactId>C</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Copy the code

Using the Idea of

If our dependencies are complex, a large project, and C’s dependencies are hidden deep, manual changes are obviously impractical

At this time we will use the universe’s first IDE Idea

Open the Maven toolbar on the side and click the button to open the dependency structure:

Press Ctrl+F to enter a conflicting dependency name, such as the common jackson-core:

Finally, just right-click -> Exclude, and you’re done:

conclusion

Read abnormality carefully, want to start again first!