How to prevent usage of certain classes in new code in Maven project?

159 views Asked by At

There is a class abc.something.MyComponent that is used by some legacy code, but we want to avoid using it in newly created code.

I'm thinking about something like maven-enforcer-plugin with additional option to create a list of "allowed violations", because it is not possible to remove all usages instantly.

What is appropriate approach to add rules of a certain class usage in Java project?

1

There are 1 answers

1
rolve On

One way is to use Checkstyle's IllegalType check. Checkstyle can easily be integrated in a Maven build using the Maven Checkstyle plugin. It can be configured to simply create a report or to run its checks during the normal build and even fail the build if any violation is found.

Here's how the general configuration would look like:

<module name="IllegalType">
  <property name="illegalClassNames" value="abc.something.MyComponent"/>
</module>

If you want to suppress violations about specific uses in legacy code, you could further configure "suppressions", as shown here. You can use file patterns and also restrict the suppressions to specific checks.