您的位置:首页 > 其它

maven之Nexus的配置【setting.xml配置镜像<mirror>】(七)

2015-11-04 23:00 441 查看
前面已经讲解了配置nexus共享仓库。启动了nexus服务后,本地仓库下载jar包都是从nexus里下载,如果nexus里没有,nexus会与maven的中央仓库打交道,然后下载对应的依赖包。当关闭了nexus服务后,本地仓库就会跳过nexus,直接去maven中央仓库下载依赖包了。

如果我们不希望本地仓库直接去maven中央仓库下载,而必须要从nexus里下载依赖包,如果nexus里没有对应的依赖包,就下载不了。

要实现这种效果,需要在setting里配置镜像(<mirror>),让所有的仓库都从这个镜像的url(仓库)中下载依赖。

setting里配置了镜像以后,本地仓库就不再与nexus打交道了,而是直接与镜像中配置的的url(仓库)进行打交道。

这里说明一下,不管是在pom.xml里配置了<repositories>去指向nexus仓库,还是在setting.xml里配置<profile>去指向nexus仓库,当从本地仓库去下载依赖的时候,如果nexus里找不到对应的依赖包,会默认的去maven仓库里下载。即使是nexus服务关闭了,本地仓库还是会去maven中央仓库下载对应依赖包。这是maven里面的默认配置(maven-model-builder-3.3.3.jar里pom-4.0.0.xml文件配置了id为central的中央仓库)。

<profiles>
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>xxx</id>
<name>111</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
<layout>default</layout>
</repository>
</repositories>
</profile>

</profiles>

<activeProfiles>
<!--激活了才生效-->
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>


配置镜像后,下载依赖包的流程为:

如果没有把默认的central仓库配置到镜像里,

<mirror>
<id>mirrorId</id>
<!--表示访问哪些工厂时需要使用镜像-->
<mirrorOf>xxx</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>

流程如下:

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

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

(3)当发现镜像里配置的url(仓库)里下载不到对应的依赖时,会自动去找到maven中默认的id为central,url为中央仓库地址的repository配置,因为central没有配置在镜像中,所以此时可以直接去到maven中央仓库下载依赖包。

结果如下图所示:



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

</pre><pre name="code" class="html"><mirror>
<id>mirrorId</id>
<!--表示访问哪些工厂时需要使用镜像-->
<!--<mirrorOf>xxx,central</mirrorOf> -->
<mirrorOf>*</mirrorOf> <!--一般用*号-->
<name>Human Readable Name for this Mirror.</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>

流程如下:

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

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

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

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

结果如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven镜像配置