上一篇咱们详解了setttings.xml的装备项,里边的装备项根本都和库房有联系,咱们使用maven更多的也是要从库房下载jar包,然后也把咱们自己公共的jar包上传到库房。由于咱们是能够装备多个库房的,这时候就涉及到了一个问题:下载一个jar包时,怎样确认这些库房的使用次序?

1、官网的解说

maven官网对这个问题给了必定的解答,如下:

Remote repository URLs are queried in the following order for artifacts until one returns a valid result:

  1. 1.effective settings:

    1. 1.Globalsettings.xml
    2. 2.Usersettings.xml
  2. 2.local effective build POM:

    1. 1.Localpom.xml
    2. 2.Parent POMs, recursively
    3. 3.Super POM
  3. 3.effective POMs from dependency path to the artifact.

For each of these locations, the repositories within the profiles are queried first in the order outlined atIntroduction to build profiles.

Before downloading from a repository,mirrors configurationis applied.

All profile elements in a POM from active profiles overwrite the global elements with the same name of the POM or extend those in case of collections. In case multiple profiles are active in the same POM or external file, the ones which are definedlatertake precedence over the ones definedearlier(independent of their profile id and activation order).

If a profile is active fromsettings, its values will override any equivalently ID’d profiles in a POM orprofiles.xmlfile.

Take note that profiles in thesettings.xmltakes higher priority than profiles in the POM.

简略翻译一下,便是:

  • •大局装备文件settings.xml中的装备项的优先级最高,也便是maven安装目录下的conf/settings.xml优先级最高
  • •其次是用户级别的装备文件优先级次高,默许是${user.home}/.m2/settings.xml
  • •最后便是本地的pom.xml文件优先级次次高
  • •当确认了要查询某个库房时,会先看这个库房有没有对应的镜像库房,假如有的话,则转向去查镜像库房,也便是会查当前库房的替代品(镜像库房),跳过对本库房的检索
  • •假如同一个pom文件里边有多个激活的profile,则靠后面激活的profile的优先级高
  • •针对pom文件,假如有激活的profile,且profile里边装备了repositories,则profile里边的repositories的库房优先级比标签下面的repositories的优先级高
  • •pom文件中无论是project标签下面直接界说的repositories,仍是profile标签下面界说的repositories,repositories内部的repository的查询次序,都是依照库房界说的次序查询,也便是自上而下查询。
  • •假如settings.xml中的profile的id和pom文件中的profile的id相同,则以settings.xml中的profile中装备的值为准
  • •假如同一个pom文件中有多个profile被激活,那么处于profiles内部靠后面生效的profile优先级比profiles中靠前的profile的优先级高

也便是全体的优先级方面:

conf/settings.xml > ${user.home}/.m2/settings.xml >本地的pom.xml文件

2、事例讲解

考虑到咱们常用的装备文件是conf/settings.xml和工程里边的pom.xml文件,咱们针对这两个文件的结合来剖析库房的使用次序。

假如咱们有如下的大局装备文件:settings.xml

<settingsxmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0http://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>D:/programs/.m2/repository</localRepository>
<servers>
<server>
<id>dev</id>
<username>repouser</username>
<password>repopwd</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexusaliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>dev-mirror</id>
<mirrorOf>dev1</mirrorOf>
<name>第二套开发库房</name>
<url>http://192.168.1.2/repository/devM</url>
</mirror>
</mirrors>
<profiles>

<profile>
<id>env-dev</id>
<repositories>
<repository>
<id>dev5</id>
<name>RepositoryforJDK1.4builds</name>
<url>http://192.168.1.1/repository/dev5</url>
</repository>
</repositories>
</profile>
<profile>
<id>env-test</id>
<repositories>
<repository>
<id>test</id>
<name>test</name>
<url>http://192.168.1.1/repository/test</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>env-dev</activeProfile>
</activeProfiles>
</settings>

工程的装备文件如下:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>1.0.0</revision>
</properties>
<repositories>
<repository>
<id>dev4</id>
<name>dev4</name>
<url>http://192.168.1.1/repository/dev4</url>
</repository>
</repositories>
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>dev1</id>
<name>dev1</name>
<url>http://192.168.1.1/repository/dev1</url>
</repository>
<repository>
<id>dev2</id>
<name>dev2</name><url>http://192.168.1.1/repository/dev2</url>
</repository>
</repositories>
</profile>
<profile>
<id>profile-2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>dev3</id>
<name>dev3</name>
<url>http://192.168.1.1/repository/dev3</url>
</repository>
</repositories>
</profile>
</profiles>
</project>

2.1、settings.xml和pom都装备激活了各自的profile

pom.xml文件默许激活了profile-1和profile-2,settings中默许激活了env-dev。依照在同一文件的profile的生效次序规矩,pom文件中的库房使用次序为

dev5->dev3->dev1->dev2->dev4->central(超级pom中界说的中心库房),

而由于在setttings.xml中为dev1和central装备了镜像库房,所以终究库房的优先查询次序为:

dev5->dev3->dev-mirror->dev2->dev4->nexus-aliyun

2.2、settings.xml没有装备激活的profile,pom中装备了激活的profile

这种情况下,settings中没有设置activeProfiles,咱们只需要考虑pom文件中库房的查询次序,依照从前说的规矩:

  • •假如同一个pom文件里边有多个激活的profile,则靠后面激活的profile的优先级高
  • •针对pom文件,假如有激活的profile,且profile里边装备了repositories,则profile里边的repositories的库房优先级比标签下面的repositories的优先级高
  • •pom文件中无论是project标签下面直接界说的repositories,仍是profile标签下面界说的repositories,repositories内部的repository的查询次序,都是依照库房界说的次序查询,也便是自上而下查询。

则库房使用次序为

dev3->dev1->dev2->dev4->central(超级pom中界说的中心库房),

而由于在setttings.xml中为dev1和central装备了镜像库房,所以终究库房的优先查询次序为:

dev3->dev-mirror->dev2->dev4->nexus-aliyun

3、库房装备主张

maven官方不主张在settings中装备profile,由于profile中装备的一些属性或者库房根本都是为项目服务的,咱们的项目能够经过代码库房(比如gitlab)进行同享,可是settings装备文件一般很难同享。假如咱们的项目依赖了自己本地的settings文件中的一些装备信息,可是其他搭档本地的settings文件又没这些信息,那么其他搭档就无法正常的运转项目。并且profile中界说的信息一般都和项目运转的环境有关,比如有开发环境的装备,测验环境的装备,还有生产环境的装备。既然和项目有直接的严密联系,就应该将其装备到项目里边。

3.1 针对库房装备的主张

  • •假如库房和环境无关,能够将库房装备到pom文件的结点下面
  • •假如不同环境使用不同的库房,则能够经过在pom文件中界说profile,并在profile结点下面装备库房

3.2、针对settings文件的装备

seetings文件主张用来装备下列几项

  • •装备本地库房途径,即装备localRepository
  • •装备中心库房的镜像,即装备mirrors
  • •装备访问库房的认证信息,即装备servers

结束语

本文先共享到这里,觉得有收获的朋友,能够重视我,或者进行共享或收藏,有疑问的也能够来私聊评论,我会及时进行回复~