Detailed explanation of Maven's Mirror and Repository

1 Repository

 

1.1 There are two main types of Maven repositories:

  • Remote repository: Equivalent to a public repository, everyone can access it, generally in the form of URL
  • local repository: a folder stored on the local disk, for example, on windows, the default is C:\Users\{username}\.m2\repository directory

1.2 There are three main types of Remote Repository:

  • Central repository: http://repo1.maven.org/maven2/ 
  • Private server: maven repository built by the intranet, its URL is an internal website 
  • Other public repositories: other maven repositories that can be accessed publicly on the Internet, such as jboss repository, etc.

The repository stores all kinds of jar packages and maven plugins. When requesting a plug-in or dependency from the repository, the local repository will be checked first, and if the local repository exists, it will be returned directly, otherwise it will be requested from the remote repository and cached in the local repository. You can also put the work in the local warehouse for local use only; or upload it to a remote warehouse for everyone to use. 

2 Mirror

Mirror is equivalent to an interceptor, which intercepts maven's related requests for remote repository, and redirects the remote repository address in the request to the address configured in mirror.

2.1 No mirror is configured:

 

2.2 Configure mirror:

 

At this point, B Repository is called a mirror of A Repository.

If repository X can provide everything stored in repository Y, then X can be considered a mirror of Y. In other words, any component that can be obtained from repository Y can be obtained from its mirror.

2.3 <mirrorOf></mirrorOf> 

Inside the <mirrorOf></mirrorOf> tag is the Repository ID to be mirrored. In order to meet some complex needs, Maven also supports more advanced image configuration: 

  • <mirrorOf>*</mirrorOf> 

            Match all remote repositories. 

  • <mirrorOf>repo1,repo2</mirrorOf> 

            Match repo1 and repo2, separate multiple remote repositories with commas. 

  • <mirrorOf>*,!repo1</miiroOf> 

            Match all remote repositories except repo1, use an exclamation mark to exclude repositories from matching. 

3 Repository与Mirror

3.1 Definitions

In fact, mirror represents the relationship between two Repositories. <mirrors><mirror>..........</mirror></mirrors> is configured in the maven configuration file (setting.xml). , which defines the mirror relationship between the two Repositories.

3.2 Purpose

Configure the mirror relationship between two Repositories, generally for the consideration of access speed and download speed.

For example, there is a project that needs to be coded in both company and residence, and a Maven library is configured in the project pom.xml. In the company, it is the telecommunications network, and access to the A library is very fast, so maven management dependencies and plug-ins are downloaded from the A library; in the residence, it is the Netcom network, and the access to the A library is very slow, but the B library is very fast. At this time, in the setting.xml of the residence, as long as you configure <mirrors><mirror>....</mirror></mirrors>, let the B library become the mirror of the A library, you don't need to change the project pom.xml For the relevant configuration of the A library.

If the image repository requires authentication, configure <server></server> in setting.xml.

3.3 Attention

It should be noted that since the mirror warehouse completely shields the mirrored warehouse, when the mirrored warehouse is unstable or stops service, Maven will still be unable to access the mirrored warehouse, so it will not be able to download the components.

4 PW

Private server is a special remote Maven warehouse, which is a warehouse service set up in the local area network. The private server is generally configured as a mirror of the Internet remote warehouse for Maven users in the local area network.

When Maven needs to download a component, it first requests the private server. If the component does not exist on the private server, it will be downloaded from the external remote warehouse and cached on the private server, and then provide the download service for the Maven download request. In addition, for custom Or third-party jars can be uploaded locally to the private server for use by other maven users in the local area network.

The main advantages are:

  1. Save Internet Broadband
  2. Speed ​​up Maven builds
  3. Deploy third-party artifacts
  4. Improve stability and enhance control: the reason is that the external network is unstable
  5. Reduce the load on the central warehouse: the reason is that the central warehouse has too many visits

 

PS: Schematic diagram of Maven warehouse

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325010807&siteId=291194637