The most detailed maven warehouse in history

Written in the front:

I am "Boiled Sheep Sheep_", my nickname comes from the abbreviation fyy of my name. The blog I worked hard to run before was accidentally cancelled due to a handicap, and now I operate this account.
I am a small dish, and I am working hard in the direction of a full-stack engineer. The article may not be very productive and basic, but every article I write is summarized carefully, please do not spray.
If you are interested in programming, please follow my dynamics and study together.
Thank you every reader!

1 Overview

In Maven terms, a warehouse is a place.
The Maven repository is a third-party library that the project relies on. The location of this library is called the repository.
In Maven, any dependency, plug-in, or project build output can be called a component.
Maven warehouse can help us manage components (mainly JAR), it is the place where all JAR files (WAR, ZIP, POM, etc.) are placed.


2 Interpret the storage path of Jar in the warehouse

1. Prepare the path based on the groupId and convert the dot separator to the path separator, which means converting "." to "/"; for example: org.mybatis --> org/mybatis

2. Prepare the path based on the artifactId and connect the aftifactId to Behind groupId, org/mybatis/mybatis-spring

3. Prepare path based on version, connect version to behind artifactId, org/mybatis/mybatis-spring/2.0.2

4. Connect artifactId and version separated by separator, org/mybatis/ mybatis-spring/2.0.2/mybatis-spring-2.0.2

5. To determine if the component has a classifier, it is necessary to add a hyphen and classifer after item 4, org/mybatis/mybatis-spring/2.0.2 /mybatis-spring-2.0.2/mybatis-spring-2.0.2-jdk8

6. Check the extension of the component. If the extension exists, add a period separator and extension. The extension is determined by packing,
org/mybatis/ mybatis-spring/2.0.2/mybatis-spring-2.0.2/mybatis-spring-2.0.2-jdk8.jar

TIPS

classifier can be any string, used to splice after gav to determine the specified file, and can be used to distinguish jar packages generated by different jdk versions. When an error occurs when using gav, you can check the jar path in the warehouse to see if the jar is divided by jdk version and no dependencies are found. If so, add the classifier tag after the corresponding dependent node gav in the pom.


3 maven warehouse

maven warehouse type

3.1 Local warehouse

As the name implies, the maven warehouse stored on the local disk

will not be created after installing maven. It is created when the maven command is executed for the first time. The

default location of the maven local warehouse: whether it is Windows or Linux, in the user There is a warehouse directory under .m2/repository/. How to change the default local warehouse location of maven? Use the <localRepository> tag to configure in the settings.xml file of maven

Insert picture description here

At this time, the local warehouse address of maven will become the address configured by yourself. Note: The local warehouse of maven configured at this time belongs to the user scope. In general, the global scope of the local warehouse is not recommended, the user scope of the local warehouse is fine.

3.2 Remote warehouse

3.2.1 Private Server

Private server is a special remote warehouse. It is a warehouse service set up in the local area network. The private server acts as a proxy for remote warehouses on the WAN for use by maven users in the local area network. When maven needs to download a component, it will request it from the private server. If the component does not exist on the private server, it will be downloaded from an external remote warehouse and cached on the private server before serving the download request of maven.

Maven private server features

  • Save external network bandwidth: reduce external network bandwidth consumption caused by repeated requests
  • Accelerate maven components: If many external remote warehouses are configured in the project, the build speed will be greatly reduced
  • Deploy third-party components: When some components cannot be obtained from external warehouses, these components can be deployed to internal warehouses (private servers) for internal maven projects
  • Improve stability and control: When the Internet is unstable, the maven build will also be unstable
  • Reduce the load of the central warehouse: The number of requests for the central warehouse of maven is huge, and the configuration of private servers can greatly reduce the pressure on the central warehouse

Mainstream maven private server

  • Archiva for Apache
  • Artifactory by JForg
  • Sonatype's Nexus

3.2.2 Central warehouse

Maven default central warehouse:

The central warehouse is managed by the maven community and contains a large number of commonly used libraries, popular open source Java components, as well as source code, author information, SCM, information, license information, etc. Generally speaking, components that simple Java projects depend on can be downloaded here.

All maven projects will inherit 超级pom, the 超级pomstructure is as follows:

<repositories>  
    <repository>  
      <id>central</id>  
      <name>Central Repository</name>  
      <url>http://repo.maven.apache.org/maven2</url>  
      <layout>default</layout>  
      <snapshots>  
        <enabled>false</enabled>  
      </snapshots>  
    </repository>  
</repositories>

Features:

  • The warehouse is managed by the maven community;
  • No configuration required
  • Need internet to access
  • Domestic download speed is slow


4 Remote warehouse configuration

Since the download speed of the default official maven repository is too slow in China, fortunately there are some mirror repositories, the download speed will be much faster! In normal development, we often do not use the default central warehouse. Not only is the speed slow, some components of the project may not have a central warehouse, but there are other remote warehouses, such as the Alibaba Cloud warehouse.

<!-- 阿里云仓库 -->
<mirrors>
	<mirror>
	   <id>alimaven</id>
	   <mirrorOf>central</mirrorOf>
	   <name>aliyun maven</name>
	   <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
	</mirror>
</mirrors>

<!-- 私服 -->
<mirrors>
	<mirror>
		<id>nexus</id>
		<mirrorOf>central</mirrorOf>
		<name>nexus repository</name>
		<url>http://nexus.dmsd.tech/nexus/content/groups/dynamic-public</url>
	</mirror> 
</mirrors>

<profile>
<id>central</id>
	<repositories>
		<repository>
			<id>central</id>
			<name>Central</name>
			<url>http://nexus.dmsd.tech/nexus/content/groups/dynamic-public </url>
		</repository>
	</repositories>
</profile> 

mirrors and respository

If warehouse X can provide all the contents stored in warehouse Y, then X can be considered a mirror image of Y. In other words, any component that can be obtained from warehouse Y can be obtained from its mirror image. E.g:

http://repo1.maven.org/maven2/  是中央仓库,
http://maven.oschina.net/content/groups/public/ 是其在中国的镜像,
由于地理位置的因素,该镜像往往能提供比中央仓库更快的服务。因此,可以配置 maven 使用该镜像来替代中央仓库。

<mirrors>
     <mirror>
       <id>maven.oschina.net</id>
       <name>maven mirror in China</name>
      <url>http://maven.oschina.net/content/groups/public/</url>
       <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

其中,mirrorOf的值是 central,表示该配置为中央仓库的镜像,任何对于中央仓库的请求都会转至该镜像,
用户也可以使用同样的方法配置其他仓库的镜像,id 表示镜像的唯一标识符,name 表示镜像的名称,url 表示镜像的地址。


关于镜像的一个更为常见的用法是结合私服,由于私服可以代理任何外部的远程仓库(包括中央仓库),因此,对于组织内部的maven用户来说 
使用一个私服地址就等于使用了所有需要的外部仓库,这可以将配置集中到私服,从而简化maven本身的配置。在这种情况下,
任何需要的构件都可以从私服中获得,私服就是所有仓库的镜像,这时,可以这样配置:

<!--配置私服镜像-->
 <mirrors> 
     <mirror>  
         <id>nexus</id>  
         <name>internal nexus repository</name>  
         <url>http://183.238.2.182:8081/nexus/content/groups/public/</url>  
        <mirrorOf>*</mirrorOf>  
   </mirror>  
</mirrors>

该例中<mirrorOf>的值为星号,表示该配置是所有Maven仓库的镜像,任何对于远程仓库的请求都会被转至局域网的 url地址,如果该镜像
需要认证,则配置一个 id 为 nexus 的认证信息即可。

如果 repositories 的 id 和 mirror 的 mirrorOf 的值相同,则该mirror 替代该 repository.
如果该 repository 找不到对应的 mirror,则使用其本身,所以起到最终作用的是 repository 集合,repositories 中默认
包含了中央仓库 central,当然也可以重写URL

mirror相当于一个拦截器,它会拦截 maven 对 repository 的相关请求,把请求里的 remote repository 地址,重定向到
mirror 里配置的地址。

5 maven dependency search order

The order of maven multi-repository query dependencies is roughly as follows:

  1. Find in the local warehouse, if not, go to the next step
  2. Find in the global configuration private server repository (settings.xml), if not, then the next step
  3. Search in the private server repository (pom.xml) configured in the project itself, if not, then the next step
  4. Search in the central warehouse, if not, terminate the search

Insert picture description here
Note:

1. If during the search process, if you find that the warehouse has mirror settings, replace it with the address of the mirror. For example, if you want to find a dependency in the respository A warehouse, but A warehouse is configured with mirror, it will go to Look up the dependency from A's mirror, and no longer look up it from A.

2. The priority of the respository under the profile (activated) configured in settings.xml is higher than the respository configured in the pom file in the project.

3. If the id of the warehouse is set to "central", the warehouse will override the default central warehouse configuration of maven.

Guess you like

Origin blog.csdn.net/weixin_42653522/article/details/108588726