Tag Archives: Maven Helper

Using Maven helper to resolve Maven plug-in conflicts

1. What is dependency conflict

Maven is an easy-to-use dependency management tool, but the best thing is not perfect. Maven’s dependency mechanism will lead to jar package conflict. For example, now in your project, you use two jar packages, a and B. Now a needs to rely on another jar package C, and B also needs to rely on C. However, a depends on version 1.0 of C and B depends on version 2.0 of C. At this time, Maven will download both 1.0 C and 2.0 C to your project, so that there are different versions of C in your project. At this time, Maven will decide which version of jar package to use according to the principle of shortest dependency path first, while another useless jar package is not used, which is the so-called dependency conflict.

Most of the time, dependency conflicts may not cause any exceptions to the system, because Maven always selects a jar package to use. However, it is not ruled out that under certain specific conditions, there will be exceptions like unable to find classes. Therefore, as long as there are dependency conflicts, in my opinion, it is best to solve them and do not leave hidden dangers to the system.

2. Solution

The method to solve dependency conflicts is to use the tag provided by Maven. The tag needs to be placed inside the tag, as follows:

<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>

log4j core itself depends on log4j API , but because some other modules also depend on log4j API , and the two log4j API versions are different, we use tags to exclude log4j core dependent log4j API , In this way, Maven will not download the log4j API that log4j core depends on, which ensures that there is only one version of log4j API in our project.

3、Maven Helper

See here, you may have a question. How can I know which dependent jar packages in my project conflict?Maven helper, a plug-in of intelij idea, helps us solve this problem. I won’t talk about the installation method of plug-ins. Since you know maven, I believe you will also install plug-ins.

After the plug-in is installed, we open the pom.xml file, and a dependency analyzer option will be added at the bottom

Click this option

Find the conflict, right-click and select exclude to exclude the jar package of the conflicting version.

4. Tips

In addition to using Maven helper to view dependency conflicts, you can also use the method provided by idea – Maven dependency structure diagram. Open Maven window, select dependencies, and then click the icon (show dependencies) or use the shortcut key (Ctrl + Alt + Shift + U) to open Maven dependency structure diagram

In the figure, we can see some red solid lines. These red solid lines are dependency conflicts, and blue solid lines are normal dependencies.

Original address: https://segmentfault.com/a/1190000017542396