Maven提高篇系列之(三)——使用自己的Repository(Nexus)
?
這是一個(gè)Maven提高篇的系列,包含有以下文章:
??
?
???
?
平時(shí)我們自己做的項(xiàng)目都是直接使用Maven提供的Central Repository,但是對(duì)于公司來(lái)說(shuō)直接使用公共的Maven Central Repository就不見(jiàn)得是件好事了,比如我們需要考慮安全問(wèn)題。此時(shí)你可以創(chuàng)建一個(gè)公司專屬的Repository(Internal Repository),公司的所有項(xiàng)目都只和這個(gè)專屬的Repository打交道,包括下載依賴,部署等。
?
總的來(lái)說(shuō),專屬Repository有以下好處:
-
代理外部Repository(比如Maven Central Repository),你可以對(duì)外部Repository做各種各樣的過(guò)濾操作,比如你可以限制只使用Spring的某個(gè)版本。
-
通過(guò)代理,專屬Repository還可以起到緩存的作用,這樣公司的每個(gè)開(kāi)發(fā)者只需要從局域網(wǎng)的專屬Repository下載依賴,而不用消耗對(duì)外網(wǎng)絡(luò)資源。
-
發(fā)布公司自己的項(xiàng)目,如果你開(kāi)發(fā)的項(xiàng)目需要被公司的其他團(tuán)隊(duì)使用,而又不能發(fā)布到公司外部的Repository中,那么專屬Repository是正中下懷的選擇。
-
發(fā)布一些購(gòu)買的第三方軟件產(chǎn)品以供公司所有人使用,比如Oracle的數(shù)據(jù)庫(kù)Driver。
?
存在多種專屬Repository,比如Nexus和Artifactory等,你甚至可以用一個(gè)FTP服務(wù)器作為一個(gè)專屬Repository。本文將以開(kāi)源的Nexus為例,演示如何將自己開(kāi)發(fā)的項(xiàng)目部署到Nexus Repository中。
?
?
(一)下載并安裝Nexus
下載Nexus的war包(本文使用的是nexus-2.5.war),將該war包放在tomcat服務(wù)器的webapps目錄下,重啟tomcat。打開(kāi)http://localhost:8080/nexus-2.5/,你將看到以下頁(yè)面:
?
?
?
點(diǎn)擊右上角的“Log In”,輸入用戶名admin,密碼admin123(這是Nexus默認(rèn)的),此后點(diǎn)擊右側(cè)的“Repositories”,顯示當(dāng)前Nexus所管理的Repository,默認(rèn)情況下Nexus為我們創(chuàng)建了以下主要的Repository:
-
Public Repositories,這是一個(gè)Repository Group,它所對(duì)應(yīng)的URL為http://localhost:8080/nexus-2.5/content/groups/public/,該Repository ?Group包含了多個(gè)Repository,其中包含了Releases、Snapshots、Third Party和Central。Repository Group的作用是我們只需要在自己的項(xiàng)目中配置該Repository Group就行了,它將自動(dòng)從其所包含的Repository中下載依賴,比如如果我們聲明對(duì)Spring的依賴,那么根據(jù)Repository Group中各個(gè)Repository的順序(可以配置),Nexus將首先從Releases中下載Spring,發(fā)現(xiàn)沒(méi)有,再?gòu)腟napshots中下載(極大可能也沒(méi)有,因?yàn)樗莻€(gè)Snapshots的Repository),依次查找,最后可能在Central Repository中找到。在配置項(xiàng)目的Repository時(shí),我們應(yīng)該首先考慮Public Repositories。
-
3rd party,該Repository即是存放你公司所購(gòu)買的第三方軟件庫(kù)的地方,它是一個(gè)由Nexus自己維護(hù)的一個(gè)Repository。
-
Apache Snapshots,看名字你就應(yīng)該知道這是個(gè)什么樣的Repository,這是一個(gè)代理Repository,即最終的依賴還是得在Apache官網(wǎng)上去下載,然后緩存在Nexus中。
-
Central,這就是代理Maven Central Repository的Repository。
-
Releases,你自己的項(xiàng)目要發(fā)布時(shí),就應(yīng)該發(fā)布在這個(gè)Repository,他也是Nexus自己維護(hù)的Repository,而不是代理。
-
Snapshots,你自己項(xiàng)目Snapshot的Repository。
??
(二)用Nexus Repository取代Maven Central Repository
在完成(一)之后,我們只是創(chuàng)建了一個(gè)專屬的Nexus Repository,我們的項(xiàng)目默認(rèn)還是使用的Maven Central Repository,所以這時(shí)我們需要將Maven Central Repository換成自己創(chuàng)建的Nexus Repository,可以通過(guò)修改~/.m2/settings.xml來(lái)達(dá)到這樣的目的。在該settings.xml文件中(沒(méi)有的話可以創(chuàng)建一個(gè)),加入以下配置:
?
<mirrors><mirror><!--This sends everything else to /public --><id>nexus</id><mirrorOf>*</mirrorOf><url>http://localhost:8080/nexus-2.5/content/groups/public</url></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></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>?
?
以上配置通過(guò)mirror和profile的方式將central Repository全部轉(zhuǎn)向到我們自己的Public Repositories,包括release版本和snapshot版本(Maven默認(rèn)允許從Maven Central Repository下載release版本,這是合理的,如果你使用了別人的snapshot版本,一邊你在使用,一邊別人在開(kāi)發(fā),別人將API簽名一換,哦豁)。這樣一來(lái),我們項(xiàng)目中的所有依賴都從Nexus的Public Repositories下載,由于其中包含了對(duì)Maven Central Repository的代理,所以此時(shí)Maven Central Repository中的類庫(kù)也是可以間接下載到的。此時(shí)你可以將~/.m2/repository/中所有的內(nèi)容刪除掉,再在項(xiàng)目中執(zhí)行“mvn clean install”,你將在終端中看到Maven已經(jīng)開(kāi)始從Nexus 的Public Repositories中下載依賴了,比如:
?
Downloading: http://localhost:8080/nexus-2.5/content/groups/public/org/codehaus/plexus/plexus-archiver/1.2/plexus-archiver-1.2.pom?
?
請(qǐng)注意,此時(shí)我們的項(xiàng)目本身不需要做任何修改。我們只是創(chuàng)建了另一個(gè)Repository和修改了Maven的默認(rèn)配置(學(xué)習(xí)完本文后,你應(yīng)該需要將~/.m2/settings.xml還原,不然如果下次在構(gòu)建之前自己的Nexus服務(wù)器沒(méi)有啟動(dòng),構(gòu)建將失敗。)。
?
?
(三)在項(xiàng)目中配置Nexus Repository的信息
接下來(lái),我們開(kāi)始著手如何將自己的項(xiàng)目部署到Nexus Repository中。這個(gè)也簡(jiǎn)單,第一我們需要在項(xiàng)目中指明部署目的Repository的URL,第二我們需要提供用戶名和密碼,哪能讓你胡來(lái)。
?
我們知道,對(duì)于一個(gè)Maven項(xiàng)目而言,如果你的項(xiàng)目版本號(hào)中有“SNAPSHOT”字樣,則表示此時(shí)的項(xiàng)目是snapshot版本,即處于開(kāi)發(fā)中。否則,Maven則認(rèn)為這是一個(gè)release版本。所以我們?cè)诓渴饡r(shí),需要分別配置這兩種發(fā)布版本所對(duì)應(yīng)的Repository。在項(xiàng)目的pom.xml文件中配置需要發(fā)布的目標(biāo)Repository:
?? ?
<distributionManagement><repository><id>releases</id><name>Nexus Release Repository</name><url>http://localhost:8080/nexus-2.5/content/repositories/releases/</url></repository><snapshotRepository><id>snapshots</id><name>Nexus Snapshot Repository</name><url>http://localhost:8080/nexus-2.5/content/repositories/snapshots/</url></snapshotRepository></distributionManagement>?
?
在上面的配置中,我們分別配置了release版本和snapshot版本所對(duì)應(yīng)的Repository,如果你項(xiàng)目的版本中包含了“SNAPSHOT”,此時(shí)將發(fā)布到Nexus的Snapshots Repository,否則發(fā)布在Releases Repository。
?
至于提供用戶名和密碼,這些信息當(dāng)然不能放在項(xiàng)目中,一是不安全,另外不同的人可能有不同的用戶名和密碼,你不至于在每次部署時(shí)都修改一次吧。所以,Maven將這些信息的存放地點(diǎn)放在了~/.m2/settings.xml文件中,每臺(tái)機(jī)器(基本上就是每個(gè)人啦)都可以有不同的settings.xml文件。在該文件中加入以下配置:
?
<servers> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers>?
?
admin和admin123都是Nexus默認(rèn)的,另外特別需要注意的是,這里的<id>需要和上面項(xiàng)目pom.xml文件中配置Repostory的<id>對(duì)應(yīng)起來(lái)。
?
(四)發(fā)布到Nexus Repository
萬(wàn)事具備,只欠東風(fēng),在項(xiàng)目中執(zhí)行:
?
mvn deploy??
?
部署成功,再在Nexus中查看部署情況:
?
此時(shí)我們部署的是項(xiàng)目的snapshot版本,上圖中的“me”目錄中既包含了作者所部署的項(xiàng)目。
在下一篇中,我們將講到如何使用Profile。
總結(jié)
以上是生活随笔為你收集整理的Maven提高篇系列之(三)——使用自己的Repository(Nexus)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 集群监控系统的设计方案
- 下一篇: winform空间批量控制