Find unused code in a Maven modularised project

when I run it on one of the modules it ignores completely the fact that other modules might be using some of the public members...

Yes, that's the problem, and that's why there is no real deterministic way to do find unused code as reminded by @cletus in this previous answer.

Having that said, tools like PMD (and its unusedcode rule), Findbugs may help anyway. IDEs like IntelliJ (Java code inspections are fully available in the Community Edition) and Eclipse also have good support for this.

For IntelliJ, have a look at Global unused declaration inspection:

Thanks to improvements in the internal indexes behind the Intellij IDEA code insight engine, Maia will be able to instantly highlight some java classes, methods and fields which are unused across the entire project.

For Eclipse, there is the UCDetector plugin:

UCDetector (Unecessary Code Detector) is an Open Source eclipse PlugIn Tool to find unecessary (dead) public java code. It also tries to make code final, protected or private.

alt text

But I confess that I'm not sure if any of these solution will really work across modules. In that case, my suggestion would be to put all the code in one "janitor" project (yeah, this is ugly but well...) and to run the tools on it (and to clean modules based on the obtained results).


I've used the Core Tools plugin to find unused code across many different maven modules. The build path for each of the projects has to be set up properly so that Eclipse knows that there are dependencies between projects. If you import the projects using the m2eclipse plugin, it will set these up automatically.

I think the way that the Core Tools plugin works is that it automates the process that the "Call Hierarchy" view performs. Select a method and run "Call Hierarchy" (ctrl-alt-H) if there are no callers then Core Tools should mark the method as uncalled.