前面已经讲解了配置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的中央仓库)。
[html] view plain copy
1. <profiles>
2. <profile>
3. <id>nexusProfile</id>
4. <repositories>
5. <repository>
6. <id>xxx</id>
7. <name>111</name>
8. <url>http://localhost:8081/nexus/content/groups/public/</url>
9. <snapshots>
10. <enabled>true</enabled>
11. </snapshots>
12. <releases>
13. <enabled>true</enabled>
14. </releases>
15. <layout>default</layout>
16. </repository>
17. </repositories>
18. </profile>
19.
20. </profiles>
21.
22. <activeProfiles>
23. <!--激活了才生效-->
24. <activeProfile>nexusProfile</activeProfile>
25. </activeProfiles>
配置镜像后,下载依赖包的流程为:
如果没有把默认的central仓库配置到镜像里,
[html] view plain copy
1. <mirror>
2. <id>mirrorId</id>
3. <!--表示访问哪些工厂时需要使用镜像-->
4. <mirrorOf>xxx</mirrorOf>
5. <name>Human Readable Name for this Mirror.</name>
6. <url>http://localhost:8081/nexus/content/groups/public/</url>
7. </mirror>
流程如下:
(1)配置了镜像后,当要下载依赖时,第一步:找到setting.xml中激活的profile下repository里id为xxx的配置,而xxx已经配置在里镜像里
(2)这时会去到到镜像里的url(仓库)里下载依赖
(3)当发现镜像里配置的url(仓库)里下载不到对应的依赖时,会自动去找到maven中默认的id为central,url为中央仓库地址的repository配置,因为central没有配置在镜像中,所以此时可以直接去到maven中央仓库下载依赖包。
结果如下图所示:
如果已经把默认的central仓库配置到镜像里,
[html] view plain copy
1. </pre><pre name="code" class="html"><mirror>
2. <id>mirrorId</id>
3. <!--表示访问哪些工厂时需要使用镜像-->
4. <!--<mirrorOf>xxx,central</mirrorOf> -->
5. <mirrorOf>*</mirrorOf> <!--一般用*号-->
6. <name>Human Readable Name for this Mirror.</name>
7. <url>http://localhost:8081/nexus/content/groups/public/</url>
8. </mirror>
流程如下:
(1)配置了镜像后,当要下载依赖时,第一步:找到setting.xml中激活的profile下repository里id为xxx的配置,而xxx已经配置在里镜像里
(2)这个时候会去到到镜像里的url(仓库)里下载依赖
(3)当发现镜像里配置的url(仓库)里下载不到对应的依赖时,会自动去找到maven中默认的id为central,url为中央仓库地址的repository配置,
(4)此时central配置在镜像中,所以这次是去到到镜像里的url(仓库)里下载依赖了。而不会去访问maven中央仓库了。
结果如下图: