Find Unused Maven Dependencies – 查找未使用的Maven依赖项

最后修改: 2020年 5月 5日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

使用Maven管理项目的依赖关系时,我们可能会失去对应用程序中使用的依赖关系的跟踪。

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.

在这个简短的教程中,我们将介绍如何使用Maven依赖性插件,这个插件可以帮助我们找到项目中未使用的依赖项。

2. Project Setup

2.项目设置

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):

让我们首先添加几个依赖项, slf4j-api(我们将要使用的那个)和 common-collections(我们不会使用的那个)。

<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:

我们可以访问Maven依赖性插件,而无需在pom中指定。在任何情况下,我们都可以使用pom.xml定义来指定版本,必要时还可以指定一些属性。

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

3. Code Sample

3.代码样本

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.

来自Slf4J库的LoggerFactory在一个方法中被返回,但没有使用通用收集库,使其成为删除的候选者。

4. Find Unused Dependencies

4.查找未使用的依赖关系

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:

使用Maven依赖性插件,我们可以找到项目中未使用的依赖性。为此,我们调用了该插件的分析目标。

$ 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.

对于我们项目中没有使用的每一个依赖,Maven都在分析报告中发出警告。

5. Specify Dependencies as Used

5.指定使用的依赖性

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.

由于依赖关系在编译时没有被使用,maven-dependency-plugin会发出警告,称某一依赖关系没有被使用,而事实上它被使用了。为此,我们可以强制要求并指示该插件正在使用某个库。

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

我们通过在usedDependencies属性中列出依赖关系来做到这一点。

<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.

再次运行analyze目标,我们看到报告中不再考虑未使用的依赖关系了。

6. Conclusion

6.结语

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.

在这个简短的教程中,我们学习了如何找到未使用的maven依赖项。定期检查未使用的依赖关系是个好办法,因为它能提高可维护性,减少项目的库容量。

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

一如既往,包含所有示例的完整源代码可在GitHub上获得