Maven 在尝试下载插件时考虑的存储库顺序是什么?

What is the repository order that is considers by Maven when it tries to download plugins?

我找不到 documentation Maven 说明它如何对声明的存储库进行排序的地方。

考虑以下位置可以配置 Maven 存储库:

Maven 尝试从所有这些存储库下载依赖项的顺序是什么?

mvn dependency:list-repositories 命令向我展示了一个非常无序列表,很难相信这是真正的优先顺序。

我执行了以下操作来确定 Maven 考虑存储库顺序的顺序:

  • 我将所有 XML 文件中所有 repository/mirror 设置的 URL 更新为无效的 Maven 存储库,以强制 Maven 失败
  • 我运行mvn compile

以下结果假设某个依赖项从未 downloaded/registered 在本地缓存 ~/.m2/repository 中(即它未被缓存或之前使用其他存储库未失败)。如果您在本地缓存中有某个依赖项的条目,Maven 将重用该缓存条目(它还保存了初始源存储库)以尝试再次获取它。

当没有配置镜像时,Maven 会尝试这样做:

Downloading from settings-repo1: http://settings-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from settings-repo2: http://settings-repo2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-child-repo1: http://child-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-child-repo2: http://child-repo2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-parent-repo1: http://parent-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-parent-repo2: http://parent-repo2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom

并且当配置了镜像时:

Downloading from settings-repo1: http://settings-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from settings-repo2: http://settings2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from settings-mirror1: http://settings-mirror1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from settings-mirror2: http://settings-mirror2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-parent-repo1: http://parent-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-parent-repo2: http://parent-repo2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom

所以顺序必须是:

  • settings.xml
  • 当前项目的pom.xml
  • 父项目的pom.xml

如果配置了任何镜像,它们将仅替换由 mirrorOf 元素标识的原始列表中的相应存储库。如果 mirrorOf 表达式标识多个存储库,则只会尝试第一次出现(在没有配置镜像的 Maven 原始顺序中)。例如,如果有一个镜像(例如 settings-mirror1)配置为:<mirrorOf>pom-parent-repo2,pom-child-repo1</mirrorOf> 存储库搜索顺序将是:

Downloading from settings-repo1: http://settings-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from settings-repo2: http://settings-repo2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from settings-mirror1: http://settings-mirror1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-child-repo2: http://child-repo2.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from pom-parent-repo1: http://parent-repo1.org/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/httpcomponents/httpclient1/4.5.6/httpclient1-4.5.6.pom

至于依赖POM文件中声明的存储库,我认为应该不关心它们。如果你有一个依赖项,这需要在声明的存储库之一(设置、父 pom、pom)中可用,否则构建将失败(即使你之前已经由另一个项目下载并缓存了该依赖项(子依赖项或非子依赖项) ).