【业务功能篇80】Springboot项目 maven配置仓库&镜像settings文件分析

项目中我们需要依赖许多包,那么就涉及到maven配置文件,我们需要配置settings.xml文件,这里面会配置我们的本地仓库localRepository ,远程仓库:仓库会有我们的依赖仓库repository和插件依赖仓库pluginRepository, mirror镜像仓库

依赖路径:工程优先去本地仓库找依赖---》 没找到就到配置文件中的mirror镜像仓库--》然后再依次去找repository、pluginRepository依赖

标签解释

localRepository 标签

xml 复制代码
<!-- 这个标签表示下载的jar,包保存到本地具体目录-->
<localRepository> </localRepository>

servers 标签

servers标签是一个大标签,主要作用于上传操作,保存仓库用户 和 仓库密码的地方

server 标签

server标签在 servers内,每个server标签表示一个仓库用户,servers标签可以包含多个server标签

xml 复制代码
  <servers>
    <!-- 仓库用户信息配置 可以配置多个-->
    <server>
      <!-- 这里的 <id> 标签的值可以自定义,
          但是一定要与pom.xml文件内的 <snapshotRepository>(或者<repository>)标签下的<id>标签值一致 -->
      <id>nexus-snapshot</id>
      <!-- 仓库的用户名 (注意:不是自己的域用户名)-->
      <username>zfl789</username>
      <!-- 仓库的密码 (注意:不是自己的域密码,这里一定不要写明文密码)-->
      <password>WmZsMTgyNzY3MzUxNjEhIQ</password>
    </server>

    <!-- 仓库用户信息配置 2-->
    <server>
      <id>nexus-release</id>
      <username>zfl789</username>
      <password>WmZsMTgyNzY3MzUxNjEhIQ</password>
    </server>
  </servers>

mirrors 标签

mirrors标签用于配置镜像仓库,以提高Maven构建的速度和稳定性

注: mirrors标签和repositories、pluginRepositories标签,都是配置远程仓库的标签。但是作用不一样。maven去远程仓库寻找的时候,优先寻找 mirrors标签下的远程仓库,其次才是另外两个

mirror 标签

mirror标签在 mirrors标签内,mirrors标签可以包含多个mirror标签,但一般建议只配置一个

mirrorOf 标签

mirrorOf标签表示要替换的远程仓库的ID集合

(远程仓库的ID指的是,repository标签【或者pluginRepository标签】下的id标签内的值)

xml 复制代码
  <mirrors>
    <mirror>
      <id>central</id>
      <mirrorOf>
        central
      </mirrorOf>
      <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/maven-central-repo/</url>
    </mirror>
  </mirrors>

mirrorOf常见值的说明

  1. central
xml 复制代码
当mirrorOf的值为 central时,表示要替换中心仓。settings.xml文件有默认的中心仓(镜像仓库)地址是 maven官网地址 https://maven.apache.org。综上所述,所以当值为 central 时候替换的实际上是默认的 maven官网地址。

注:如果没有配置 mirrors标签,那么当maven去远端找包的时候,第一步依旧是先寻找 镜像仓库(中心仓库)。由于没有配置mirrors标签的原因,那么默认的 镜像仓库(中心仓库) 就是 https://maven.apache.org 地址,这也是为什么没有配置 https://maven.apache.org 地址,但是查看日志时候,发现有这个maven官方仓库的原因。

  1. *
xml 复制代码
* 表示 不止替换 central (中心仓库),也替换 <repository></repository> 标签和 <pluginRepository> </pluginRepository> 标签内的所有仓库。也就是说,当 mirrorOf的值为 *的时候,只有一个远端仓库地址。
  1. ![远程仓库的id]
    ![远程仓库的id] 值主要配合 * 进行使用,表示根据仓库ID值进行排除,被排除的这个仓库不进行替换。多个值之间用英文的逗号隔开。

repositories标签

repositories标签是用来配置远程仓库地址的,一个repositories标签包含多个repository标签。

注:repositories与pluginRepositories虽然都是配置远程仓库地址的标签。但是pluginRepositories配置的是插件下载地址,repositories配置的是依赖的下载地址。因此,在新增仓库时候,如果你无法区分当前的jar包是插件还是依赖,就同时配置pluginRepositories和repositories。

repository标签

repository标签主要由 id和url组成,每个id不能重复,url是远程仓库的地址。

xml 复制代码
<repositories>
        <!-- 自研仓 -->
        <repository>
          <id>product_maven</id>
          <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/product_maven/</url>
        </repository>
        <!-- 开源中心仓 -->
        <repository>
          <id>maven-central-repo</id>
          <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/maven-central-repo/</url>
        </repository>
</repositories>

snapshots标签和releases标签(repository下)

snapshots标签和releases标签是 repository标签下的子标签,主要用于控制依赖 release版本和snapshot版本的下载。

xml 复制代码
<repository>
    <id>maven-central-repo</id>
    <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/maven-central-repo/</url>
    <snapshots>
        <!-- 当enabled值为true的时候,表示下载snapshot版本的包。
        当为false的时候表示不下载snapshot版本的包。 -->
        <enabled>false</enabled>
    </snapshots>
    <releases>
        <!-- 当enabled值为true的时候,表示下载releases版本的包。
        当为false的时候表示不下载releases版本的包。 -->
        <enabled>true</enabled>
    </releases>
</repository>

注:有的时候,明明配置了正确的仓库地址,但是发现包还是没有下载成功,这个时候就要检查releases和snapshots标签,是否做了下载限制

pluginRepositories标签

pluginRepositories标签是用来配置远程仓库地址的,一个pluginRepositories标签包含多个pluginRepository标签。

注:repositories与pluginRepositories虽然都是配置远程仓库地址的标签。但是pluginRepositories配置的是插件下载地址,repositories配置的是依赖的下载地址。因此,在新增仓库时候,如果你无法区分当前的jar包是插件还是依赖,就同时配置pluginRepositories和repositories。

pluginRepository标签

pluginRepository标签主要由 id和url组成,每个id不能重复,url是远程仓库的地址。

xml 复制代码
<pluginRepositories>
    <pluginRepository>
        <id>maven-central-repo-plugin</id>
        <!-- 开源中心仓 -->
        <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/maven-central-repo/</url> 
    </pluginRepository>

    <pluginRepository>
        <id>product-maven-plugin</id>
        <!-- 自研制品中心仓地址-->
        <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/product_maven/</url> 
    </pluginRepository>
</pluginRepositories>

snapshots标签和releases标签(pluginRepository下)

snapshots标签和releases标签是 pluginRepository标签下的子标签,主要用于控制依赖 release版本和snapshot版本的下载。

xml 复制代码
<pluginRepositories>
    <pluginRepository>
        <id>product-maven-plugin</id>
        <url>https://cmc.centralrepo.rnd.huawei.com/artifactory/product_maven/</url>
        <!-- 当enabled值为true的时候,表示下载releases版本的包。
        当为false的时候表示不下载releases版本的包。 -->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!-- 当enabled值为true的时候,表示下载snapshot版本的包。
        当为false的时候表示不下载snapshot版本的包。 -->
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

注:有的时候,明明配置了正确的仓库地址,但是发现包还是没有下载成功,这个时候就要检查releases和snapshots标签,是否做了下载限制

相关推荐
budingxiaomoli8 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫8 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁8 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
学习中.........8 小时前
从扰动函数的变化,感受红黑树带来的性能提升
java
计算机安禾9 小时前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
weixin199701080169 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
环流_9 小时前
redis核心数据类型在java中的操作
java·数据库·redis
雨辰AI10 小时前
SpringBoot3 项目国产化改造完整流程|从 MySQL 到人大金仓落地
java·数据库·后端·mysql·政务
带刺的坐椅10 小时前
Java 流程编排新范式 Solon Flow:一个引擎,七种节点,覆盖规则/任务/工作流/AI 编排全场景
java·spring·ai·solon·flow
知彼解己10 小时前
Arthas:Java生产环境问题排查利器,从入门到实战
java