Maven配置 settings.xml

本地仓库是远程仓库的一个缓冲和子集,当你构建Maven项目的时候,首先会从本地仓库查找资源,如果没有,那么Maven会从远程仓库下载到你本地仓库。这样在你下次使用的时候就不需要从远程下载了。如果你所需要的jar包版本在本地仓库没有,而且也不存在于远程仓库,Maven在构建的时候会报错,这种情况可能发生在有些jar包的新版本没有在Maven仓库中及时更新。

Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。当然你可以通过修改${user.home}/.m2/settings.xml 配置这个地址:

Xml代码

  1. <settings>   

  2.   …   

  3.   <localRepository> D:/java/repository</localRepository>   

  4.   …   

  5. </settings>   

如果你想让所有的用户使用统一的配置那么你可以修改Maven主目录下的setting.xml:

${M2_HOME}/conf/setting.xml

repository是指在局域网内部搭建的repository,它跟central repository, jboss repository等的区别仅仅在于其URL是一个内部网址 
mirror则相当于一个代理,它会拦截去指定的远程repository下载构件的请求,然后从自己这里找出构件回送给客户端。配置mirror的目的一般是出于网速考虑。 

不过,很多internal repository搭建工具往往也提供mirror服务,比如Nexus就可以让同一个URL,既用作internal repository,又使它成为所有repository的mirror。

高级的镜像配置: 
1.<mirrorOf>*</mirrorOf> 
匹配所有远程仓库。 这样所有pom中定义的仓库都不生效
2.<mirrorOf>external:*</mirrorOf> 
匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。 
3.<mirrorOf>repo1,repo2</mirrorOf> 
匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。 
4.<mirrorOf>*,!repo1</miiroOf> 
匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。 


mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性,id是唯一标识一个mirror就不多说了,name貌似没多大用,相当于描述,url是官方的库地址,mirrorOf代表了一个镜像的替代位置,例如central就表示代替官方的中央库。


我本以为镜像库是一个分库的概念,就是说当a.jar在第一个mirror中不存在的时候,maven会去第二个mirror中查询下载。但事实却不是这样,当第一个mirror中不存在a.jar的时候,并不会去第二个mirror中查找,甚至于,maven根本不会去其他的mirror地址查询。


后来终于知道,maven的mirror是镜像,而不是“分库”,只有当前一个mirror无法连接的时候,才会去找后一个,类似于备份和容灾。

还有,mirror也不是按settings.xml中写的那样的顺序来查询的。

所谓的第一个并不一定是最上面的那个。


当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询。



?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< settings  xmlns = "http://maven.apache.org/SETTINGS/1.0.0"
           xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" >
 
     < servers >
         < server >
             < id >repo-iss</ id >
             < username >deployment</ username >
             < password >deployment123</ password >
         </ server >
     </ servers >
 
     < mirrors >
         <!-- osc镜像 -->
         < mirror >
             <!-- 镜像所有远程仓库,但不包括指定的仓库 -->
             < id >mirror-osc</ id >
             < mirrorOf >external:*,!repo-osc-thirdparty,!repo-iss</ mirrorOf >
             < url >http://maven.oschina.net/content/groups/public/</ url >
         </ mirror >
<!--
         <mirror>
             <id>mirror-iss</id>
             <mirrorOf>external:*</mirrorOf>
             <url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
         </mirror>
-->
     </ mirrors >
 
     < profiles >
         < profile >
             < id >profile-default</ id >
             < repositories >
                 < repository >
                     < id >central</ id >
                     < url >http://central</ url >
                     < releases >
                         < enabled >true</ enabled >
                     </ releases >
                     < snapshots >
                         < enabled >false</ enabled >
                     </ snapshots >
                 </ repository >
                 < repository >
                     < id >repo-osc-thirdparty</ id >
                     < url >http://maven.oschina.net/content/repositories/thirdparty/</ url >
                     < releases >
                         < enabled >true</ enabled >
                     </ releases >
                     < snapshots >
                         < enabled >false</ enabled >
                     </ snapshots >
                 </ repository >
             </ repositories >
             < pluginRepositories >
                 < pluginRepository >
                     < id >central</ id >
                     < url >http://central</ url >
                     < releases >
                         < enabled >true</ enabled >
                     </ releases >
                     < snapshots >
                         < enabled >false</ enabled >
                     </ snapshots >
                 </ pluginRepository >
             </ pluginRepositories >
         </ profile >
         < profile >
             < id >profile-iss</ id >
             < repositories >
                 < repository >
                     < id >repo-iss</ id >
                     < url >http://10.24.16.99:5555/nexus/content/groups/public/</ url >
                     < releases >
                         < enabled >true</ enabled >
                     </ releases >
                     < snapshots >
                         < enabled >true</ enabled >
                     </ snapshots >
                 </ repository >
             </ repositories >
             < pluginRepositories >
                 < pluginRepository >
                     < id >repo-iss</ id >
                     < url >http://10.24.16.99:5555/nexus/content/groups/public/</ url >
                     < releases >
                         < enabled >true</ enabled >
                     </ releases >
                     < snapshots >
                         < enabled >true</ enabled >
                     </ snapshots >
                 </ pluginRepository >
             </ pluginRepositories >
         </ profile >
     </ profiles >
 
     < activeProfiles >
         < activeProfile >profile-default</ activeProfile >
         <!--<activeProfile>profile-iss</activeProfile>-->
     </ activeProfiles >
 
<!--
     <proxies>
         <proxy>
             <active>true</active>
             <protocol>http</protocol>
             <host>10.10.204.160</host>
             <port>80</port>
         </proxy>
     </proxies>
-->
</ settings >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值