03-2.Maven项目怎么调用远程仓库,及镜像配置mirrors的使用

Maven 有个超级POM,它默认的位置在:maven-model-builder-3.3.9.jar\org\apache\maven\model\pom-4.0.0.xml,里面配置了maven默认调用远程仓库和插件仓库的地址,如下:

<repositories>
    <repository><!--依赖中央仓库-->
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout><!--仓库布局:默认-->
      <snapshots>
        <enabled>false</enabled><!--下载快照版本:关闭-->

      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories><!--插件的中央仓库-->
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

下面在讲讲Mirror

mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
这里写图片描述
这里写图片描述

mirrorOf标签里面放置的是要镜像的远程库,它当于一个代理,会拦截去指定的远程repository下载构件的请求。
比如上面两个图,正常调用远程仓库时候,maven回去找ID为A的对应的远程仓库,设置mirror后,mirrorOf为A,则会拦截请求为A的仓库调用,然后重定向到B

高级的镜像配置:
1.*
匹配所有远程仓库。 这样所有pom中定义的仓库都不生效
即,镜像的mirrorof如果设置成*可以拦截所有的url请求,并把他重定向到镜像设置的url路径上
2.external:*
匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。
3.repo1,repo2
匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。
4.*,!repo1
匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。

正如上面所讲到的如果maven有自己默认配置好的调用远程仓库和远程插件仓库的地址,如果我们在mirrors里做如下配置,则会替代原来默认的远程仓库调用地址

 <mirror>       
     <id>alimaven</id><!--此时的镜像  -->       
     <name>aliyun maven</name>       
     <url>http://maven.aliyun.com/nexus/content/groups/public/</url>       
     <mirrorOf>central</mirrorOf>             
     </mirror> 

原来默认配置的仓库ID是central,所以这里的mirrorof为central,这样就会拦截默认的仓库调用,然后转到对应我们配置好的阿里云的仓库地址上


我们知道pom.xml里面的 repository和pluginRepository标签可以设置远程的仓库地址,同时settings.xml里的profile标签里的repository和pluginRepository标签可以设置远程的仓库地址,同时设置时,pom里的会覆盖settings里的。

一般来说,配置了私服的maven调用远程仓库顺序试着样的
1)如果没有把默认的central仓库配置到镜像里
当要下载依赖时,第一步:找到setting.xml中激活的profile下repository里id为xxx的配置,而xxx已经配置在里镜像里,如下
代码中repository的id为nexus,查找mirrors列表发现有一个的mirrorof里是neuxs,则会使用mirror配置的url去查找。那么就会直接用repository的url去获取。获取规则详见:http://blog.csdn.net/isea533/article/details/22437511

 <profile>        
        <id>Nexus</id>        
        <repositories>          
        <repository>           
        <id>nexus</id>           
        <url>http://192.168.8.244:8081/nexus/content/groups/public</url>           
        <releases><enabled>true</enabled></releases>           
        <snapshots><enabled>true</enabled></snapshots>          
        </repository>        
        </repositories>        
        <pluginRepositories>          
        <pluginRepository>            
        <id>nexus</id>            
        <url>http://192.168.8.244:8081/nexus/content/groups/public</url>            
        <releases>             
        <enabled>true</enabled>            
        </releases>           
        <snapshots>             
        <enabled>true</enabled>           
        </snapshots>          
        </pluginRepository>         
        </pluginRepositories>      
        </profile>

        <mirror>       
     <id>tets-cent</id><!--此时的镜像  -->       
     <name>aliyun maven</name>       
     <url>http://maven.aliyun.com/nexus/content/groups/public/</url>       
     <mirrorOf>nexus</mirrorOf>             
     </mirror> 
这时会去repository中id对应的镜像里的url(仓库)http://192.168.8.244:8081/nexus/content/groups/public里下载依赖。当发现镜像里配置的url(仓库)里下载不到对应的依赖时,会自动去找到maven中默认的id为central,url为中央仓库地址的repository配置,因为central没有配置在镜像中,所以此时可以去到maven中央仓库下载依赖包

2)如果已经把默认的central仓库配置到镜像里

(1)配置了镜像后,当要下载依赖时,第一步:找到setting.xml中激活的profile下repository里id为xxx的配置,而xxx已经配置在里镜像里

(2)这个时候会去到到镜像里的url(仓库)里下载依赖

(3)当发现镜像里配置的url(仓库)里下载不到对应的依赖时,会自动去找到maven中默认的id为central,url为中央仓库地址的repository配置,

(4)此时central配置在镜像中,所以这次是去到到镜像里的url(仓库)里下载依赖了。而不会去访问maven中央仓库了。

参考:
http://blog.csdn.net/TheManOfCoding/article/details/78864965
http://blog.csdn.net/b452608/article/details/49871237

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值