ERROR - Maven - Rule 0: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message: Duplicate classes found:

Error

Rule 0: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message:
Duplicate classes found:

  Found in: 
    groupxxx:artifactxxx:jar:a.b:compile
    groupyyyy:artifactyyyy:jar:c.d:compile
  Duplicate classes:
    org/alphaPackage/abc.class


Error Type

Build


Cause

The error occurs because you have configured maven-enforcer-plugin which restricts the build with duplicate classes , You can see the following configuration in your pom file 


<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
            <banDuplicateClasses>
            </banDuplicateClasses>
          </rules>
        </configuration>

</plugin>


Resolution

Resolution # 1 - Ignore Duplicates - Within the plugin configuration , you can specify the class / classes that should be ignored. For example -

Ignoring a particular class duplicates 

<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
            <banDuplicateClasses>
<dependencies>
                <dependency>
                  <groupId>Module 1 group</groupId>
                  <artifactId>Module 1 artifatc</artifactId>
                  <ignoreClasses>
                    <ignoreClass>abc.class</ignoreClass>
                  </ignoreClasses>
                </dependency>
</dependencies>
            </banDuplicateClasses>
          </rules>
        </configuration>
</plugin>

Ignoring multiple classes duplicates 

<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
             <banDuplicateClasses>
<dependencies>
                 <dependency>
                   <groupId>Module 1 group</groupId>
                   <artifactId>Module 1 artifatc</artifactId>
                   <ignoreClasses>
                     <ignoreClass>org.alphaPackage.*</ignoreClass>
                   </ignoreClasses>
                 </dependency>
</dependencies>
             </banDuplicateClasses>
           </rules>
        </configuration>
</plugin>

The obove resolution only ignores the duplicates during Build but won't help in the runtime issue that might arise at runtime due to those duplicates

Resolution # 2 - Don't allow duplicates in the Build by excluding them.

Identify which jar you want and which one you would like to exclude ( groupxxx:artifactxxx or groupyyyy:artifactyyyy )

Go to the project pom directory and do mvn dependency:tree and locate the jar file which you would like to exclude

If it's a direct dependency, simply remove that dependency from the pom file configuration.

If it's a transitive dependency, first identify the direct dependency and then add exclusion config in the pom file like following

<dependency>
        <groupId>DirectDependency Group</groupId>
        <artifactId>DirectDependency Artifact</artifactId>=
        <exclusions>
          <exclusion>
            <artifactId>groupyyyy</artifactId>
            <groupId>artifactyyyy</groupId>
 </exclusion>
        </exclusions>
</dependency>

This config will not allow project / jar groupyyyy:artifactyyyy to be included in the current build and hence it won't complain about the duplicate.