Clearing the Maven Cache – 清除Maven缓存

最后修改: 2021年 3月 14日

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

1. Overview

1.概述

In this short tutorial, we’ll explore ways to clear our local Maven cache. We may want to do this in order to save disk space or clear up artifacts that we no longer reference.

在这个简短的教程中,我们将探讨清除本地Maven缓存的方法。我们可能想这样做,以节省磁盘空间或清除我们不再引用的工件。

We’ll first clear the cache manually, where we physically delete the directory. Then, we’ll clear our cache using the Maven Dependency Plugin, using some of the different plugin options available to us.

我们首先手动清除缓存,即物理删除该目录。然后,我们将使用Maven依赖性插件清除缓存,使用一些不同的插件选项。

2. Deleting the Local Cache Directory

2.删除本地缓存目录

Our local Maven repositories are stored in different locations based on the operating system. Also, as the .m2 directory is likely to be hidden, we’ll need to change the directory properties in order to display it.

我们的本地Maven仓库根据操作系统存储在不同位置。另外,由于.m2目录可能是隐藏的,我们需要改变目录属性,以便显示它。

In Windows, the default location is:

在Windows中,默认位置是。

C:\Users\<user_name>\.m2

And on the Mac:

而在Mac上。

/Users/<user_name>/.m2

And on Linux-based systems:

而在基于Linux的系统上。

/home/<user_name>/.m2

Once we locate the directory, we can simply delete the folder .m2/repository. With Unix-based systems like MacOS or Linux, we can delete the directory with one command:

一旦我们找到这个目录,我们就可以简单地删除文件夹.m2/repository. 对于基于Unix的系统,如MacOS或Linux,我们可以用一个命令删除这个目录。

rm -rf ~/.m2/repository

If our cache directory isn’t in the default location, we can use the Maven Help Plugin to locate it:

如果我们的缓存目录不在默认位置,我们可以使用Maven帮助插件来定位它。

mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout

3. Using the Maven Dependency Plugin

3.使用Maven依赖性插件

Instead of deleting the cache directory directly, we can use the Maven Dependency Plugin with the purge-local-repository goal.

我们可以使用带有purge-local-repository目标的Maven依赖性插件来代替直接删除缓存目录的做法。

First, we need to navigate to the root of our Maven project. Then, we can run:

首先,我们需要导航到Maven项目的根目录。然后,我们可以运行。

mvn dependency:purge-local-repository

When we run this plugin without any additional flags, it may download artifacts that aren’t present in our cache to resolve the dependency tree. This is known as transitive dependency resolution. Next, it deletes our local cache and, finally, re-downloads the artifacts.

当我们在没有任何额外标志的情况下运行这个插件时,它可能会下载不存在于我们缓存中的工件来解决依赖树。这就是所谓的过渡性依赖解析。接下来,它删除了我们的本地缓存,最后,重新下载工件。

Alternatively, in order to delete our cache and avoid the first step of pre-downloading the missing dependencies, we can pass in the flag actTransitively=false:

另外,为了删除我们的缓存,避免第一步预先下载缺少的依赖,我们可以传入标志actTransitively=false

mvn dependency:purge-local-repository -DactTransitively=false

Finally, if we just want to clear out our cache, without pre-downloading or re-resolving the artifacts:

最后,如果我们只是想清除我们的缓存,而不预先下载或重新解析工件

mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false

Here, we pass in an additional flag of reResolve=false, which tells the plugin to avoid re-downloading the dependencies.

在这里,我们传入一个额外的标志reResolve=false,它告诉插件避免重新下载依赖。

4. Conclusion

4.总结

In this short article, we looked at two ways to clear our local Maven cache.

在这篇短文中,我们研究了两种清除本地Maven缓存的方法。

First, we looked at manually emptying our local cache directory. Then, we used the Maven Dependency Plugin, exploring different options to achieve our desired outcome.

首先,我们研究了手动清空本地缓存目录的问题。然后,我们使用了Maven依赖性插件,探索了不同的选项,以达到我们想要的结果。