1. Overview

When using Maven to manage our project dependencies, we can lose track of what dependencies are used in our application.

In this short tutorial, we'll cover how to use the Maven dependency plugin, a plugin that helps us find unused dependencies in our project.

2. Project Setup

Let's begin by adding a couple of dependencies, slf4j-api (the one we will be using) and common-collections (the one we won't use):

<dependencies>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
</dependencies>

We can access the Maven dependency plugin without specifying it in our pom. In any case, we can use the pom.xml definition to specify the version and also some properties if needed:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
        </plugin>
    </plugins>
</build>

3. Code Sample

Now that we have our project set up, let's write a line of code where we use one of the dependencies we defined before:

public Logger getLogger() {
    return LoggerFactory.getLogger(UnusedDependenciesExample.class);
}

The LoggerFactory from the Slf4J library is returned in a method, but there's no use of the common-collections library, making it a candidate for removal.

4. Find Unused Dependencies

Using the Maven dependency plugin, we can find the dependencies that are not in use in our project. For that, we invoke the analyze goal of the plugin:

$ mvn dependency:analyze

[INFO] --- maven-dependency-plugin:3.1.1:analyze (default-cli) @ maven-unused-dependencies ---
[WARNING] Unused declared dependencies found:
[WARNING]    commons-collections:commons-collections:jar:3.2.2:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.225 s
[INFO] Finished at: 2020-04-01T16:10:25-04:00
[INFO] ------------------------------------------------------------------------

For every dependency that is not in use in our project, Maven issues a warning in the analysis report.

5. Specify Dependencies as Used

Depending on the nature of the project, sometimes we might need to load classes at runtime, like for example in a plugin oriented project.

Since the dependencies are not used at compile-time, the maven-dependency-plugin would issue a warning stating that a dependency is not being used, when in fact it is. For that, we can enforce and instruct the plugin that a library is being used.

We do this by listing the dependencies inside the usedDependencies property:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <usedDependencies>
            <dependency>commons-collections:commons-collections</dependency>
        </usedDependencies>
    </configuration>
</plugin>

Running the analyze goal again, we see that the unused dependency is no longer considered in the report.

6. Conclusion

In this short tutorial, we learned how to find unused maven dependencies. It's a good practice to check for unused dependencies regularly since it improves maintainability and reduces the library size of our project.

As always, the full source code with all examples is available over on GitHub.