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

Viewed 4905

First off, I searched google but cant seem to find an answer for this. Apologies if its an obvious answer.

In maven we can define 0 or more repositories where it looks for resources. Repositories can be defined in settings.xml or within your pom. By default if you define no repositories everything will come from a repository name 'central' which is just the default one maintained by maven.

The below setup pom snippet comes from the jboss eap sample apps. When I do a mvn clean install I can see some things are pulled from central and some from the jboss repos. There does not seem to be anything in the dependency tag that tells maven which repository contains the dependency so how does it decide? Is it some how tied to group ID in the dependency or does maven just check all the repos one by one till it finds the first one that contains the 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>
2 Answers

Maven just goes through the list and looks into all the repositories.

It is not possible to tie dependencies to special repositories.

Lets take a look at the 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.

Related