maven的settings.xml重点解读

          在公司开发中,大多数情况都会使用到maven来构建项目,那么就需要对maven的常规配置有所掌握,才会快速清晰的定位maven使用中出现的问题。

          maven的下载安装网上教程很多很简单,这里不做阐述。下面主要说一下maven的conf目录中的settings.xml文件。

1).配置本地仓库

     修改本地仓库地址,网络上下载下来的jar包就是存放在此目录下,当然你也可以使用maven的默认的目录。

     <localRepository>D:/RepMaven</localRepository>

2).配置镜像地址

    <mirrors>

       <mirror>

       <id>alimaven</id>

       <name>aliyun maven</name>

       <url>https://maven.aliyun.com/repository/public/</url> <!-- 阿里云镜像仓库 -->

       <mirrorOf>central</mirrorOf>

       </mirror>

     </mirrors>

【1】配置镜像仓库的目的是什么?

        答:它有点类似于一个代理,他会拦截去指定的远程仓库下载组件的请求,然后从镜像仓库里找到组件返回给客户端,使用镜像仓库主要是出于网速的考虑。

【2】远程仓库是否可以当做镜像仓库?

        答:自行搭建的私服往往也会提供mirror服务,例如nexus就可以及当做远程仓库,又可以成为其他仓库的mirror(镜像仓库)

【3】mirrorOf标签的解释

        答: 是mirror(镜像仓库)拦截下载请求的详细设置,哪些拦截,哪些不拦截,都是由它控制。

                1.<mirrorOf>*</mirrorOf>匹配所有远程仓库

                2.<mirrorOf>external:*</mirrorOf>匹配所有不在本机上的的远程仓库

                3.<mirrorOf>repo1,repo2</mirrorOf>匹配仓库( repository id ) repo1和repo2使用逗号分割多个远程仓库

                4.<mirrorOf>*,!repo1</mirrorOf>匹配所有仓库,repo1除外.

                5.mirrorOf的值为central,表示该配置为中央仓库的镜像,任何对于中央仓库的请求都会转至该镜像.

【4】如果配置了多个mirror,是怎么工作的?

         答:从上到下的mirror依次执行,如果有多个mirror的 mirrorOf都是*,那么首先会去第一个mirror里看是否有需要的组件,没有的话,就去第二个mirror里进行下载组件,直到找到这个组件

3).配置远程仓库

      使用 repository 标签 ,这里id 是唯一标识 nexus,配置好远程仓库还需要使用 activeProfiles 标签来**这个profile,让我们可以去远程仓库下载需要的组件

    <profiles>

         <profile>

               <id>nexus</id>

               <repositories>

                      <repository> <!-- repository标签配置远程的maven仓库 -->

                          <id>central</id>

                          <url>http://repository.sonatype.org/content/groups/public/</url>

                          <releases><enabled>true</enabled></releases>

                          <snapshots><enabled>false</enabled></snapshots>

                      </repository>

                      <repository>

                          <id>spring-milestones</id>

                          <name>Spring Milestones</name>

                          <url>http://repo.spring.io/milestone</url>

                          <snapshots>

                               <enabled>false</enabled>

                          </snapshots>

                      </repository>

         </repositories>

        <pluginRepositories>

             <pluginRepository>

             <id>central</id>

             <url>http://repository.sonatype.org/content/groups/public/</url>

             <releases><enabled>true</enabled></releases>

             <snapshots><enabled>false</enabled></snapshots>

             </pluginRepository>

          </pluginRepositories>

        </profile>

    </profiles>

             <activeProfiles>

                    <!--make the profile active all the time **指定ID的profile-->

                   <activeProfile>nexus</activeProfile>

             </activeProfiles>

4)举个例子

在maven的settings.xml中没有配置 mirror(镜像仓库),那么请求是如下图:

maven的settings.xml重点解读

当配置了mirror的时候,则下载构建的请求会变成下面途中这样,远程仓库A 作为了 mirror(镜像仓库)通过 mirrorOf 标签代理了所有去 B 仓库下载组件的请求,转到 A仓库下载组件

maven的settings.xml重点解读

5)如果使用的远程仓库需要认证,则使用 server标签配置接口;示例如下:

        <servers>

            <server>

            <id>nexus-releases</id>

            <username>deployment</username>

            <password>deployment123</password>

            </server>

         </servers>