How to check library conflicts in Maven


Publication Date:January 20, 2021



INFOMARTION > How to check library conflicts in Maven

summary

How to check for library conflicts in Maven. Maven automatically resolves dependencies for the jar library, but the default configuration does not inform you of conflicts. This is how to check for library conflicts in Maven in such cases.

Table of Contents

  1. When library conflicts occur in Maven
  2. How to check
  3. summary

1. When library conflicts occur in Maven

Maven automatically resolves Java library dependencies. Automatic resolution of dependencies means that if you have a Jar file that you need, it will automatically retrieve the Jar needed to run that Jar. Please refer to the following article for specific library dependency resolution mechanisms.

How Maven automatically resolves jar library dependencies?

However, when library conflicts occur, they are not automatically resolved. Furthermore, if you do not have the settings in place, you will not even be able to detect a conflict. I would like to describe how to check for library conflicts.

2. How to check

There are two main ways to check for library conflicts

  • Introducing Plug-ins
  • Verify with commands before build

The "Plug-in Installation" is a method of adding settings to pom.xml. The "command check before build" literally means that the command is manually checked before the build. I would like to explain the specifics of how to do this.

2-1. Introducing Plug-ins

The following Maven website is a good reference

http://maven.apache.org/enforcer/enforcer-rules/dependencyConvergence.html

Add the following settings to pom.xml.

pom.xml


<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <id>enforce</id>
            <configuration>
              <rules>
                <dependencyConvergence/>
              </rules>
            </configuration>
            <goals>
              <goal>enforce</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

If you put in the above settings, you will get the following error and the build will fail if there are conflicting libraries at build time.

[WARNING] 
Dependency convergence error for org.apache.commons:commons-collections4:4.1 paths to dependency are:
+-com.example.todo:todo-web:1.0.0-SNAPSHOT
  +-org.apache.poi:poi:3.17
    +-org.apache.commons:commons-collections4:4.1
and
+-com.example.todo:todo-web:1.0.0-SNAPSHOT
  +-org.apache.commons:commons-collections4:4.0

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message:
Failed while enforcing releasability. See above detailed error message.

If a build is applied with no plug-ins, no error occurs and the build terminates normally. If you want to detect conflicts, be sure to set this option.

2-2. Verify with commands before build

Dependencies are checked by the following commands

mvn dependency:tree -Dverbose

command and "omitted for conflict" is displayed if there is a conflict. It is also possible to narrow down the output to only those areas of conflict, as in the following command. ※find" is the command for Windows. For Linux, it is "grep".

mvn dependency:tree -Dverbose | find "omitted for conflict"

You can check for library conflicts by looking at the results of executing the above command.

3. summary

If you want to detect library conflicts reliably, we recommend "Install Plug-in". By installing the plugin, you can be sure that you will notice any conflicts as they will cause the build to fail.

Thank you for taking the time to read this to the end.




■INFORMATION

Please click here to go to the top page of INFORMATION.


■PROFILE

Please click here to view the profile.


■For inquiries, please contact

For inquiries about the article, please contact us here.