私服是架設在局域網的一種特殊的遠程倉庫。可以代理遠程倉庫以及部署第三方構件。
有了私服之后,當maven下載構件時,直接請求私服,私服上存在則下載到本地倉庫。否則會請求外部的遠程倉庫,將構建下載到私服,再提供給本地倉庫下載。
構建私服的軟件,我們這邊采用Sonatype Nexus
解壓縮:
在bin下執行:
./nexus.exe /run
訪問:8081端口,可以修改端口。
賬號:admin
密碼:admin123
maven-central:maven中央庫,默認從https://repo1.maven.org/maven2/拉取jar
maven-releases:私庫發行版jar
maven-snapshots:私庫快照(調試版本)jar
maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務,在本地maven基礎配置settings.xml中使用。
有些jar在中心倉庫是沒有的,如oracle的驅動。
測試第三方jar包,手動導入到私服中
可以看到已經加載進來了。
maven關聯私服
配置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一樣,這樣才可以認證通過。
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 > |
創建一個項目,添加依賴,可以看到把我們剛才手動加的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 > |
執行命令:
mvn deploy
需要等待執行完畢。
這邊就可以看見,跑到私服里面來了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/chenmz1995/p/12802474.html