Multiple repositories

Each Maven project has its own effective POM file. The effective POM file is the aggregated POM file from the application POM, all parent POM files, and the super POM. Finally, what matters to Maven is the effective POM, not the individual ones. Each individual POM file can have its own repositories defined under the repositories section, but in the effective POM file, there will be one single repositories section, which aggregates all the repositories defined in each POM file.

Note

More details about Maven POM files were discussed in Chapter 2, Demystifying Project Object Model.

When you have multiple repositories defined in the POM, the order in which they are defined matters. Whenever Maven detects that a required artifact is missing in the local repository, it will try to download from the very first eligible repository defined in the effective POM file. When Maven generates the effective POM, the top repositories will be taken from the application POM, then from the parent POM files and finally from the super POM. Maven will move down the repositories in the order they are defined in the effective POM if it cannot find an artifact in a given repository, as shown in the following figure:

Multiple repositories

Repositories in settings.xml

You can also define repositories in settings.xml, which is available by default under the USER_HOME/.m2 directory. The repositories defined in settings.xml will get priority over all the other repositories. The following explains how to add plugin repositories to settings.xml:

  1. Open USER_HOME/.m2/settings.xml and look for the <profiles> element.
  2. Add the following section under the <profiles> element. When you define repositories in settings.xml, they must be within a profile element. This configuration introduces two plugin repositories and both are defined as snapshot repositories. We will talk more about Maven profiles in Chapter 9, Best Practices:
    <profile>
      <id>apache</id>
      <pluginRepositories>
        <pluginRepository>
          <id>apache.snapshots</id>
          <name>Apache Snapshot Repository</name>
          <url>http://repository.apache.org/snapshots</url>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
          </snapshots>
          <releases>
            <enabled>false</enabled>
          </releases>
        </pluginRepository>
    
        <pluginRepository>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
          </snapshots>
          <id>apache-snapshots</id>
          <name>Apache Snapshots Repository</name>
          <url>http://people.apache.org/repo/m2-snapshot-repository</url>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  3. Go to a directory that has any pom.xml file and execute the following command, which will display the effective POM. Here, we are executing the effective-pom goal of the help Maven plugin with an additional argument, which starts with –P. The –P tag needs to be post fixed with the name of the profile defined in the settings.xml, where we have our plugin repositories. In this case, the name of the profile is apache(<id>apache</id> ):
    $ mvn help:effective-pom -Papache
    
  4. Assuming that you have no repositories defined in your application POM file, the above command will display the following. This includes repositories from the settings.xml as well as from the super POM:
    <repositories>
      <repository>
        <snapshots>
          <enabled>false</enabled>
        </snapshots>
          <id>central</id>
          <name>Central Repository</name>
          <url>http://repo.maven.apache.org/maven2</url>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <releases>
          <enabled>false</enabled>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>daily</updatePolicy>
        </snapshots>
        <id>apache.snapshots</id>
        <name>Apache Snapshot Repository</name>
        <url>http://repository.apache.org/snapshots</url>
      </pluginRepository>
      <pluginRepository>
        <releases>
          <enabled>false</enabled>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>daily</updatePolicy>
        </snapshots>
        <id>apache-snapshots</id>
        <name>Apache Snapshots Repository</name>
        <url>http://people.apache.org/repo/m2-snapshot-repository</url>
      </pluginRepository>
      <pluginRepository>
        <releases>
          <updatePolicy>never</updatePolicy>
        </releases>
        <snapshots>
          <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
      </pluginRepository>
    </pluginRepositories>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.222