剛接觸maven就是在公司里配置好的,所以一直以來使用都沒毛病,所以一直沒有去動這些固有的東西。
但是,后來把公司的電腦拿回家之后,發現有的東西就搞不起來了。原因也看一下就明白了,因為在公司的時候用的是公司的maven私服,所以回家后,用不了也是正常。
但是,真的脫離了公司,自己就不能工作了嗎?不可能吧。 難道一下開源工具都必須要依賴于公司的網絡? 這明顯是不合理的。
那么,就扯出本次文章的意義了,在家里,自然是要公有的maven倉庫了,那么,怎樣配置maven倉庫才能讓自己用起來順心呢?
1. 改掉原有的maven倉庫地址,讓maven從公網上摘取jar包下載,方便、快捷。
原私有配置示例如下:
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
|
<? 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" > <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository --> < localRepository >${user.home}/.m2/repository</ localRepository > <!--pluginGroups></pluginGroups--> <!--proxies></proxies--> < servers > < server > < id >releases</ id > < username >admin</ username > < password >123</ password > </ server > < server > < id >snapshots</ id > < username >admin</ username > < password >123</ password > </ server > </ servers > < pluginRepositories > < pluginRepository > < id >mypublic</ id > < name >Public</ name > < url >http://test.nexus.com/nexus/content/groups/public/</ url > </ pluginRepository > </ pluginRepositories > < mirrors > < mirror > < id >central</ id > < name >internal</ name > < url >http://test.nexus.com/nexus/content/groups/public/</ url > < mirrorOf >central</ mirrorOf > </ mirror > </ mirrors > < profiles > < profile > < id >nexus</ id > <!--Enable snapshots for the built in central repo to direct --> <!--all requests to nexus via the mirror --> < repositories > < repository > < id >central</ id > < url >http://central</ url > < releases >< enabled >true</ enabled ></ releases > < snapshots > < enabled >true</ enabled > < updatePolicy >always</ updatePolicy > </ snapshots > </ repository > </ repositories > < pluginRepositories > < pluginRepository > < id >central</ id > < url >http://central</ url > < releases >< enabled >true</ enabled ></ releases > < snapshots >< enabled >true</ enabled ></ snapshots > </ pluginRepository > </ pluginRepositories > </ profile > </ profiles > < activeProfiles > <!--make the profile active all the time --> < activeProfile >nexus</ activeProfile > </ activeProfiles > </ settings > |
如果想直接把私有的地方干掉,那么,這是最快的,直接把mirror的url改掉就行了,如:
1
2
3
4
5
6
7
8
9
|
< mirrors > < mirror > < id >central</ id > < name >internal</ name > < url >http://maven.aliyun.com/nexus/content/groups/public/</ url > <!-- <url>http://test.nexus.com/nexus/content/groups/public/</url> --> < mirrorOf >central</ mirrorOf > </ mirror > </ mirrors > |
當然了,到需要的地方,再把這個地址改回來就可以了,這可能是改動最小的方法了。但是也很惱火的一種配置方式,因為你要不時地記得切換(誰有那閑心)!!!
2. 添加一個類似結構的倉庫配置,這樣的話就不切來切去的了,一勞永逸。
相當于添加了多倉庫,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< mirrors > <!-- 再添加一個mirror, 注意mirrorsOf 為 * --> < mirror > < id >alimaven</ id > < name >aliyun maven</ name > < url >http://maven.aliyun.com/nexus/content/groups/public/</ url > < mirrorOf >*</ mirrorOf > </ mirror > </ mirrors > < repositories > <!-- 添加一個 repository --> < repository > < id >alimaven</ id > < url >http://alimaven</ url > < releases >< enabled >true</ enabled ></ releases > < snapshots > < enabled >true</ enabled > < updatePolicy >always</ updatePolicy > </ snapshots > </ repository > </ repositories > |
這樣的話,就不用再切換了。但是,這會導致一種情況,在某環境不可用時,maven下載jar將會很慢,嚴重影響心情,所以,其實是不建議這么干的。
3. 按照最簡單的方式,新增一個倉庫地址,隨時切換。
不用去添加mirror了,直接以url的形式,配置到reponsitory里即可,如下:
1
2
3
4
5
6
7
8
9
10
|
< repository > <!-- 直接添加一個 repository,在 activeProfiles 里加上此id 即可 --> < id >repo1</ id > < name >org.maven.repo1</ name > < layout >default</ layout > < url >https://repo1.maven.org/</ url > < snapshots > < enabled >false</ enabled > </ snapshots > </ repository > |
1
2
3
4
5
|
< activeProfiles > < activeProfile >nexus</ activeProfile > <!-- 添加此屬性,以便激活repo1的配置 --> < activeProfile >repo1</ activeProfile > </ activeProfiles > |
這樣,既不影響原來的結構,也不影響現在使用,在家的時候,可以將私有倉庫注釋掉,以提高訪問速度。
注意: 最后一個 activeProfiles 的屬性是必須的,否則你可能發現你改了配置,然而并沒有什么卵用!
1
2
3
4
|
< activeProfiles > <!-- 放心,此處的 nexus 是多個倉庫的配置 --> < activeProfile >nexus</ activeProfile > </ activeProfiles > |
4. 無法拉取包的困惑?你可能發現,你的maven無法拉取 SNAPSHOT 包,然而包明明就在那里!
是的,出于安全的考慮,maven 默認是不會去使用 snapshot 包的,所以,如果你有需要使用 snapshot 包(很多公司可能大量使用),那么你就需要配置 SNAPSHOT 為允許了!
1
2
3
4
5
6
7
8
9
10
11
12
|
< repository > < id >central</ id > < url >http://central</ url > < releases > < enabled >true</ enabled > </ releases > <!-- 如下開啟 snapshots 功能 --> < snapshots > < enabled >true</ enabled > < updatePolicy >always</ updatePolicy > </ snapshots > </ repository > |
5.一個完整的配置樣例參考
前面說的基本是從解決問題的思路去講解,可能還是不夠形象,或者每個人的理解總是有些差異,下面來一個完整的配置參考。你可以在這上面繼續任意發揮:
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
<? 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" > < localRepository >${user.home}/.m2/repository</ localRepository > < servers > <!-- 賬號信息保留,私服使用 --> < server > < id >sec@public-snapshots</ id > < username >snapshot_user</ username > < password >snapshot123</ password > </ server > < server > < id >mirror-sec</ id > < username >snapshot_user</ username > < password >snapshot123</ password > </ server > </ servers > <!-- 注釋掉該mirror 配置項 --> <!-- <mirrors> <mirror> <id>mirror-sec</id> <mirrorOf>sec@public-snapshots,mirror-sec</mirrorOf> <url>http://maven.sec-ins.com.cn/repository/maven-public/</url> </mirror> </mirrors> --> < profiles > < profile > < id >sec</ id > <!-- 注釋掉該默認激活,統一在 activeProfiles 中配置--> <!-- <activation> <activeByDefault>true</activeByDefault> </activation> --> < repositories > <!-- 直接添加一個 repository, 運行maven更新時,就會先嘗試使用該repo進行拉取了 可添加任意多個倉庫,但如果網絡不通,這樣會導致很長時間的切換重試,可在ide中查看maven正在嘗試哪個repository --> < repository > < id >repo1</ id > < name >org.maven.repo1</ name > < layout >default</ layout > < url >https://repo1.maven.org/</ url > <!-- <url>http://maven.aliyun.com/nexus/content/groups/public</url> --> < snapshots > < enabled >false</ enabled > </ snapshots > </ repository > <!-- 公司的配置保留,添加自定義的倉庫即可 --> < repository > < id >sec@public-snapshots</ id > < name >sec-snapshots</ name > < url >http://maven.sec-ins.com.cn/repository/maven-public</ url > < releases > < enabled >true</ enabled > < updatePolicy >always</ updatePolicy > < checksumPolicy >warn</ checksumPolicy > </ releases > < snapshots > < enabled >true</ enabled > </ snapshots > </ repository > </ repositories > </ profile > <!-- 注釋掉該plugin 配置 --> <!-- <pluginRepositories> <pluginRepository> <id>sec@public-snapshots</id> <name>sec_plugin</name> <url>http://maven.sec-ins.com.cn/repository/maven-public</url> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> --> <!-- 你也可以另開一個 profile, 保存自定義的 repositories, 在 activeProfiles 里加上此id 即可遍歷以下 repository --> < profile > < id >my-profile</ id > < repositories > < repository > < id >alirepo1</ id > < name >org.maven.repo1</ name > < layout >default</ layout > <!-- <url>https://repo1.maven.org/</url> --> < url >http://maven.aliyun.com/nexus/content/groups/public</ url > < snapshots > < enabled >false</ enabled > </ snapshots > </ repository > < repository > < id >spring-milestones</ id > < name >Spring Milestones</ name > < url >https://repo.spring.io/milestone</ url > < snapshots > < enabled >false</ enabled > </ snapshots > </ repository > < repository > < id >repository.springframework.maven.release</ id > < name >Spring Framework Maven Release Repository</ name > < url >http://maven.springframework.org/milestone/</ url > </ repository > < repository > < id >org.springframework</ id > < url > http://maven.springframework.org/snapshot</ url > </ repository > < repository > < id >spring-milestone</ id > < name >Spring Maven MILESTONE Repository</ name > < url >http://repo.spring.io/libs-milestone</ url > </ repository > < repository > < id >spring-release</ id > < name >Spring Maven RELEASE Repository</ name > < url >http://repo.spring.io/libs-release</ url > </ repository > </ repositories > </ profile > </ profiles > < activeProfiles > <!-- 添加此屬性,需要激活自定義的repo 的profile配置 --> < activeProfile >sec</ activeProfile > < activeProfile >my-profile</ activeProfile > </ activeProfiles > </ settings > |
6. 另附一個idea maven配置的方法
maven作為基礎輔助工具,雖不需去深入的理解,也沒有高深的技巧,但是作為沒有處理過幾個相關問題的同學,還是很有必要了解的。
到此這篇關于詳解maven配置多倉庫的方法示例的文章就介紹到這了,更多相關maven配置多倉庫內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/yougewe/p/7294265.html