maven 如何知道哪个 repo 用于依赖项?

How does maven know which repo to use for a dependency?

首先,我搜索了 google 但似乎找不到答案。如果这是一个明显的答案,我们深表歉意。

在 maven 中我们可以定义 0 个或多个存储库来查找资源。存储库可以在 settings.xml 或您的 pom.xml 中定义。默认情况下,如果您没有定义存储库,则所有内容都将来自存储库名称 'central',这只是 maven 维护的默认存储库。

以下设置 pom 片段来自 jboss eap 示例应用程序。当我执行 mvn clean install 时,我可以看到有些东西是从中央提取的,有些是从 jboss 存储库中提取的。依赖标签中似乎没有任何内容告诉 maven 哪个存储库包含依赖项,那么它是如何决定的呢?它是如何与依赖项中的组 ID 绑定的,还是 Maven 只是一个接一个地检查所有回购协议,直到找到第一个包含 jar 的回购协议?

<dependencies>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jboss.spec.javax.xml.bind</groupId>
        <artifactId>jboss-jaxb-api_2.3_spec</artifactId>
    </dependency>
</dependencies>


   <repositories>
        <repository>
            <id>jboss-enterprise-maven-repository</id>
            <url>https://maven.repository.redhat.com/qa/</url>
        </repository>
        <repository>
            <id>jboss-enterprise-maven-repository-ea</id>
            <url>https://maven.repository.redhat.com/earlyaccess/all/</url>
        </repository>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

Maven 只是浏览列表并查看所有存储库。

无法将依赖关系绑定到特殊存储库。

让我们看看 official documentation:

Remote repository URLs are queried in the following order for artifacts until one returns a valid result:

  1. Effective settings:
  • Global settings.xml
  • User settings.xml
  1. Local effective build POM:
  • Local pom.xml
  • Parent POMs, recursively
  • Super POM
  1. Effective POMs from dependency path to the artifact.

For each of these locations, the repositories within the profiles are queried first in the order outlined at Introduction to build profiles.

Before downloading from a repository, mirrors configuration is applied.

Effective settings and local build POM, with profile taken into account, can easily be reviewed to see their repositories order with mvn help:effective-settings and mvn help:effective-pom -Dverbose.