What is DepClean?
DepClean is a tool to automatically remove dependencies that are included in your Java dependency tree but are not actually used in the project’s code. DepClean detects and removes all the unused dependencies declared in the
pom.xml file of a project or imported from its parent. For that, it relies on bytecode static analysis and extends the
maven-dependency-analyze plugin (more details on this plugin). DepClean does not modify the original source code of the application nor its original
pom.xml. It can be executed as a Maven goal through the command line or integrated directly into the Maven build lifecycle.
How does it work?
DepClean runs before executing the
package phase of the Maven build lifecycle. It statically collects all the types referenced in the project under analysis as well as in its declared dependencies. Then, it compares the types that the project actually use in the bytecode with respect to the class members belonging to its dependencies.
With this usage information, DepClean constructs a new
pom.xml based on the following steps:
- add all used transitive dependencies as direct dependencies
- remove all unused direct dependencies
- exclude all unused transitive dependencies
If all the tests pass and the project builds correctly after these changes, then it means that the dependencies identified as bloated can be removed. DepClean produces a file named
pom-debloated.xml, located in the root of the project, which is a clean version of the original
pom.xml without bloated dependencies.
You can configure the
pom.xml file of your Maven project to use DepClean as part of the build:
<plugin> <groupId>se.kth.castor</groupId> <artifactId>depclean-maven-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <goals> <goal>depclean</goal> </goals> </execution> </executions> </plugin>
The Maven plugin can be configured with the following additional parameters.
||Add a list of dependencies, identified by their coordinates, to be ignored by DepClean during the analysis and considered as used dependencies. Useful to override incomplete result caused by bytecode-level analysis. Dependency format is
||If this is true, DepClean creates a debloated version of the pom without unused dependencies called
||If this is true, and DepClean reported any unused dependency in the dependency tree, the build fails immediately after running DepClean. Default value is:
||If this is true, DepClean creates a debloated version of the pom without unused dependencies clled “debloated-pom.xml”, in root of the project. Default value is:
||Skip plugin execution completely. Default value is:
Installing and building from source
In a terminal clone the repository and switch to the cloned folder:
git clone https://github.com/castor-software/depclean.git cd depclean
Then run the following Maven command to build the application and install the plugin locally:
mvn clean install
Once the plugin is installed, you can execute the plugin goal directly in the command line:
Distributed under the MIT License. See LICENSE for more information.