Is there a way to fail a build based if source code is using a particular class/method?

This is not a gradle specific question, but I’m curious to know if there is a way to wire up a task either before compile or after compile that will analyze the source code or compiled class and fail the build is any of the code is accessing a particular class or method.

The use case I’m thinking of is that I’m exploring the idea of restricting developers from calling either log4j or slf4j error or warning methods directly. Rather, we’ve prefer them to use our own in house abstractions for reporting error conditions which in turn may result in an underlying logging framework from being called.

So I don’t want to fail the build if the module has a dependency on the loggging framework jar files themselves, as its would be perfectly legal to call debug (and maybe info) methods.

I feel this could be done and wrapped in a gradle task, but I’m at a loss as to what underlying framework to use for this.

Maybe a custom plugin is available in gradle to do something like this already.



You might be able to write your own Checkstyle rule for that. Then in the Gradle build you can use the Checkstyle plugin and apply your own rule set.