Maven Enforcer Plugin to Ban dependency

Maven enforcer plugin has many usages when it comes to managing dependencies. The one I am going to give an example of now, has to do with banning certain dependencies.

The use case is something like this — you have a multi-module maven project. As time has grown your repo has evolved and now you have the need to deprecate a module. You want to make sure that when someone is building using a module — they don’t use the older implementation. The way I have managed it is, using mvn enforcer plugin.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<execution>
<id>enforce-ban-deprecated</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<excludes>
<exclude>com.group1.domain:artefact1</exclude>
</excludes>
<message>Module artefact1 has been deprecated. Please use the new-artefact1 module instead</message>
</bannedDependencies>
<bannedDependencies>
<excludes>
<exclude>com.group1.domain:artefact2</exclude>
</excludes>
<message>Module artefact2 has been deprecated. Please use the newartefact2 module instead</message>
</bannedDependencies>
</rules>
<fail>false</fail>
</configuration>
</execution>
</plugin>

After adding this enforcer rule to the parent pom, no one will be able to add the module artefact1 as a dependency in their new modules. They will received the message with a build failure informing them that they should use new-artefact module instead.