【业务功能篇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标签,是否做了下载限制

相关推荐
冬天的雪20088 分钟前
springboot项目tomcat中加载不了
spring boot·后端·tomcat
一天八小时11 分钟前
Servlet+tomcat
java·servlet·tomcat
冷环渊12 分钟前
Finish技术生态计划: FinishRpc
java·后端·nacos·rpc·netty
敖云岚30 分钟前
【Redis】Another Redis Desktop Manager 安装指南
java·redis·github
【D'accumulation】2 小时前
Spring Boot 微服务打包为 Docker 镜像并部署到镜像仓库实战案例
spring boot·docker·微服务
耀耀_很无聊6 小时前
02_使用 AES 算法实现文件加密上传至阿里云、解密下载
java·spring boot·算法·阿里云·云计算·aes·oss
GalaxySpaceX7 小时前
Hibernate-Core (CVE-2020-25638)
java·spring·hibernate
小钻风巡山9 小时前
springboot 视频分段加载在线播放
java·spring boot·后端
不会写代码的女程序猿9 小时前
废品回收小程序:全链路数字化解决方案,赋能绿色未来
java·小程序·微信小程序定制开发
江沉晚呤时9 小时前
Redis缓存穿透、缓存击穿与缓存雪崩:如何在.NET Core中解决
java·开发语言·后端·算法·spring·排序算法