Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Mirror in Maven settings.xml file?

Tags:

maven

Correct me if I'm wrong, but a Mirror is used to redirect all traffic to a specific repository URL and block everything else (including Maven central repo).

Now what if I have a Mirror to http://a.com:8081 and a repository to http://b.com:8081. Would the second URL ever get used? I assume the answer is a No. Can you have multiple Mirrors?

like image 941
Dark Night Avatar asked Apr 21 '16 01:04

Dark Night


People also ask

What is settings xml file in Maven?

A Maven settings. xml file defines values that configure Maven execution in various ways. Most commonly, it is used to define a local repository location, alternate remote repository servers, and authentication information for private repositories.

What should I put in settings xml?

The settings. xml should provide the remote artifact repository's deployment information, such as URL, user name, and password.

What is difference between POM xml and settings xml?

settings. xml contains system and/or user configuration, while the pom. xml contains project information. All build configuration ends up in the pom.


2 Answers

Correct me if I'm wrong, but a Mirror is used to redirect all traffic to a specific repository URL and block everything else (including Maven central repo).

This is not entirely correct. Artifacts are downloaded by Maven by searching them inside defined repositories. Those repositories are defined in project's POM or in the Maven settings. But, as specified in the Mirror documentation:

However, you may want to use an alternative mirror for a particular repository without changing the project files.

Let's take the example from the documentation and comment on it:

<mirrors>   <mirror>     <id>UK</id>     <name>UK Central</name>     <url>http://uk.maven.org/maven2</url>     <mirrorOf>central</mirrorOf>   </mirror> </mirrors> 

This defines a single mirror that will be used when Maven will want to fetch a dependency from the Maven Central Repository. Let's say you have 2 current repositories, which are Maven Central and a custom repo A. What happens is the following:

  1. You are declaring a dependency to an artifact;
  2. Maven will look for that dependency inside your defined repositories, so it will look for it inside the repository A and Maven Central;
  3. Let's say it starts with A: it will hit the URL defined for that repository, like you would expect. Then let's say it wasn't found there;
  4. It will then look inside Maven Central. However, Maven will notice that a mirror was configured for that repository. It will not hit Maven Central at repo1.maven.org. Instead, it will fetch it from uk.maven.org, which is the URL defined in the mirror configuration element.

As this example shows, a mirror only applies to certain repositories, which are defined with the <mirrorOf> configuration element. In the previous case, we had <mirrorOf>central</mirrorOf> which means "Mirror Maven Central". This element can have various values, and the documentation gives examples:

Examples:

  • * = everything
  • external:* = everything not on the localhost and not file based.
  • repo,repo1 = repo or repo1
  • *,!repo1 = everything except repo1:

Now what if I have a Mirror to http://a.com:8081 and a repository to http://b.com:8081. Would the second URL ever get used? I assume the answer is a No.

The above shows that the answer is not strictly no. The mirror to http://a.com:8081 will be used when Maven will try to fetch a dependency from one of the repository that it mirrors. If it indeed mirrors the repository to http://b.com:8081 then no requests will ever be made to http://b.com:8081 (they will be redirected to http://a.com:8081); but if it doesn't, Maven will continue to fetch dependencies from http://b.com:8081 like usual.


Can you have multiple Mirrors?

Yes, you can.

like image 87
Tunaki Avatar answered Sep 28 '22 00:09

Tunaki


To answer your questions:

Correct me if I'm wrong, but a Mirror is used to redirect all traffic to a specific repository URL and block everything else (including Maven central repo).

Kind of right, but I would say all artifacts are redirected to the mirror.

The blocking doesn't happen because it's a mirror. The filtering or re-direction happens based on what is defined in the <mirrorOf> element. The syntax <mirrofOf>*</mirrorOf> causes ALL repositories to be directed to that mirror. To use your words, the mirror blocks the other repository http://b.com:8081. (but I wouldn't use those words).

If, however, you defined the mirror pattern as: <mirrofOf>*,!third-party-repo</mirrorOf> then the mirror would handle all artifacts that were not found in the other repositories. So in this case (in your words), the mirror would not block access to the other repositories.

Now what if I have a Mirror to http://a.com:8081 and a repository to http://b.com:8081. Would the second URL ever get used? I assume the answer is a No.

This all depends on how you've defined your mirror and repositories as explained above, it is possible for them both to be used.

Can you have multiple Mirrors?

Yes. The maven docs explain how to do that here: https://maven.apache.org/guides/mini/guide-mirror-settings.html#advanced-mirror-specification

But I've never personally had the need to deal with multiple mirrors. Instead, I typically have one mirror and then one or more repos. I'll explain that next.

Example using a mirror and two repos

In this example, I have:

  1. One public mirror of Maven central (called acme-central) for my fictitious company Acme.

  2. One repo for my companies locally developed artifacts (called acme-repo).

  3. One repo for a 3rd party company third-party-repo that has some artifacts I needed (called third-party-repo).

The simplified syntax for all of this is shown below. I'm only showing the bits of the configuration that affect this example.

<settings>
    <mirrors>
      <mirror>
    <mirrorOf>central,!acme-repo,!third-party-repo</mirrorOf>

    <profiles>
        <profile>
        <id>default</id>
        <repositories>
            <repository>
                <id>acme-repo</id>
                <url>https://acme.com/repository/releases</url>

            <repository>
                <id>third-party-repo</id>
                <url>https://third-party.com/repository/releases</url>

    <activeProfiles>
      <activeProfile>default</activeProfile>

So we have a mirror (of maven central), and two other repositories. The mirror statement says to use the mirror instead of maven central, but not for the two repos acme-repo and third-party-repo.

The profile is required (just because that is the syntax of the settings.xml file). You can only define <repositories> within a profile.

The repositories have names, and the way artifacts are looked up is as follows (see Repository Order in Maven docs). What follows is simplified for this answer.

  1. settings.xml file is used
  2. pom.xml file is used

Before downloading from a repository, mirrors configuration is applied. The maven docs show an example with two <mirror>s.

Running the command mvn help:effective-pom -Dverbose is a good way to see which repo/mirror was used to download an artifact because the syntax is:

Downloading from third-party-repo: https://third-party.com/...
Downloading from acme-repo: https://third-party.com/...

like image 30
PatS Avatar answered Sep 28 '22 02:09

PatS