本文主要介紹了maven實(shí)戰(zhàn)之搭建maven私服和鏡像的方法(圖文),分享給大家,具體如下:
準(zhǔn)備工作
安裝3.6.*的nexus
理論
nexus的倉(cāng)庫(kù)類(lèi)型
- hosted(宿主):宿主倉(cāng)庫(kù)主要用于存放項(xiàng)目部署的構(gòu)件、或者第三方構(gòu)件用于提供下載。
- proxy(代理):代理倉(cāng)庫(kù)就是對(duì)遠(yuǎn)程倉(cāng)庫(kù)的一種代理,從遠(yuǎn)程倉(cāng)庫(kù)下載構(gòu)件和插件然后緩存在nexus倉(cāng)庫(kù)中
- group(倉(cāng)庫(kù)組):倉(cāng)庫(kù)的一種組合策略,并不存在實(shí)在意義的依賴(lài),只是作為一種中轉(zhuǎn)站的作用存在。
nexus內(nèi)置倉(cāng)庫(kù)類(lèi)型
- maven-central:代理中央倉(cāng)庫(kù)、策略為release、只會(huì)下載和緩存中央倉(cāng)庫(kù)中的發(fā)布版本構(gòu)件。
- maven-releases:策略為release的宿主倉(cāng)庫(kù)、用來(lái)部署組織內(nèi)部的發(fā)布版本內(nèi)容。
- maven-snapshots:策略為snapshot的宿主倉(cāng)庫(kù)、用來(lái)部署組織內(nèi)部的快照版本內(nèi)容。
- maven-public:該倉(cāng)庫(kù)將上述所有策略為release的倉(cāng)庫(kù)聚合并通過(guò)一致的地址提供服務(wù)。
- nuget-hosted:用來(lái)部署nuget構(gòu)件的宿主倉(cāng)庫(kù)
- nuget.org-proxy:代理nuget遠(yuǎn)程倉(cāng)庫(kù),下載和緩沖nuget構(gòu)件。
- nuget-group:該倉(cāng)庫(kù)組將nuget-hosted與nuget.org-proxy倉(cāng)庫(kù)聚合并通過(guò)一致的地址提供服務(wù)。
- maven-public:該倉(cāng)庫(kù)組將maven-central,maven-releases與maven-snapshots倉(cāng)庫(kù)聚合并通過(guò)一致的地址提供服務(wù)。
如下圖
搭建私服流程
- 打開(kāi)nexus地址,賬號(hào)密碼默認(rèn)【admin】【admin123】
- 點(diǎn)擊【create repository】,選擇【maven2(hosted)】類(lèi)型,如下圖填寫(xiě)相關(guān)信息
按步驟二構(gòu)建一個(gè)【testsnapshot】倉(cāng)庫(kù)
構(gòu)建一個(gè)maven項(xiàng)目,在pom文件里面,配置以下信息:
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
|
<distributionmanagement> <repository> <id>releases</id> <url>http: //192.168.0.1:8888/repository/testrelease/</url> </repository> <snapshotrepository> <id>snapshots</id> <url>http: //192.168.0.1:8888/repository/testsnapshot/</url> </snapshotrepository> <!-- 這邊url就是倉(cāng)庫(kù)的路徑 --> </distributionmanagement> <build> <plugins> <!-- 打jar包插件 --> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <version> 3.0 . 2 </version> <configuration> <excludes> <exclude>**/*.properties</exclude> </excludes> </configuration> </plugin> <!-- 打包源碼插件 --> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-source-plugin</artifactid> <version> 3.0 . 1 </version> <configuration> <attach> true </attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
配置在maven的setting文件,倉(cāng)庫(kù)用戶(hù)賬號(hào)密碼
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<servers> <server> <id>release</id> <username>user</username> <password> 123456 </password> </server> <server> <id>snapshots</id> <username>user</username> <password> 123456 </password> </server> </servers> //這邊id需保持和上面?zhèn)}庫(kù)id一致 |
輸入mvn命令打包上傳
1
2
|
mvn clean source:jar package mvn deploy -e |
查看nexus的【components】,如下便是成功
引用如下,在引用項(xiàng)目的pom.xml中配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!-- nexus私服配置 --> <repositories> <repository> <id>nexus</id> <name>nexus repository</name> <url>http: //192.168.2.20:8081/repository/java/</url> <releases> <enabled> true </enabled> </releases> <!--snapshots默認(rèn)是關(guān)閉的,需要開(kāi)啟 --> <snapshots> <enabled> true </enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupid>com.example</groupid> <artifactid>utils</artifactid> <version> 1.0 </version> </dependency> </dependencies> |
搭建鏡像流程
- 現(xiàn)在就是結(jié)合倉(cāng)庫(kù)組,來(lái)處理各種請(qǐng)求從代理倉(cāng)庫(kù)或者宿主倉(cāng)庫(kù)獲取實(shí)際構(gòu)件內(nèi)容。
- 創(chuàng)建【倉(cāng)庫(kù)組】testgroup和【代理倉(cāng)庫(kù)】testproxy
- 在【倉(cāng)庫(kù)組】的members添加【代理倉(cāng)庫(kù)】testproxy【宿主倉(cāng)庫(kù)】testrelease和testsnapshot。引用的順序會(huì)決定倉(cāng)庫(kù)組遍歷倉(cāng)庫(kù)的順序。
在maven的setting配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<mirrors> <mirror> <id>mirror</id> <mirrorof>*</mirrorof> <name>nexus aliyun</name> <url>http: //116.62.220.224:8888/repository/testgroup/</url> </mirror> </mirrors> <servers> <server> <id>mirror</id> <username>user</username> <password> 123456 </password> </server> </servers> |
這樣所有maven項(xiàng)目都會(huì)從這個(gè)鏡像引用依賴(lài)
總結(jié)
鏡像相當(dāng)于起到攔截和請(qǐng)求轉(zhuǎn)發(fā)的作用,而私服則起到加速下載構(gòu)建和存儲(chǔ)第三方構(gòu)建,節(jié)省本地maven配置等作用,二者結(jié)合可以使maven的優(yōu)點(diǎn)體現(xiàn)地淋漓盡致。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/Gred01/article/details/78724660