Maven multiple warehouse source configuration and configuration and differences between repositories and mirrors

Maven warehouse configuration

The warehouse priority is: local warehouse (localRepositories) > repositories warehouse in profile > POM > mirrors global warehouse

repositories configuration

repositories specifies the location where the jar package is downloaded, and there can be more than one.

Repositories can be configured through setting.xml, which will take effect for all maven projects, or only configure a maven warehouse tag in pom.xml of this project.

When adding the following configuration in the pom.xml of your own maven project.

<repositories>
  <repository>
    <id>aliyun-releases</id>
    <name>阿里云仓库(name可以随便起)</name>
    <url>https://maven.aliyun.com/repository/public</url>
  </repository>
</repositories>

At this time, maven will give priority to this configuration instead of reading the configuration in setting.xml. After this configuration, maven will automatically download the jar package from aliyun.

Multiple repositories can be configured under the repositories tag. If we configure multiple repositories, which one will maven use? The answer is to use them in the order of appearance. If the first one is available, use the first one. If it is not available, go down in order Find, the following 2 pictures can illustrate this problem.

mirror configuration

<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <!--mirrorOf的配置很重要后面会详细说明-->
  <mirrorOf>central</mirrorOf>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

As configured above, maven will read the mirror configured in setting.xml as the download address of the jar package.

why?
Because the set mirrorOf is <mirrorOf>central</mirrorOf>, if <mirrorOf></mirrorOf> I set a parameter casually, such as <mirrorOf>abc</mirrorOf>, the warehouse we configured will not work at this time. This is because maven has the following built-in warehouse by default. The id of this default warehouse is central. When we set mirrorOf to <mirrorOf>central</mirrorOf>, maven will search for a warehouse with an id of central, and then put the id Replace the central warehouse address with the url configured in our <mirror> tag, so that the mirror we configured will work. Of course, we can also set mirrorOf to <mirrorOf>*</mirrorOf>, indicating that all warehouses use the mirror we configured as the jar package download address.

What is the relationship between mirrorOf and mirrorOf?

<repository> <id> seems useless, indeed, if you just configure a warehouse in pom.xml, this id is useless, you can write it casually.

In fact, this id is used in conjunction with the mirror mentioned above. Do you still remember mirrorOf? We configure mirrorOf as <mirrorOf>central</mirrorOf>. Yes, the url in the mirror will overwrite the url of the default central warehouse, so here The id under the <repository> tag is for mirrorOf.

When the id in the repository is consistent with mirrorOf, the url in mirrorOf will overwrite the url address in the repository.

Guess you like

Origin blog.csdn.net/lzzyok/article/details/120397536