Maven configuration multi-repository is invalid? Take a look at this article

When using Maven to manage jar package dependencies in a project, the following situations often occur:

1. The default remote central mirroring of domestic access to maven is particularly slow;

2. Use Ali's mirroring instead of remote central mirroring;

3. Some jar packages are missing in Alibaba Cloud image;

4. Use private warehouses and public warehouses at the same time;

In view of the above situation, we need to let Maven support multi-repository configuration.

Separate warehouse configuration

When only one warehouse is configured, the operation is relatively simple. You can directly configure the global configuration in the settings.xml file of Maven. Take the image of Alibaba Cloud as an example:

<mirrors>
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

Just add a mirror configuration. To achieve a single warehouse, set mirrorOf to *.

The asterisk configured in mirrorOf means to match all artifacts, that is, everything uses the proxy address here. The above mirrorOf is configured with a specific name, which refers to the name of the repository.

Mirror configuration instructions:

1. id: the unique identification of the image;

2. name: name description;

3. url: address;

4. MirrorOf: Specify the mirroring rules and under what circumstances will it be pulled from the mirror warehouse. Among them,
*: matches all, all content is pulled from the mirror;

external:*: All pulled from the mirror warehouse except for the local cache;

repo, repo1: repo or repo1, where repo refers to the warehouse ID;

*,!repo1: All repositories except repo1;

Multi-warehouse configuration

So is it enough to configure a few more mirrors for the configuration of multiple warehouses? This configuration will not take effect.

The correct operation is to configure multiple profiles under the profiles node and activate them after configuration.

<profiles>
    <profile>
      <id>boundlessgeo</id> 
      <repositories>
        <repository>
          <id>boundlessgeo</id> 
          <url>https://repo.boundlessgeo.com/main/</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
    <profile>
      <id>aliyun</id> 
      <repositories>
        <repository>
          <id>aliyun</id> 
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile> 
    <profile>
      <id>maven-central</id> 
      <repositories>
        <repository>
          <id>maven-central</id> 
          <url>http://central.maven.org/maven2/</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
<profiles>

Activate by configuring the activeProfiles sub-node:

<activeProfiles>
    <activeProfile>boundlessgeo</activeProfile>
    <activeProfile>aliyun</activeProfile>
    <activeProfile>maven-central</activeProfile>
</activeProfiles>

At this time, if you use the local Maven configuration in Idea, you will see the profile option similar to the following figure in the project's Maven management.

image

When packaging, check the profile used. If you use the Maven command to package the execution command format as follows:

mvn -Paliyun ...

1. If the id of the aliyun warehouse is set to central, it will override the default remote warehouse in maven.

2. Aliyun warehouse can also be configured without configuration, directly configure a mirror warehouse in the mirrors tab, and set the value of mirrorOf to central to override the default warehouse.

Configure mirroring in the project

Adding multiple warehouses to the project is achieved by modifying the pom file in the project.

Idea: Add multiple repository nodes under the repository node of the pom file in the project (if not manually added). Each repository node is a warehouse.

The configuration effect is as follows:

<!-- 特殊maven仓库 -->
<repositories>
    <repository>
        <id>central-repo1</id>
        <name>Maven Repository Switchboard</name>
        <url>http://repo1.maven.org/maven2/</url>
        <layout>default</layout>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

The id here is the ID to be used by mirrorOf.

In the course of practice, it is found that the configuration of the warehouse alone will not take effect. It is necessary to define a warehouse configuration with mirrorOf as central-repo1 in the setting.xml at the same time, which is compared with the id of the configuration.

The control configuration in setting.xml is as follows:

<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>https://repo1.maven.org/maven2/</url>
    <mirrorOf>central-repo1</mirrorOf>
</mirror>

A domestic mirror worth collecting

<mirrors>
     <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/mvn/view</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>jboss-public-repository-group</id>
        <mirrorOf>central</mirrorOf>
        <name>JBoss Public Repository Group</name>
        <url>http://repository.jboss.org/nexus/content/groups/public</url>
    </mirror>
    <mirror>
        <id>ibiblio</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>https://maven.aliyun.com/mvn/view</url>
    </mirror>
    <mirror>
        <id>central</id>
        <name>Maven Repository Switchboard</name>
        <url>http://repo1.maven.org/maven2/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</url>
    </mirror>
</mirrors>

Original link: " Maven configuration of multiple warehouses is invalid? Take a look at the article "

Fine SpringBoot 2.x video tutorial

"Spring Boot 2.x Video Tutorial Family Bucket" , a boutique Spring Boot 2.x video tutorial, to create the most complete set of Spring Boot 2.x video tutorials.


New Vision of Procedure

The public account " New Vision of Program ", a platform that allows you to simultaneously improve your soft power and hard technology, providing massive amounts of data

WeChat Official Account: New Vision of Program

Guess you like

Origin blog.csdn.net/wo541075754/article/details/107782973