一籽孙、為什么要搭建私服nexus
1.本地開(kāi)發(fā)機(jī)器上安裝nexus
首先聲明公司內(nèi)部是有自己的nexus倉(cāng)庫(kù)攒岛,但是對(duì)上傳jar包做了限制夜焦,不能暢快的上傳自己測(cè)試包依賴懂傀。于是就自己在本地搭建了一個(gè)nexus私服,即可以使用公司nexus私服倉(cāng)庫(kù)中的依賴,也可以上傳和使用自己的測(cè)試包依賴枣察。
2.公司搭建私服作用:
原因很簡(jiǎn)單,有些公司都不提供外網(wǎng)給項(xiàng)目組人員燃逻,因此就不能使用maven訪問(wèn)遠(yuǎn)程的倉(cāng)庫(kù)地址序目,所以很有必要在局域網(wǎng)里找一臺(tái)有外網(wǎng)權(quán)限的機(jī)器,搭建nexus私服伯襟,然后開(kāi)發(fā)人員連到這臺(tái)私服上猿涨,這樣的話就可以通過(guò)這臺(tái)搭建了nexus私服的電腦訪問(wèn)maven的遠(yuǎn)程倉(cāng)庫(kù)。還有就是公司有自己的jar可以發(fā)布到私服上等等逗旁。
此文章測(cè)試搭建的是本地開(kāi)發(fā)機(jī)器上安裝nexus嘿辟,無(wú)論是本地還是公司私服的搭建,都是一樣的片效。
二红伦、環(huán)境搭建
1.首先確定我們的環(huán)境安裝好maven,jdk等必須的環(huán)境
2.這些都準(zhǔn)備好之后淀衣,去下載最新版本的nexus
下載地址:http://www.sonatype.org/nexus/go
三昙读、解壓
將下載的nexus-3.14.0-04-win64.zip解壓到自定義目錄即可。
四膨桥、配置nexus的端口和上下文路徑
打開(kāi)zip解壓文件下的 ../nexus-3.14.0-04-win64/nexus-3.14.0-04/etc/nexus-default.properties蛮浑。
1.如下屬性可以自定義修改。
application-host : Nexus服務(wù)監(jiān)聽(tīng)的主機(jī)
application-port: Nexus服務(wù)監(jiān)聽(tīng)的端口只嚣,
nexus-context-path : Nexus服務(wù)的上下文路徑
通尘谥桑可以不做任何修改,但個(gè)人習(xí)慣于修改 application-host 為0.0.0.0(關(guān)于0.0.0.0與127.0.0.1的區(qū)別自行檢索)册舞,我這里修改了端口和host蕴掏。
五、運(yùn)行環(huán)境配置
打開(kāi)解壓目錄下的 ../nexus-3.14.0-04-win64/nexus-3.14.0-04/bin/nexus.vmoptions
可以在下圖配置運(yùn)行時(shí)的最大堆、最小堆等盛杰,可以根據(jù)個(gè)人的電腦以及需要修改挽荡,默認(rèn)配置如下。
六即供、nexus安裝
在.../nexus-3.14.0-04-win64/nexus-3.14.0-04/bin 目錄下定拟,
必須以管理員身份運(yùn)行cmd :
- nexus.exe /run 命令可以啟動(dòng)nexus服務(wù)(參考官方文檔)
- 安裝nexus本地服務(wù)來(lái)啟動(dòng)(推薦使用這種方式,參考官方文檔),命令如下所示逗嫡。
安裝nexus服務(wù)
PS D:\Nexus\nexus-3.14.0-04\bin> ./nexus.exe /install //安裝nexus服務(wù)
七青自、啟動(dòng)/關(guān)閉nexus服務(wù)
必須以管理員身份運(yùn)行cmd :
PS D:\Nexus\nexus-3.14.0-04\bin> ./nexus.exe /start //啟動(dòng)nexus服務(wù)
PS D:\Nexus\nexus-3.14.0-04\bin> ./nexus.exe /stop //停止nexus服務(wù)
八、登錄
如果沒(méi)有做任何端口和上下文路徑的修改祸穷,直接訪問(wèn) http://localhost:8081即可性穿。
我這里更改成 http://127.0.0.1:8181
默認(rèn)的用戶名和密碼分別是:admin/amdin123
修改密碼:
九、nexus倉(cāng)庫(kù)類型介紹
默認(rèn)安裝有以下這幾個(gè)倉(cāng)庫(kù)雷滚,在控制臺(tái)也可以修改遠(yuǎn)程倉(cāng)庫(kù)的地址需曾,第三方倉(cāng)庫(kù)等。
十祈远、分組倉(cāng)庫(kù)的使用
如上圖所示呆万,maven-public就我創(chuàng)建的組倉(cāng)庫(kù)。以及還創(chuàng)建了3個(gè)代理倉(cāng)庫(kù)车份,如下谋减。
配置代理倉(cāng)庫(kù):
1、jcenter倉(cāng)庫(kù):https://jcenter.bintray.com/
創(chuàng)建過(guò)程:(其余一樣)
2扫沼、maven中央倉(cāng)庫(kù):https://repo1.maven.org/maven2/
一般默認(rèn)已經(jīng)創(chuàng)建出爹,沒(méi)有創(chuàng)建就和上面一樣1過(guò)程。
3缎除、公司內(nèi)部nexus倉(cāng)庫(kù)严就,這里就不給出了 ---可以選擇配置
創(chuàng)建公司的代理
4、最后建立組倉(cāng)庫(kù)maven-public器罐,如下梢为。
(1) 將創(chuàng)建的倉(cāng)庫(kù)添加進(jìn)倉(cāng)庫(kù)組。
組倉(cāng)庫(kù)中包含了公司私服dist轰坊、jcenter铸董、maven-central、本地maven-releases肴沫,本地maven-snapshots粟害。
修改maven配置setting.xml文件
5.創(chuàng)建好組倉(cāng)庫(kù)之后,修改maven配置setting.xml文件颤芬,添加maven倉(cāng)庫(kù)鏡像我磁,如下孽文。
Nexus的倉(cāng)庫(kù)對(duì)于匿名用戶只是只讀的。為了能夠部署構(gòu)件夺艰,我們還需要再settings.xml中配置驗(yàn)證信息:
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
配置遠(yuǎn)程倉(cāng)庫(kù)下載jar地址
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://127.0.0.1:8181/repository/maven-public/</url>
</mirror>
接著修改maven項(xiàng)目中的pom.xml,如下沉衣。
6.不配置setting.xml中server就沒(méi)有權(quán)限上傳jar郁副,只能下載jar。
這個(gè)配置可以下載jar和部署構(gòu)建
<!--============== 配置nexus私服START =============== -->
<repositories>
<repository>
<id>maven-central</id>
<name>maven-central</name>
<!--倉(cāng)庫(kù)地址-->
<url>http://127.0.0.1:8181/repository/maven-central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<!--插件地址-->
<id>nexus</id>
<url>http://127.0.0.1:8181/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>nexus</id>
<url>http://127.0.0.1:8181/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<!--上傳快照-->
<id>nexus</id>
<name>Nexus Snapshot</name>
<url>http://127.0.0.1:8181/repository/maven-snapshots/</url>
</snapshotRepository>
<site>
<id>nexus</id>
<name>Nexus Sites</name>
<url>dav:http://127.0.0.1:8181/repository/maven-snapshots/</url>
</site>
</distributionManagement>
<!--============== 配置私服End =============== -->
部署構(gòu)件到私服
(1)Nexus的倉(cāng)庫(kù)對(duì)于匿名用戶只是只讀的豌习。為了能夠部署構(gòu)件存谎,我們還需要再settings.xml中配置驗(yàn)證信息:
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
其中,驗(yàn)證信息中service的id應(yīng)該與POM中repository的id一致
(2)發(fā)布到私服的配置
<!-- 配置遠(yuǎn)程發(fā)布到私服肥隆,mvn deploy -->
<distributionManagement>
<repository>
<id>releases</id>
<name> Nexus Release Repository </name>
<url> http://127.0.0.1:8081/nexus/content/repositories/releases/ </url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name> Nexus Snapshot Repository </name>
<url> http://127.0.0.1:8081/nexus/content/repositories/snapshots/ </url> </snapshotRepository >
</distributionManagement >
部署命令:
還可以這么配
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8082/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8082/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
如果是gradle項(xiàng)目既荚,修改init.gradle文件,如下栋艳。
uploadArchives {
def nexus_credentials = [userName: "admin", password: "admin123"]
repositories.mavenDeployer {
snapshotRepository(url: "http://127.0.0.1:8082/repository/maven-snapshots/") {
authentication(nexus_credentials)
}
repository(url: "http://127.0.0.1:8082/repository/maven-releases/") {
authentication(nexus_credentials)
}
}
}
十一恰聘、上傳jar的兩種方式:
比如上傳這個(gè)jar
nexus提供了3rd party、Snapshots吸占、Releases這三個(gè)目錄存放第三方j(luò)ar包
(1)登錄nexus(默認(rèn)用戶名密碼/admin晴叨、admin123)
(2)Create repository
解釋一下:
proxy:即你可以設(shè)置代理,設(shè)置了代理之后矾屯,在你的nexus中找不到的依賴就會(huì)去配置的代理的地址中找 hosted:你可以上傳你自己的項(xiàng)目到這里面 group:它可以包含前面兩個(gè)兼蕊,是一個(gè)聚合體。一般用來(lái)給客戶一個(gè)訪問(wèn)nexus的統(tǒng)一地址件蚕。 簡(jiǎn)單的說(shuō)孙技,就是你可以上傳私有的項(xiàng)目到hosted,以及配置proxy以獲取第三方的依賴(比如可以配置中央倉(cāng)庫(kù)的地址)排作。前面兩個(gè)都 弄好了之后牵啦,在通過(guò)group聚合給客戶提供統(tǒng)一的訪問(wèn)地址。
修改maven安裝目錄下的 /conf/settings.xml 文件纽绍,添加server 節(jié)點(diǎn)蕾久。如圖:
1.方式一
開(kāi)始上傳jar
打開(kāi)cmd輸入修改后的模板
模板
mvn deploy:deploy-file -DgroupId=xxx.xxx -DartifactId=xxx -Dversion=0.0.2 -Dpackaging=jar -Dfile=D:\xxx.jar -Durl=http://xxx.xxx.xxx.xxx:8081/repository/3rdParty/ -DrepositoryId=3rdParty
模板解析:
-DgroupId 為上傳的jar的groupId-DartifactId 為上傳的jar的artifactId-Dversion 為上傳的jar的需要被依賴的時(shí)候的版本號(hào)-Dpackaging為jar,-Dfile為jar包路徑-Durl 為要上傳的路徑拌夏,可以通過(guò)以下方式獲取到
舉個(gè)例子:
C:\Users\Administrator>mvn deploy:deploy-file -DgroupId=dist.xdata.product -DartifactId=distexcel -Dversion=1.0.1.RELEASE -Dpackaging=jar -Dfile=I:\maven\repository\dist\xdata\product\distexcel\1.0.1.RELEASE\distexcel-1.0.1.RELEASE.jar -Durl=http://192.168.2.81:8181/repository/3rd-party/ -DrepositoryId=3rd-party
mvn deploy:deploy-file -DgroupId=dist.xdata.product -DartifactId=distexcel -Dversion=1.0.1.RELEASE -Dpackaging=jar -Dfile=I:\maven\repository\dist\xdata\product\distexcel\1.0.1.RELEASE\distexcel-1.0.1.RELEASE.jar -Durl=http://192.168.2.81:8181/repository/3rd-party/ -DrepositoryId=3rd-party
上傳成功:
上傳后在nexus中查看:
2.上傳方式二:
更簡(jiǎn)單的上傳方式
nexus提供了3rd party僧著、Snapshots、Releases這三個(gè)目錄存放第三方j(luò)ar包
十二障簿、nexus3.x權(quán)限配置
1.去掉“勾”是禁用匿名訪權(quán)限盹愚。
2.角色創(chuàng)建
3.人員創(chuàng)建
4.代理配置
通過(guò)該配置可以使得兩個(gè)Nexus服務(wù)器相關(guān)聯(lián)。
(1)配置地址
(2)配置用戶