如何解决修改Maven依赖版本后触发的duplicate-finder-maven-plugin重复类检测报错
Hey there, let's break down how to resolve this duplicate class issue you're hitting with the duplicate-finder-maven-plugin.
Why This Happens
When you upgraded Interface-3 to 1.1.NEW but kept Interface-1 and Interface-2 on 1.1, Maven is pulling in overlapping class files across these dependencies. This usually happens because:
- The newer
Interface-3version includes classes that are already present inInterface-1/Interface-2(1.1) - Or, the different versions are pulling in conflicting transitive dependencies that share duplicate classes
Step-by-Step Fixes
1. Identify Exactly Which Classes Are Duplicate
First, get a detailed report of the duplicate classes to target the problem accurately. Run this command in your project root:
mvn duplicate-finder:check -X
The verbose output will show you the full list of duplicate classes, along with which JAR files they come from. This is critical for deciding the next step.
2. Align Versions of Same-Group Dependencies (Best Practice)
Since all three dependencies share the same groupId (com.abc.interfaces), they're likely part of a cohesive set. Version mismatches here are a common source of conflicts. Try:
- Upgrading
Interface-1andInterface-2to1.1.NEWto matchInterface-3<dependency> <groupId>com.abc.interfaces</groupId> <artifactId>Interface-1</artifactId> <version>1.1.NEW</version> </dependency> <dependency> <groupId>com.abc.interfaces</groupId> <artifactId>Interface-2</artifactId> <version>1.1.NEW</version> </dependency> - If upgrading the other two isn't an option, revert
Interface-3back to1.1(if that's acceptable for your project needs)
3. Exclude Duplicate Classes/Transitive Dependencies
If you must keep the version mismatch:
- Use the
dependency:treecommand to check for transitive dependency conflicts:mvn dependency:tree - If the duplicates come from transitive dependencies, add
<exclusions>to the problematic dependency in yourpom.xml. For example, ifInterface-3pulls in an older version of a library thatInterface-1already includes, exclude it:<dependency> <groupId>com.abc.interfaces</groupId> <artifactId>Interface-3</artifactId> <version>1.1.NEW</version> <exclusions> <exclusion> <groupId>[conflicting-group-id]</groupId> <artifactId>[conflicting-artifact-id]</artifactId> </exclusion> </exclusions> </dependency> - As a last resort, you can configure the
duplicate-finder-maven-pluginto ignore specific duplicate classes. Add this to your plugin configuration (note: this hides the issue rather than fixing it, so use sparingly):<plugin> <groupId>org.basepom.maven</groupId> <artifactId>duplicate-finder-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <ignoredClasses> <ignoredClass>com.abc.interfaces.duplicate.ClassName</ignoredClass> </ignoredClasses> </configuration> </plugin>
Final Note
Sticking to consistent versions for dependencies in the same group is always the safest approach—it avoids most dependency-related headaches like this one.
内容的提问来源于stack exchange,提问作者Farhan

