Force Repository Update with Maven – 用Maven强制更新版本库

最后修改: 2021年 7月 15日

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

1. Overview

1.概述

In this tutorial, we’ll learn how to force-update a local Maven repository when it becomes corrupted. To accomplish this, we’ll use a straightforward example to understand why a repository can become corrupted and how to fix it.

在本教程中,我们将学习如何在本地Maven版本库损坏时强制更新它。为了达到这个目的,我们将用一个简单的例子来理解为什么版本库会损坏,以及如何修复它。

2. Prerequisites

2.前提条件

To learn and run the commands in this tutorial, we need to use a Spring Initializr project and have a JDK and Maven installed.

要学习和运行本教程中的命令,我们需要使用Spring Initializr项目,并安装有JDK和Maven。

3. Maven Repository Structure

3.Maven资源库结构

Maven saves all the dependencies of projects in the .m2 folder. For example, in the following image, we can observe the structure of Maven repositories:

Maven将项目的所有依赖项保存在.m2文件夹中。例如,在下图中,我们可以观察到Maven资源库的结构。

As we can see, Maven downloads all dependencies under the repository folder. Therefore, the download into our local repository is necessary to access all needed code at runtime.

正如我们所见,Maven在repository文件夹下下载所有依赖项。因此,下载到我们的本地仓库是必要的,以便在运行时访问所有需要的代码。

4. Downloading Dependencies

4.下载依赖性

We know that Maven works based on the pom.xml file configurations. When Maven executes this pom.xml file, the dependencies will be downloaded from a central Maven repository and put into your local Maven repository. If we already have the dependencies in our local repository, Maven will not download them.

我们知道,Maven是基于pom.xml文件的配置工作的。当Maven执行这个pom.xml文件时,依赖项将从中央Maven仓库下载并放入你的本地 Maven仓库。如果我们的本地仓库中已经有了依赖项,Maven就不会下载它们。

The download happens when we execute the following commands:

当我们执行以下命令时,下载就会发生。

mvn package
mvn install

Both of the above include executing the following command:

上述两种情况包括执行以下命令。

mvn dependency:resolve 

Therefore, we can solely resolve the dependencies without using a package or install by just running the dependency:resolve command.

因此,我们只需运行dependency:resolve命令,就可以在不使用包或安装的情况下解决依赖关系。

5. Corrupted Dependencies

5.损坏的依赖关系

While downloading the dependencies, a network glitch can happen, resulting in corrupted dependencies. The interrupted dependency download is the leading cause of corruption. Maven will display a message accordingly:

在下载依赖关系时,可能会发生网络故障,导致依赖关系被破坏。依赖品下载中断是导致损坏的主要原因。Maven会相应地显示一条信息。

Could not resolve dependencies for project ...

无法解决项目的依赖关系 ...

Let’s see next how we can solve this issue.

接下来让我们看看如何解决这个问题。

6. Automatically Fixing the Corrupted Dependencies

6.自动修复被破坏的依赖关系

Maven usually displays the corrupted dependency when it notifies that the build failed:

Maven在通知构建失败时通常会显示损坏的依赖关系。

Could not transfer artifact [artifact-name-here] ...

无法转移工件[工件名称-此处]...

To fix this, we can have automatic or manual approaches. Additionally, we should run any repository update in debug mode, adding the -X option after -U to have a better picture of what happens during the update.

为了解决这个问题,我们可以采用自动或手动的方法。此外,我们应该在调试模式下运行任何版本库的更新,在-U之后添加-X选项,以便更好地了解更新过程中发生的情况。

6.1. Force Update All SNAPSHOT Dependencies

6.1.强制更新所有SNAPSHOT的依赖关系

As we know already, Maven will not download existing dependencies again. Therefore, to force Maven to update all corrupted SNAPSHOT dependencies, we should add in our command the  -U/–update-snapshots option:

我们已经知道,Maven不会再次下载现有依赖。因此,为了强迫Maven更新所有损坏的SNAPSHOT依赖,我们应该在命令中加入 -U/-update-snapshots选项。

mvn package -U
mvn install -U

Still, we have to remember that the option does not re-download a SNAPSHOT dependency if Maven already downloaded it and if the checksum is the same.

但我们必须记住,该选项 如果Maven已经下载了SNAPSHOT依赖,且校验和相同,则不会重新下载。

This will also package or install our project next. Finally, we’ll learn how to update the repository without including the current working project.

这也会在接下来打包或安装我们的项目。最后,我们将学习如何在不包括当前工作项目的情况下更新版本库。

6.2. Dependency Resolve Goal

6.2.依赖性解决的目标

We can tell Maven to resolve our dependencies and update snapshots without any package or install command. For this purpose, we’ll use the dependency:resolve goal, by including the -U option:

我们可以告诉Maven解决我们的依赖关系并更新快照,而不需要任何打包或安装命令。为此,我们将使用dependency:resolve目标,包括-U选项。

mvn dependency:resolve -U

6.3. Purge Local Repository Goal

6.3.清除本地存储库目标

We know that -U  just re-downloads the corrupted SNAPSHOT dependencies. As a result, in the case of corrupted local release dependencies, we might need deeper commands. For this purpose, we should use:

我们知道,-U只是重新下载损坏的SNAPSHOT依赖。因此,在本地发布依赖关系被破坏的情况下,我们可能需要更深入的命令。为了这个目的,我们应该使用

mvn dependency:purge-local-repository

The purpose of the dependency:purge-local-repository is to purge (delete and optionally re-resolve) artifacts from the local Maven repository. By default, artifacts will be re-resolved.

dependency:purge-local-repository的目的是清除(删除并可重新解析)本地Maven资源库中的工件。默认情况下,工件将被重新解析。

6.4. Purge Local Repository Options

6.4.清除本地存储库选项

The purge local repository can be configured to run for only a certain group of dependencies by specifying the “groupId” for the resolutionFuzziness option, and the exact group ID to search for using the include option:

通过为resolutionFuzziness选项指定”groupId”,以及使用include选项搜索确切的组ID,可以将清除本地版本库配置为只针对某一组依赖关系运行。

mvn dependency:purge-local-repository -Dinclude:org.slf4j -DresolutionFuzziness=groupId -Dverbose

The resolutionFuzziness option can have the values: version, artifactId, groupId, file.

resolutionFuzziness选项可以有以下值。version, artifactId, groupId, file

The example above will search for and purge all artifacts in the org.slf4j group. We’ve also set the verbose option so we can see detailed logs of the artifacts deleted.

上面的例子将搜索并清除org.slf4j组中的所有工件。我们还设置了verbose选项,所以我们可以看到删除工件的详细日志。

If any files that match the condition are found, the log will display a text like:

如果发现任何符合条件的文件,日志将显示类似的文字。

Deleting 2 transitive dependencies for project [...] with artifact groupId resolution fuzziness
[INFO] Purging artifact: org.slf4j:jul-to-slf4j:jar:1.7.31
Purging artifact: org.slf4j:slf4j-api:jar:1.7.31 

Note that to specify the artifacts included or excluded for deletion or refresh, we can use the options include/exclude:

注意,为了指定包括或排除删除或刷新的工件,我们可以使用选项include/exclude:

mvn dependency:purge-local-repository -Dinclude=com.yyy.projectA:projectB -Dexclude=com.yyy.projectA:projectC

7. Manually Deleting the Repository

7.手动删除版本库

Whereas the -U option and purge-local-repository might resolve the corrupted dependencies without refreshing all of them, the manual delete of the .m2 local repository will cause a forced re-download of all dependencies.

虽然-U选项和purge-local-repository可能会在不刷新所有依赖项的情况下解决损坏的依赖项,但手动删除.m2本地仓库将导致强制重新下载所有依赖项。

This can be useful when having old and maybe corrupted dependencies. Then a simple re-package or re-install should do the job. Moreover, we can use the dependency:resolve option to solve the dependencies of our project solely.

这在有旧的和可能被破坏的依赖关系时可能是有用的。那么简单的重新打包或重新安装就能完成工作。此外,我们可以使用dependency:resolve选项来解决我们项目的依赖关系。

8. Conclusion

8.结语

In this tutorial, we discussed the options and goals of Maven that forcibly updates our local repository.

在本教程中,我们讨论了强行更新我们本地仓库的Maven的选项和目标。

The samples used are simple Maven commands that can be used on any project with a proper pom.xml configured.

使用的样本是简单的Maven命令,可用于任何配置了适当pom.xml的项目。