Maven's mirror, repository, server and proxy configuration and jar package download logic

1. Analysis of related concepts

Maven's settings.xml file contains proxy, server, repository, and mirror configurations, which are easy to confuse when configuring the warehouse address.

  1. Proxy is a proxy service that needs to be set when the server cannot directly access the external network, which is not commonly used.
    • It is the proxy in the VPN. This configuration can be used when the network is blocked.
  2. server is the place where the authentication information of the private server is set when the server is to package and upload to the private server.
    • Generally, it is the authentication information of the private server of the enterprise.
    • It generally matches by ID and repository. For example, a private repository requires user authentication information, which can be configured here.
  3. repository is the address where the server downloads the jar package.
    • Store the address of the jar package. Download the jar package from this address.
    • Multiple can be configured, each repository has a unique ID. In theory, the ID is arbitrary, but the repository whose ID is central is a special repository.
    • If we do not configure any repository, maven will generate a repository with an id of central for its own use by default.
    • Maven searches all configured repositories one by one when downloading jar packages. If all repositories cannot be found, an exception will be prompted.
  4. mirror is the mirror address used to overwrite the repository. At this time, when we download the jar package, if we need to download it from this repository. In fact, maven will download the jar package from the address corresponding to the mirror corresponding to this repository. It can be understood that the mirror address will overwrite its corresponding repository address, thereby changing the download address of the jar package, which is also the working principle of various maven mirror sites. (The correspondence between repository and mirror is matched by their IDs).

Two, mirror and repository matching logic

The matching rules between mirror and repository are as follows.

  1. Congruent matches. If the mirrorOf value of the mirror is exactly the same as the ID of the repository, the mirror matches the repository. However, mirrorOf can usually configure multiple values, separated by commas. The configuration is as follows, when we need to download the jar from the two warehouses of aliyun or google, maven will download it directly from the mirror of ALiYunMirror. That is, download it from https://maven.aliyun.com/repository/public.
<mirror>
     <id>ALiYunMirror</id>
     <mirrorOf>aliyun,google</mirrorOf>
     <name>Nexus aliyun</name>
     <url>https://maven.aliyun.com/repository/public</url>
</mirror>


<repository>
    <id>aliyun</id>
    <url>https://maven.aliyun.com/repository/central</url>
</repository>

<repository>
    <id>google</id>
    <url>https://maven.aliyun.com/repository/google</url>
</repository>
  1. Wildcard matching. "*" can match all repositories. As shown below, when the jar package needs to be downloaded from aliyun or google, the download address will be redirected to the address configured by ALiYunMirror.
<mirror>
     <id>ALiYunMirror</id>
     <mirrorOf>*</mirrorOf>
     <name>Nexus aliyun</name>
     <url>https://maven.aliyun.com/repository/public</url>
</mirror>


<repository>
    <id>aliyun</id>
    <url>https://maven.aliyun.com/repository/central</url>
</repository>

<repository>
    <id>google</id>
    <url>https://maven.aliyun.com/repository/google</url>
</repository>
  1. external match. Use "external:*" to match against the repository. It matches the "everything not on localhost and not file-based" repository configuration. As shown below, when the jar package needs to be downloaded from aliyun or google, the download address will be redirected to the address configured by ALiYunMirror. Instead of redirecting local or local-http download requests.
    1. The host part in the URL does not match "localhost".
    2. The host part in the URL is "127.0.0.1" does not match.
    3. The protocol part in the URL does not match "file".
    4. Everything else matches.
<mirror>
     <id>ALiYunMirror</id>
     <mirrorOf>external:*</mirrorOf>
     <name>Nexus aliyun</name>
     <url>https://maven.aliyun.com/repository/public</url>
</mirror>


<repository>
    <id>aliyun</id>
    <url>https://maven.aliyun.com/repository/central</url>
</repository>

<repository>
    <id>google</id>
    <url>https://maven.aliyun.com/repository/google</url>
</repository>

<repository>
    <id>local</id>
    <url>file:///root/m2/</url>
</repository>

<repository>
    <id>local-http</id>
    <url>http://localhost:8080/m2/</url>
</repository>
  1. External http matches. Use "external:http:*" to match the repository. It matches the repository configuration of "http-related protocols". As shown below, when the jar package needs to be downloaded from http-repo and http-dav, the download address will be redirected to the address configured by ALiYunMirror. It will not redirect download requests from aliyun, google, local or local-http.
    • The protocol part of the URL is "http", "dav", "dav:http" or "dav+http" and is not a match of "local URL".
      • "Local URL" means:
      1. The host part of the URL is the URL of "localhost".
      2. The host part of the URL is the URL of "127.0.0.1".
      3. The protocol part of the URL is the URL of "file".
    • Nothing else matches.
<mirror>
     <id>ALiYunMirror</id>
     <mirrorOf>external:*</mirrorOf>
     <name>Nexus aliyun</name>
     <url>https://maven.aliyun.com/repository/public</url>
</mirror>


<repository>
    <id>aliyun</id>
    <url>https://maven.aliyun.com/repository/central</url>
</repository>

<repository>
    <id>google</id>
    <url>https://maven.aliyun.com/repository/google</url>
</repository>

<repository>
    <id>local</id>
    <url>file:///root/m2/</url>
</repository>

<repository>
    <id>local-http</id>
    <url>http://localhost:8080/m2/</url>
</repository>

<repository>
    <id>http-repo</id>
    <url>http://maven456.com:8080/m2/</url>
</repository>

<repository>
    <id>http-dav</id>
    <url>dav:http://maven123.com:8080/m2/</url>
</repository>
  1. Negates the match. Use "!repo" to match against repository. It says there is no match for the repository with ID "repo". As shown below, when jar packages need to be downloaded from aliyun, local, http-repo, local-http, and http-dav, the downloaded addresses will be redirected to the address configured in ALiYunMirror. Instead of redirecting google's download request.
<mirror>
     <id>ALiYunMirror</id>
     <mirrorOf>*,!google</mirrorOf>
     <name>Nexus aliyun</name>
     <url>https://maven.aliyun.com/repository/public</url>
</mirror>


<repository>
    <id>aliyun</id>
    <url>https://maven.aliyun.com/repository/central</url>
</repository>

<repository>
    <id>google</id>
    <url>https://maven.aliyun.com/repository/google</url>
</repository>

<repository>
    <id>local</id>
    <url>file:///root/m2/</url>
</repository>

<repository>
    <id>local-http</id>
    <url>http://localhost:8080/m2/</url>
</repository>

<repository>
    <id>http-repo</id>
    <url>http://maven456.com:8080/m2/</url>
</repository>

<repository>
    <id>http-dav</id>
    <url>dav:http://maven123.com:8080/m2/</url>

</repository>

The configuration of the plugin repository pluginRepositories.

  1. The configuration of the plug-in warehouse is consistent with the configuration of the repository. And mirror will also intercept (or "mirror") the jar package download request of the matching plug-in repository. So there is no special explanation here.

some problems.

  1. During development, I encountered a situation where a certain version of the dependent jar package and pom were not in the same warehouse. Therefore, if you want to download the dependencies of this version completely, you must configure both the warehouse where the jar package is located and the warehouse where the pom is located (of course, this does not happen under normal circumstances, and some private warehouses may have this situation).
    The dependent jar package and pom of the same version are not in the same warehouse

3. Illustrated Jar package (or pom) download situation

1. [Maven: jar package (or pom) download logic without mirror configuration]

Maven configuration reference:

<repository>
    <id>repo1</id>
    <url>http://mv.com/m1/</url>
</repository>
<repository>
    <id>repo2</id>
    <url>http://mv.com/m2/</url>
</repository>
<repository>
    <id>repo3</id>
    <url>http://mv.com/m3/</url>
</repository>

1. [Maven: jar package (or pom) download logic without mirror configuration]

2. [Maven: jar package (or pom) download logic under mirror configuration]

Maven configuration reference:

<mirror>
     <id>ALiYunMaven</id>
     <mirrorOf>*</mirrorOf>
     <name>Nexus aliyun</name>
     <url>https://maven.aliyun.com/repository/public</url>
</mirror>

<repository>
    <id>repo1</id>
    <url>http://mv.com/m1/</url>
</repository>
<repository>
    <id>repo2</id>
    <url>http://mv.com/m2/</url>
</repository>
<repository>
    <id>repo3</id>
    <url>http://mv.com/m3/</url>
</repository>

2. [Maven: jar package (or pom) download logic under mirror configuration]

3. [Maven: jar package (or pom) download logic under mirror and server configuration]

Maven configuration reference:

<server>
    <id>repo1</id>
    <username>repouser1</username>
    <password>repopwd1</password>
</server>
<server>
    <id>repo2</id>
    <username>repouser2</username>
    <password>repopwd2</password>
</server>
<server>
    <id>repo3</id>
    <username>repouser3</username>
    <password>repopwd3</password>
</server>
<mirror>
    <id>ALiYunMaven</id>
    <mirrorOf>repo1,repo2</mirrorOf>
    <name>Nexus aliyun</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>
<repository>
    <id>repo1</id>
    <url>http://mv.com/m1/</url>
</repository>
<repository>
    <id>repo2</id>
    <url>http://mv.com/m2/</url>
</repository>
<repository>
    <id>repo3</id>
    <url>http://mv.com/m3/</url>
</repository>

3. [Maven: jar package (or pom) download logic under mirror and server configuration]

4. [Maven: jar package (or pom) download logic with mirror, proxy and server configurations]

Maven configuration reference:

<proxy>
    <id>optional</id>
    <active>true</active>
    <protocol>http</protocol>
    <username>proxyuser</username>
    <password>proxypass</password>
    <host>proxy.host.net</host>
    <port>80</port>
    <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
<server>
    <id>repo1</id>
    <username>repouser1</username>
    <password>repopwd1</password>
</server>
<server>
    <id>repo2</id>
    <username>repouser2</username>
    <password>repopwd2</password>
</server>
<server>
    <id>repo3</id>
    <username>repouser3</username>
    <password>repopwd3</password>
</server>
<mirror>
    <id>ALiYunMaven</id>
    <mirrorOf>repo1,repo2</mirrorOf>
    <name>Nexus aliyun</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>
<repository>
    <id>repo1</id>
    <url>http://mv.com/m1/</url>
</repository>
<repository>
    <id>repo2</id>
    <url>http://mv.com/m2/</url>
</repository>
<repository>
    <id>repo3</id>
    <url>http://mv.com/m3/</url>
</repository>

4. [Maven: jar package (or pom) download logic with mirror, proxy and server configurations]

Guess you like

Origin blog.csdn.net/qq_14947845/article/details/124765578