Maven has always been configured as a private server repository for the company. Today, we found that the JMH package could not be installed. Therefore, we are considering configuring multiple repositories to meet the needs of work and daily development

Maven sets up multiple repositories

There are two different ways to specify the use of multiple repositories. The first is to specify the repository to use in the POM. This is supported both inside and outside the build profile

<project>
...
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
    <repository>
      <id>my-repo2</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
  </repositories>
...
</project>

Copy the code

Another way to specify multiple repositories is to create a new profile in ${user.home}/.m2/settings. XML or ${maven.home}/conf/settings. XML as follows:

<settings> ... <profiles> // First repository address <profile> < ID >nexus</ ID > <repository> < ID >my-repo2</ ID > <name> Your custom Repo </name> <url>http://jarsm2.dyndns.dk</url> </repository> </profile> <id>aliyun</id> <repositories> <repository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> <activeProfile>aliyun</activeProfile> </activeProfiles> ... </settings>Copy the code

If you specify a repository repository in Profiles, you need to activate that particular Profiles, which we do by configuring in activeProfiles

You can also activate the configuration file by executing the following command:

mvn -Pnexus ...
Copy the code

After normal Maven settings. XML profiles are configured, you can switch in IDEA

Set up the mirror

The image is the interceptor. It intercepts Maven’s request for a remote repository and redirects the remote repository address in the request to the address configured in the image. It mainly provides a convenient way to switch remote warehouse addresses. For example, when working in a company, using a telecom network, connecting to a telecom warehouse. When I go home, it’s Unicom’s network. I want to connect to unicom’s warehouse. I can change my project’s warehouse address to Unicom via mirroring configuration, rather than changing addresses one by one in a specific project configuration file

<settings>
  ...
  <mirrors>
    <mirror>
      <id>aliyun</id>
      <name>Maven Repository Manager running on repo.mycompany.com</name>
      <url>http://repo.mycompany.com/proxy</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>
Copy the code

Configuration description:

•id: unique identifier of the mirror •mirrorOf: specifies the mirroring rule, under which conditions to pull from the mirror repository, •*: matches all, all content is pulled from the mirror • External :*: all ideas are pulled from the mirror repository except the local cache •repo,repo1: Repo or rePO1, where repo refers to the repository ID. Repo1: all warehouses except rePO1 • Name: name description • URL: address

Listing: Set the aliyun repository to redirect the mirror to the address configured in the mirror

  <mirrors>
     <mirror>
      <id>aliyun</id>
      <mirrorOf>aliyun</mirrorOf>
      <name>ppd mirror</name>
      <url>http://repo.mycompany.com/proxy</url>
    </mirror>
  </mirrors>
Copy the code

image-20210805154919762

In this case, you will find that although repository has the correct aliyun address, the mirror is redirecting the url to the new url because of the interception.

image-20210805155037900

Mirrors and profiles set the difference between repository

Mirror and Repository are different. If multiple mirrors are configured for a repository, they are backed up and switched to another repository only when the repository is disconnected

If you can connect to the mirror but cannot find the dependency, you will not try the next mirror address

reference

• maven.apache.org/guides/mini…