maven多个仓库查询的优先级顺序是什么

发布时间:2023-04-24 16:09:27 作者:iii
来源:亿速云 阅读:112

这篇文章主要介绍“maven多个仓库查询的优先级顺序是什么”,在日常操作中,相信很多人在maven多个仓库查询的优先级顺序是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”maven多个仓库查询的优先级顺序是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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.

简单翻译一下,就是:

也就是整体的优先级方面:

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文件中仓库的查询顺序,按照先前说的规则:

则仓库使用顺序为

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 针对仓库配置的建议

3.2、针对settings文件的配置

seetings文件建议用来配置下列几项

到此,关于“maven多个仓库查询的优先级顺序是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Maven nexus安装nexus私服出现的问题及解决办法
  2. BAT脚本调用maven命令的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

maven

上一篇:vue2和vue3中provide/inject怎么使用

下一篇:React中的ref属性怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》