Maven enforcer rule to manage source level dependencies

- 1 min

The maven-enforcer-plugin allows to enforce certain conditions that must be met in order for the build to succeed. The plugin itself is customizable via so called rules. For example there is a rule (which is shipped per default) that allows to restrict the usage of unwanted dependencies. This default dependency check works on artifact level, meaning that you can restrict whole artifacts from being used as dependency.

There are cases however, where you have to include a certain artifact but you want use only certain packages or classes from it. Or you happen to have a classpath crowded with certain transitive dependencies which you do not want to be used directly in your code base. A common example are logging frameworks: if you maintain a bigger project you likely have at least three different classes called Logger on your classpath and your IDE happily suggests them all for auto completion. As it is good practice to use slf4j as a facade, you might want to enforce that the developers do not use java.util.logging.Logger but only org.slf4j.Logger.

Using the restrict-imports-enforcer-rule extension you have fine grained control over the classes/packages that can be referenced from your project. Actively maintaining dependencies is one sign of a well-conceived and clean code base.

Check out the full documentation on GitHub

rss facebook twitter github gitlab dev youtube mail spotify lastfm instagram linkedin xing google google-plus pinterest medium vimeo stackoverflow reddit quora quora