Maven settings.xml中私有仓库配置浅析

Maven setting中私有仓库配置浅析

最近遇到过不少这样那样的问题,曾经做过maven的分享,但是发现当时部分内容还是太想当然了,下面经过尝试后简单总结下:

首先几个逻辑:

  • pom>启用的profile>maven原有配置

  • mirror配置mirrorOf和id匹配优先

    简单maven配置

    一般大家的配置(略去无关私有仓库配置)都是这样的

    <mirrors>
      <mirror>
          <id>nexus</id>
          <name>mvn.xxx.com</name>
          <mirrorOf>central</mirrorOf>
          <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>         
      </mirror>
    </mirrors>
      <profile>
          <id>dev</id>
          <repositories>       
          <repository>
            <id>nexus</id>
            <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
            <releases><enabled>true</enabled></releases>
                      <snapshots><enabled>true</enabled></snapshots>
          </repository>
                      
              <repository>
                  <id>alibaba</id>
                  <url>http://code.alibabatech.com/mvn/releases/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </repository>
             
          </repositories>
          
          <pluginRepositories>
              <pluginRepository>
            <id>nexus</id>
            <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
            <releases><enabled>true</enabled></releases>
                      <snapshots><enabled>true</enabled></snapshots>
          </pluginRepository>
          </pluginRepositories>
      </profile>  
    <activeProfiles>
      <activeProfile>dev</activeProfile>
    </activeProfiles>
    

其实上面这个配置不能说错误,只能说有部分是没有生效的。几个重要的点逐一说明

mirrors

这个标签重要的属性包括idmirrorOf。id用来唯一区分。mirrorOf用来关联repository。
url用来表示私服地址。
mirrorOf常见大家配置成*、central、repo啥的。这里刚才提到了是用来关联respository的,等提到下面<respository>标签的时候在解释。
推荐大家阅读下http://blog.csdn.net/isea533/article/details/21560089

profile

这个就简单说下吧,就是算是个配置,可以配多个,具体哪个生效可以通过mvn命令指定,或者配置<activeProfiles>

repositories

这里面算是配置的重点

<repository>
      <id>alibaba</id>
      <url>http://code.alibabatech.com/mvn/releases/</url>
      <releases>
              <enabled>true</enabled>
      </releases>
      <snapshots>
               <enabled>false</enabled>
      </snapshots>
</repository>

几个重要的配置,一目了然吧,id标识,url地址,是否从该仓库下release,是否从该仓库下快照版本。
这里就有人会懵逼了,这里怎么又配了个地址,跟mirrors里面的地址哪个生效呢?

好的,那咱们试试。先规定一下配置:

<mirrors>
    <mirror>
        <id>nexus</id>
        <name>mvn.ws.netease.com</name>
        <mirrorOf>central</mirrorOf>
        <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>       
    </mirror>
  </mirrors>
  <repositories>       
        <repository>
          <id>nexus</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
</repositories> 

把地址区分下,mirror里配成xxx,repository配成ccc
随便找一个项目,设定一个不存在的依赖,mvn -U compile下:


image.png

可以发现去ccc找了。说明repository里的生效了。
那么mirror里的地址什么时候生效呢?其实刚才说了,mirror里的是靠mirrorOf中的内容和repository中id关联的。比如我们把刚才配置改为

<mirrors>
    <mirror>
        <id>nexus</id>
        <name>mvn.ws.netease.com</name>
            <mirrorOf>central</mirrorOf>
        <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>       
    </mirror>
  </mirrors>
  <repositories>       
        <repository>
          <id>central</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
        </repository>
</repositories> 

把repository中的id改成central

image.png

这样就行了。此外mirrorOf中可以配置通配符,例如*,表示任何repository都和这个关联。

其实简单来说就是如果repository的id能和mirrorOf关联上,那么url以mirror的为准,否则以repository中自己的url为准

其他还有一些点,repositories中可以配置多个repository,配置多个话,一个找不到会找下一个,比如我们在刚才基础上加上阿里的配置

<repositories>       
        <repository>
          <id>nexus</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
                <id>alibaba</id>
                <url>http://code.alibabatech.com/mvn/releases/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository> 
</repositories>

在构建一次:

image.png

当配置多个时,会逐一进行下载尝试。

总结

咱们在回顾下起初的配置,可以看到启用的profile是dev,dev中的repository的id是nexus,跟mirrorOf没有匹配,那么生效的配置就是repository中自己的url配置,所以这里完全可以省略掉mirror的配置。当然如果多个profile公用一个私服地址,也可以指定mirror地址,然后repository中的id指定成和mirrorOf相同,同时可以省略掉自己标签中url。

此外还有几个点要说,pluginRepositories,配置信息基本和repository一致,不过这个地址是用来下maven的插件的,就是pom中这样的配置

        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>**</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

还有,pom也可以指定repository:


image.png

这样配置会和settings.xml中生效的配置合并,并优先从这个库找,找不到继续走settings.xml配置。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,478评论 19 139
  • |-1-更新内容[6.从仓库解析依赖的机制(重要)] 1Maven仓库作用 仓库用来存储所有项目使用到构件,在ma...
    zlcook阅读 11,414评论 0 25
  • 搭建 nexus 私服(centos6.7) 备注:Centos 6.7 、 nexus-2.12.1-01-bu...
    逐暗者阅读 7,237评论 3 9
  • 1.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很...
    followtry阅读 13,861评论 3 4
  • 宋庄前西淇水芳, 庄水之上雁成行。 蓝天丽日朔风冽, 途经马耳北过常。 征尘仆仆羽翼苦, 一路雁歌向衡阳。 迤逦溹...
    王守栋阅读 2,720评论 0 6