maven多个仓库查询的优先级顺序案例讲解

目录
  • 1、官网的解释
  • 2、案例讲解
    • 2.1、settings.xml和pom都配置激活了各自的profile
    • 2.2、settings.xml没有配置激活的profile,pom中配置了激活的profile
  • 3、仓库配置建议
    • 3.1 针对仓库配置的建议
    • 3.2、针对settings文件的配置

上一篇我们详解了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. effective settings:

1. Global settings.xml

2. User settings.xml

2. local effective build POM:

1. Local pom.xml

2. Parent POMs, recursively

3. Super POM

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 at Introduction to build profiles.

Before downloading from a repository, mirrors configuration is 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 defined later take precedence over the ones defined earlier (independent of their profile id and activation order).

If a profile is active from settings, its values will override any equivalently ID'd profiles in a POM or profiles.xml file.

Take note that profiles in the settings.xml takes 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

<settings xmlns="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.0 http://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>Nexus aliyun</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>Repository for JDK 1.4 builds</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>

工程的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://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/> <!-- lookup parent from repository -->
    </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

到此这篇关于maven多个仓库查询的优先级顺序的文章就介绍到这了,更多相关maven多个仓库内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Maven仓库分类的优先级

    目录 1.仓库的分类 2.仓库的优先级 1.仓库的分类 maven的仓库主要是用于存储及管理项目中所依赖的组件.可分为本地仓库和远程仓库,远程仓库有可以细分为中央仓库.私有仓库,中央仓库主要是由maven的开发团队负责开发管理的一些公共组件的公用的仓库:私有仓库主要是指个人通过一些第三方平台搭建的自己的专属仓库:私有仓库又可以根据配置的位置不同可分为全局profile仓库.项目profile仓库.项目仓库.镜像仓库. 本地仓库:本地仓库默认是在.m2文件夹中,可以通过settings.xml文件

  • maven配置多个仓库的实现

    目录 说明 多仓库配置方式一:全局多仓库设置 多仓库配置方式二:在项目中添加多个仓库 说明 maven的中央仓库很强大,绝大多数的jar都收录了.但也有未被收录的.遇到未收录的jar时,就会编译报错.除了maven官方提供的仓库之外,也有很多的仓库.尽可能的将可信的仓库(嗯,可信的仓库!)添加几个,弥补maven官方仓库的不足. 多仓库配置方式一:全局多仓库设置 全局多仓库设置,是通过修改maven的setting文件实现的. 设置思路:在setting文件中添加多个profile(也可以在一个

  • maven 配置多个仓库的方法

    1>方法一 之前在配置 Maven 的 settings.xml 时,都会设置 mirror 节点,例如: <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central

  • maven多个仓库查询的优先级顺序案例讲解

    目录 1.官网的解释 2.案例讲解 2.1.settings.xml和pom都配置激活了各自的profile 2.2.settings.xml没有配置激活的profile,pom中配置了激活的profile 3.仓库配置建议 3.1 针对仓库配置的建议 3.2.针对settings文件的配置 上一篇我们详解了setttings.xml的配置项,里面的配置项基本都和仓库有关系,我们使用maven更多的也是要从仓库下载jar包,然后也把我们自己公共的jar包上传到仓库.由于我们是可以配置多个仓库的,

  • 关于Maven混合配置私有仓库和公共仓库的问题

    目录 背景 私有和公共仓库混合配置 Maven仓库 解决步骤 一.验证私有仓库 二.搜索共有仓库 三.搜索第三方仓库 四.maven配置 mirror profile activeProfile 配置结果 setting.xml完整配置 结论 背景 近期在调研一个开源仓库,于是将 代码从github下载后,当IDEA sync依赖时出现Cannot resolve org.fourthline.cling:cling-support:2.1.1 的问题,详情如下: Cannot resolve

  • 从零开始学习SQL查询语句执行顺序

    SQL查询语句执行顺序如下: (7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_type> JOIN <right_table> (2) ON <join_condition> (4) WHERE <where_condition> (5) GROUP BY <group_by_list> (6) HAVING <having_

  • 详解maven配置多仓库的方法示例

    刚接触maven就是在公司里配置好的,所以一直以来使用都没毛病,所以一直没有去动这些固有的东西. 但是,后来把公司的电脑拿回家之后,发现有的东西就搞不起来了.原因也看一下就明白了,因为在公司的时候用的是公司的maven私服,所以回家后,用不了也是正常. 但是,真的脱离了公司,自己就不能工作了吗?不可能吧. 难道一下开源工具都必须要依赖于公司的网络? 这明显是不合理的. 那么,就扯出本次文章的意义了,在家里,自然是要公有的maven仓库了,那么,怎样配置maven仓库才能让自己用起来顺心呢? 1.

  • Maven之远程仓库的配置详解

    在很多情况下,默认的中央仓库无法满足项目的需求,可能项目需要的构件存在于另外一个远程仓库中,如Company Maven仓库.这时,可以在项目POM中或maven的settings.xml中配置该仓库 <repositories> <repository> <id>company</id> <name>Company Repository</name> <url>http://repository.company.com/

  • maven配置阿里仓库的方法步骤

    大家使用maven下载jar包会很慢,最主要的原因是maven的仓库在英国,但如果使用了阿里的仓库下载jar包就会变得很便捷.下面是如和配置阿里的仓库. <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repos

  • MySQL中 and or 查询的优先级分析

    这个可能是容易被忽略的问题,首选我们要清楚: MySQL中,AND的执行优先级高于OR.也就是说,在没有小括号()的限制下,总是优先执行AND语句,再执行OR语句. 比如: select * from table where  条件1 AND 条件2 OR 条件3 等价于 select * from table where  ( 条件1 AND 条件2 )  OR 条件3 select * from table where  条件1 AND  条件2 OR 条件3 AND 条件4 等价于 sel

  • Maven配置多仓库无效的解决

    在项目中使用Maven管理jar包依赖,往往会出现以下状况: 1.国内访问maven默认远程中央镜像特别慢: 2.使用阿里的镜像替代远程中央镜像: 3.阿里云镜像中缺少部分jar包: 4.同时使用私有仓库和公有仓库: 针对以上情况,我们就需要让Maven支持多仓库配置. 单独仓库配置 当只配置一个仓库时,操作比较简单,直接在Maven的settings.xml文件中进行全局配置即可,以阿里云的镜像为例: <mirrors> <mirror> <id>alimaven&l

  • maven配置本地仓库的方法步骤

    目录 1.下载apache-maven-3.6.3-bin.zip 2.配置环境变量 3.测试 4.配置本地仓库 5.输入命令 本文主要介绍了maven配置本地仓库,分享给大家,具体如下: 官网http://maven.apache.org/download.cgi 1.下载apache-maven-3.6.3-bin.zip 然后解压放在本地盘(我放在了C盘,重命名为maven).然后再新建一个文件夹,命名为:maven-repository,作为本地仓库. 2.配置环境变量 在系统属性-环境

随机推荐