私服是架設(shè)在局域網(wǎng)的一種特殊的遠(yuǎn)程倉庫。可以代理遠(yuǎn)程倉庫以及部署第三方構(gòu)件。
有了私服之后,當(dāng)maven下載構(gòu)件時,直接請求私服,私服上存在則下載到本地倉庫。否則會請求外部的遠(yuǎn)程倉庫,將構(gòu)建下載到私服,再提供給本地倉庫下載。
構(gòu)建私服的軟件,我們這邊采用Sonatype Nexus
官網(wǎng):https://blog.sonatype.com/
解壓縮:
在bin下執(zhí)行:
./nexus.exe /run
訪問:8081端口,可以修改端口。
賬號:admin
密碼:admin123
maven-central:maven中央庫,默認(rèn)從https://repo1.maven.org/maven2/拉取jar
maven-releases:私庫發(fā)行版jar
maven-snapshots:私庫快照(調(diào)試版本)jar
maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務(wù),在本地maven基礎(chǔ)配置settings.xml中使用。
有些jar在中心倉庫是沒有的,如oracle的驅(qū)動。
測試第三方j(luò)ar包,手動導(dǎo)入到私服中
可以看到已經(jīng)加載進(jìn)來了。
maven關(guān)聯(lián)私服
配置maven的setting文件:
1)配置賬號密碼
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
|
|--> < servers > <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> --> <!-- Another sample, using keys to authenticate. <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> --> < server > < id >nexus-public</ id > < username >admin</ username > < password >admin123</ password > </ server > </ servers > |
2)配置profile, 在<profiles></profiles>中添加, 這邊配置repository的id需要跟上面的server配置的id一樣,這樣才可以認(rèn)證通過。
1
2
3
4
5
6
7
8
9
10
11
12
|
< profile > < id >nexus</ id > < repositories > < repository > < id >nexus-public</ id > < name >private reposity</ name > < url >http://localhost:8081/repository/maven-public/</ url > < layout >default</ layout > < snapshotPolicy >always</ snapshotPolicy > </ repository > </ repositories > </ profile > |
3)使profile生效
1
2
3
|
< activeProfiles > < activeProfile >nexus</ activeProfile > </ activeProfiles > |
創(chuàng)建一個項目,添加依賴,可以看到把我們剛才手動加的jar給依賴過來了。
把maven項目部署到私服
這邊repository中配置的id需要跟maven setting中配置的server的id需要一樣。需要在本項目的pom.xml添加如下配置。
1
2
3
4
5
6
7
8
9
10
11
12
|
< distributionManagement > < repository > < id >nexus-public</ id > < name >core release repository</ name > < url >http://localhost:8081/repository/maven-releases/</ url > </ repository > < snapshotRepository > < id >nexus-public</ id > < name >core snapshots repository</ name > < url >http://localhost:8081/repository/maven-snapshots/</ url > </ snapshotRepository > </ distributionManagement > |
執(zhí)行命令:
mvn deploy
需要等待執(zhí)行完畢。
這邊就可以看見,跑到私服里面來了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/chenmz1995/p/12802474.html