How to Resolve Maven Dependency Conflicts and Avoid ClassNotFound Errors
Learn why Maven dependency conflicts occur, how the shortest‑path rule selects versions, and step‑by‑step methods—including exclusion tags, Maven Helper plugin, and IDE dependency graphs—to identify and eliminate conflicting JARs, ensuring a single consistent version in your project.
1. What Is a Dependency Conflict
Maven is a powerful dependency management tool, but it can introduce JAR conflicts when different libraries require different versions of the same artifact. For example, if library A depends on C version 1.0 and library B depends on C version 2.0, Maven downloads both and selects the version based on the “shortest dependency path” rule, causing a conflict.
Most of the time the conflict does not break the application because Maven picks one version, but under certain conditions a ClassNotFoundException may occur, so it is advisable to resolve the conflict.
2. Resolution Methods
The conflict can be resolved by adding an <exclusions> tag inside the dependency that brings the unwanted version. Example:
<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>In this case log4j-core depends on log4j-api, but another module also brings a different version of log4j-api. By excluding the transitive log4j-api from log4j-core, Maven will keep only a single version of log4j-api in the project.
3. Maven Helper
The IntelliJ IDEA plugin “Maven Helper” helps you visualise which JARs are in conflict. After installing the plugin, open pom.xml and the “Dependency Analyzer” panel appears at the bottom.
Select a conflicted dependency, right‑click and choose “Exclude” to remove the unwanted version.
4. Tips
Besides Maven Helper, IntelliJ provides a built‑in Maven Dependency Diagram. Open the Maven tool window, select “Dependencies”, and click the “Show Dependencies” icon (or press Ctrl+Alt+Shift+U) to view the graph.
In the diagram, red solid lines indicate conflicting dependencies, while blue lines represent normal dependencies.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
