Maven: find not used dependencies

If you work with legacy project you may find it useful to analyze and find unused dependencies added in pom.xml file. Possible way to achieve it is using Maven Dependency Plugin. Let's take a look at short example where we will add a new dependency and identify it as redundant.

Let's say we already have maven project. Now we need an "old an unused" dependency. We will add some :) For example:

 <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.0</version>
 </dependency>

Now it's time to find it. We start with adding Maven Dependency Plugin to the pom.xml:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>analyze</id>
            <goals>
                <goal>analyze-only</goal>
            </goals>
        </execution>
    </executions>
 </plugin>

when you execute mvn dependency:analyze goal, the output will contain just added but not used dependency, in our case:



For more information about plugin refer to the official documentation. Hopefully you found this short example helpful.

 

Comments