Ban Duplicate Classes
Ban Duplicate Classes
This rule checks the dependencies and fails if any class is present in more than one dependency.
The following parameters are supported by this rule:
-
ignoreClasses - a list of classes to ignore duplicates of. Wildcards can be specified using the * character.
-
findAllDuplicates - a boolean to indicate whether the rule should find all duplicates or fail fast at the first duplicate. Defaults to
false. -
ignoreWhenIdentical - when
trueindicates duplicate classes don't fail the build when their bytecode exactly matches each other. Defaults tofalse. -
message - an optional message to provide when duplicates are found.
-
dependencies - a list of dependencies for which you want to ignore specific classes.
-
resolutionScope - optional classpath to resolve:
compile,runtime, ortest. Selects direct dependencies before collection and filters the mediated artifacts for that classpath. ThescopesandignoredScopesfilters only apply to the mediated artifacts to scan. WhenresolutionScopeis omitted, all direct dependencies participate in collection and no classpath filter is applied. -
scopes - a list of scopes (e.g. test, provided) to include when scanning artifacts
-
ignoredScopes - a list of scopes (e.g. test, provided) to ignore when scanning artifacts
-
ignoreOptionals - a boolean, if
trueall dependencies which have<optional>true</optional>are ignored. -
searchTransitive - a boolean, specify if transitive dependencies should be searched (default) or only look at direct dependencies.
Sample Plugin Configuration:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version><!-- latest release --></version> <!-- find the latest version at https://maven.apache.org/plugins/maven-enforcer-plugin/ -->
<executions>
<execution>
<id>enforce-ban-duplicate-classes</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<banDuplicateClasses>
<ignoreClasses>
<!-- example of ignoring one specific class -->
<ignoreClass>com.xyz.i18n.Messages</ignoreClass>
<!-- example of ignoring with wildcards -->
<ignoreClass>org.apache.commons.logging.*</ignoreClass>
</ignoreClasses>
<scopes>
<scope>compile</scope>
<scope>provided</scope>
</scopes>
<findAllDuplicates>true</findAllDuplicates>
<ignoreWhenIdentical>true</ignoreWhenIdentical>
</banDuplicateClasses>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>extra-enforcer-rules</artifactId>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
[...]
</project>
Selecting a Classpath
The resolutionScope selects the direct dependencies used to collect the requested classpath. Maven Resolver applies dependency management and resolves transitive version and scope conflicts within that graph. The scopes and ignoredScopes parameters further select exact dependency scope labels to scan. For example:
<banDuplicateClasses>
<resolutionScope>compile</resolutionScope>
<scopes>
<scope>compile</scope>
</scopes>
</banDuplicateClasses>
Here a direct provided dependency participates in mediation, overriding a transitive declaration of the same artifact. The scopes filter then excludes the provided winner from scanning. The superseded transitive declaration is not scanned.
For a runtime check, set resolutionScope to runtime. A direct provided or test dependency is excluded before collection, allowing a transitive declaration required by the runtime classpath to be resolved and scanned. A direct test dependency is likewise excluded before collecting a compile classpath. With no scan-scope filters, all artifacts in the chosen classpath are checked. searchTransitive and ignoreOptionals remain available to restrict the check.
Ignoring Classes per Dependency
You can even ignore classes per dependency if you'd like. For every dependency you can specify either the groupId, artifactId, type or classifier.
You can't specify the version, because once Maven has resolved the version of a specific dependency there's only one version available.
Note that you can use wildcard-expressions for these values, i.e. a ‘?’ for one character and a ‘*’ for any number of characters.
The configuration would look like this
<configuration>
<rules>
<banDuplicateClasses>
<ignoreClasses>
<ignoreClass>org.slf4j.*</ignoreClass>
</ignoreClasses>
<dependencies>
<dependency>
<artifactId>jcl-over-slf4j</artifactId>
<ignoreClasses>
<ignoreClass>org.apache.commons.logging.impl.*</ignoreClass>
</ignoreClasses>
</dependency>
<dependency>
<groupId>org.*</groupId>
<artifactId>maven-*</artifactId>
<type>*ar</type>
<classifier>sources</classifier>
</dependency>
</dependencies>
</banDuplicateClasses>
</rules>
</configuration>
Inheriting Configuration from Parent POMs
When you want to extend banDuplicateClasses configuration from a parent POM in a child POM, you must use Maven's combine.children="append" attribute on the collection elements (ignoreClasses and dependencies) to append to the parent's configuration rather than replacing it.
Without this attribute, Maven will replace the parent's configuration entirely.
Parent POM Example
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
<banDuplicateClasses>
<ignoreClasses>
<ignoreClass>org.apache.commons.logging.*</ignoreClass>
</ignoreClasses>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<ignoreClasses>
<ignoreClass>org.apache.commons.logging.impl.WeakHashtable*</ignoreClass>
</ignoreClasses>
</dependency>
</dependencies>
</banDuplicateClasses>
</rules>
</configuration>
</plugin>
Child POM Example
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
<banDuplicateClasses>
<ignoreClasses combine.children="append">
<ignoreClass>some.other.Class</ignoreClass>
</ignoreClasses>
<dependencies combine.children="append">
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<ignoreClasses>
<ignoreClass>org.slf4j.*</ignoreClass>
</ignoreClasses>
</dependency>
</dependencies>
</banDuplicateClasses>
</rules>
</configuration>
</plugin>
Note: The combine.children="append" attribute must be placed on the collection element in the child POM (not the parent POM).
See Maven POM Reference for more details on configuration combination.
Trademarks
Apache, Apache Maven, Maven and the Apache feather logo are trademarks of The Apache Software Foundation.


