setting.xml的mirror、mirrorOf和pom.xml的repositories、repository的关系关联*

一、先搞清楚mirrors,mirror,mirrorOf 简单点来说,repository就是个仓库。maven里有两种仓库,本地仓库和远程仓库。远程仓库相当于公共的仓库,大家都能看到。本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用。当你向仓库请求插件或依赖的时候,会先检查本地仓库里是否有。如果有则直接返回,否则会向远程仓库请求,并做缓存。你也可以把你做的东西上传到本地仓库给你本地自己用,或上传到远程仓库,供大家使用。远程仓库可以在工程的pom.xml文件里通过repositories、repository指定。如果没指定,就会去maven的安装目录找setting.xml的mirror、mirrorOf,如果没有设置,默认就会把下面这地方做远程仓库,即默认会到http://repo1.maven.org/maven2这个地方去请求插件和依赖包。

<repository> 
  <snapshots> 
    <enabled>false</enabled> 
  </snapshots> 
  <id>central</id> 
  <name>Maven Repository Switchboard</name> 
  <url>http://repo1.maven.org/maven2</url> 
</repository> 

本地仓库默认在你本地的用户目录下的.m2/repository目录下。
mirror就是镜像,主要提供一个方便地切换远程仓库地址的途径。比如,上班的时候在公司,用电信的网络,连的是电信的仓库。回到家后,是网通的网络,我想连网通的仓库,就可以通过mirror配置,统一把我工程里的仓库地址都改成联通的,而不用到具体工程配置文件里一个一个地改地址。 mirror的配置在settings.xml里。如:

<mirrors> 
  <mirror> 
    <id>UK</id> 
    <name>UK Central</name> 
    <url>http://uk.maven.org/maven2</url> 
    <mirrorOf>central</mirrorOf> 
  </mirror> 
</mirrors> 

这样的话,就会给上面id为central的远程仓库做了个镜像。以后向central这个仓库发的请求都会发到http://uk.maven.org/maven2而不是http://repo1.maven.org/maven2了。<mirrorOf>central</mirrorOf>里是要替代的仓库的id,这里就是会代替central的。如果填*,就会替代所有仓库。

二、经典示例如下:

<servers>
 <server>
  <id>tomcat7</id>
  <username>tomcat</username>
  <password>tomcat</password>
 </server>
 <server>  
      <id>nexus-releases</id>  
      <username>admin</username>  
      <password>admin123</password>
   <!--<password>123456</password>-->
    </server>
  </servers>
  <mirrors>
  <mirror>   
      <id>public_repo</id>   
      <mirrorOf>nexus-releases</mirrorOf>   
      <url>http://192.168.1.21:8081/nexus/content/groups/bigdata/</url>
   <!--<url>http://112.74.204.238:8081/nexus/content/groups/public</url> --> 
    </mirror>
 <mirror>   
      <id>plugin_repo</id>   
      <mirrorOf>nexus-plugin</mirrorOf>   
      <url>http://repo.maven.apache.org/maven2</url>   
    </mirror>
  </mirrors>
  <profiles>
 <profile>  
  <id>nexus</id>  
  <repositories>  
   <repository>  
     <id>nexus-releases</id>  
     <url>http://nexus-releases</url>  
     <releases><enabled>true</enabled></releases>  
     <snapshots><enabled>true</enabled></snapshots>  
   </repository>   
  </repositories>
  <pluginRepositories>  
   <pluginRepository>  
        <id>nexus-plugin</id>  
        <url>http://plugin_repo</url>  
        <releases><enabled>true</enabled></releases>  
        <snapshots><enabled>true</enabled></snapshots>  
   </pluginRepository>
  </pluginRepositories>
 </profile> 
  </profiles>
  <activeProfiles>  
      <activeProfile>nexus</activeProfile>  
  </activeProfiles>

activeProfiles中表示id:nexus的profile生效,nexus的profile又定义了2个repository,当id为nexus-releases的repository去下包时会找到mirrors(用mirrorOf中的值去关联),用对应的mirror的中配置的url来代替repository配置的url去下载包,这就是他们对应的关系了activeProfiles---》repository---》mirrors,用mirrors中的url代替repository的url。也可以使用简要配置,直接所有的东西都用私服地址来下载,那么mirrorOf应该配置成*(代表替换所有的)或者maven默认的repository地址(代表替换默认的)

<mirror>   
  <id>public_repo</id>   
  <mirrorOf>*</mirrorOf>   
  <url>http://192.168.1.21:8081/nexus/content/groups/bigdata/</url>
</mirror>

参考:
Maven里面mirror元素和repository元素的作用
Maven settings配置中的mirrorOf
Maven系列二setting.xml 配置详解
maven setting.xml配置说明

转载于:https://my.oschina.net/cjun/blog/881766

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值