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

项目中我们需要依赖许多包,那么就涉及到maven配置文件,我们需要配置settings.xml文件,这里面会配置我们的本地仓库localRepository ,远程仓库:仓库会有我们的依赖仓库repository和插件依赖仓库pluginRepository, mirror镜像仓库
依赖路径:工程优先去本地仓库找依赖—》 没找到就到配置文件中的mirror镜像仓库–》然后再依次去找repository、pluginRepository依赖

标签解释

localRepository 标签

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

servers 标签

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

server 标签

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

  <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标签内的值)

  <mirrors>
    <mirror>
      <id>central</id>
      <mirrorOf>
        central
      </mirrorOf>
      <url>https://maven.test.com/artifactory/maven-central-repo/</url>
    </mirror>
  </mirrors>

mirrorOf常见值的说明

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

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

  1. *
* 表示 不止替换 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是远程仓库的地址。

<repositories>
        <!-- 自研仓 -->
        <repository>
          <id>product_maven</id>
          <url>https://maven.test.com/artifactory/product_maven/</url>
        </repository>
        <!-- 开源中心仓 -->
        <repository>
          <id>maven-central-repo</id>
          <url>https://maven.test.com/artifactory/maven-central-repo/</url>
        </repository>
</repositories>

snapshots标签和releases标签(repository下)

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

<repository>
    <id>maven-central-repo</id>
    <url>https://maven.test.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是远程仓库的地址。

<pluginRepositories>
    <pluginRepository>
        <id>maven-central-repo-plugin</id>
        <!-- 开源中心仓 -->
        <url>https://maven.test.com/artifactory/maven-central-repo/</url> 
    </pluginRepository>

    <pluginRepository>
        <id>product-maven-plugin</id>
        <!-- 内源中心仓地址-->
        <url>https://maven.test.com/artifactory/product_maven/</url> 
    </pluginRepository>
</pluginRepositories>

snapshots标签和releases标签(pluginRepository下)

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

<pluginRepositories>
    <pluginRepository>
        <id>product-maven-plugin</id>
        <url>https://maven.test.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标签,是否做了下载限制

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值