日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maven提高篇系列之(三)——使用自己的Repository(Nexus)

發(fā)布時間:2024/4/17 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maven提高篇系列之(三)——使用自己的Repository(Nexus) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

這是一個Maven提高篇的系列,包含有以下文章:

??

  • Maven提高篇系列之(一)——多模塊 vs 繼承
  • Maven提高篇系列之(二)——配置Plugin到某個Phase(以Selenium集成測試為例)
  • Maven提高篇系列之(三)——使用自己的Repository(Nexus)
  • Maven提高篇系列之(四)——使用Profile
  • Maven提高篇系列之(五)——處理依賴沖突
  • Maven提高篇系列之(六)——編寫自己的Plugin(本系列完)
  • ?

    ???

    ?

    平時我們自己做的項目都是直接使用Maven提供的Central Repository,但是對于公司來說直接使用公共的Maven Central Repository就不見得是件好事了,比如我們需要考慮安全問題。此時你可以創(chuàng)建一個公司專屬的Repository(Internal Repository),公司的所有項目都只和這個專屬的Repository打交道,包括下載依賴,部署等。

    ?

    總的來說,專屬Repository有以下好處:

    • 代理外部Repository(比如Maven Central Repository),你可以對外部Repository做各種各樣的過濾操作,比如你可以限制只使用Spring的某個版本。

    • 通過代理,專屬Repository還可以起到緩存的作用,這樣公司的每個開發(fā)者只需要從局域網(wǎng)的專屬Repository下載依賴,而不用消耗對外網(wǎng)絡(luò)資源。

    • 發(fā)布公司自己的項目,如果你開發(fā)的項目需要被公司的其他團隊使用,而又不能發(fā)布到公司外部的Repository中,那么專屬Repository是正中下懷的選擇。

    • 發(fā)布一些購買的第三方軟件產(chǎn)品以供公司所有人使用,比如Oracle的數(shù)據(jù)庫Driver。

    ?

    存在多種專屬Repository,比如Nexus和Artifactory等,你甚至可以用一個FTP服務(wù)器作為一個專屬Repository。本文將以開源的Nexus為例,演示如何將自己開發(fā)的項目部署到Nexus Repository中。

    ?

    ?

    (一)下載并安裝Nexus

    下載Nexus的war包(本文使用的是nexus-2.5.war),將該war包放在tomcat服務(wù)器的webapps目錄下,重啟tomcat。打開http://localhost:8080/nexus-2.5/,你將看到以下頁面:

    ?

    ?

    ?

    點擊右上角的“Log In”,輸入用戶名admin,密碼admin123(這是Nexus默認的),此后點擊右側(cè)的“Repositories”,顯示當(dāng)前Nexus所管理的Repository,默認情況下Nexus為我們創(chuàng)建了以下主要的Repository:

    • Public Repositories,這是一個Repository Group,它所對應(yīng)的URL為http://localhost:8080/nexus-2.5/content/groups/public/,該Repository ?Group包含了多個Repository,其中包含了Releases、Snapshots、Third Party和Central。Repository Group的作用是我們只需要在自己的項目中配置該Repository Group就行了,它將自動從其所包含的Repository中下載依賴,比如如果我們聲明對Spring的依賴,那么根據(jù)Repository Group中各個Repository的順序(可以配置),Nexus將首先從Releases中下載Spring,發(fā)現(xiàn)沒有,再從Snapshots中下載(極大可能也沒有,因為它是個Snapshots的Repository),依次查找,最后可能在Central Repository中找到。在配置項目的Repository時,我們應(yīng)該首先考慮Public Repositories。

    • 3rd party,該Repository即是存放你公司所購買的第三方軟件庫的地方,它是一個由Nexus自己維護的一個Repository。

    • Apache Snapshots,看名字你就應(yīng)該知道這是個什么樣的Repository,這是一個代理Repository,即最終的依賴還是得在Apache官網(wǎng)上去下載,然后緩存在Nexus中。

    • Central,這就是代理Maven Central Repository的Repository。

    • Releases,你自己的項目要發(fā)布時,就應(yīng)該發(fā)布在這個Repository,他也是Nexus自己維護的Repository,而不是代理。

    • Snapshots,你自己項目Snapshot的Repository。

    ??

    (二)用Nexus Repository取代Maven Central Repository

    在完成(一)之后,我們只是創(chuàng)建了一個專屬的Nexus Repository,我們的項目默認還是使用的Maven Central Repository,所以這時我們需要將Maven Central Repository換成自己創(chuàng)建的Nexus Repository,可以通過修改~/.m2/settings.xml來達到這樣的目的。在該settings.xml文件中(沒有的話可以創(chuàng)建一個),加入以下配置:

    ?

    <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>

    ?

    ?

    以上配置通過mirror和profile的方式將central Repository全部轉(zhuǎn)向到我們自己的Public Repositories,包括release版本和snapshot版本(Maven默認允許從Maven Central Repository下載release版本,這是合理的,如果你使用了別人的snapshot版本,一邊你在使用,一邊別人在開發(fā),別人將API簽名一換,哦豁)。這樣一來,我們項目中的所有依賴都從Nexus的Public Repositories下載,由于其中包含了對Maven Central Repository的代理,所以此時Maven Central Repository中的類庫也是可以間接下載到的。此時你可以將~/.m2/repository/中所有的內(nèi)容刪除掉,再在項目中執(zhí)行“mvn clean install”,你將在終端中看到Maven已經(jīng)開始從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

    ?

    ?

    請注意,此時我們的項目本身不需要做任何修改。我們只是創(chuàng)建了另一個Repository和修改了Maven的默認配置(學(xué)習(xí)完本文后,你應(yīng)該需要將~/.m2/settings.xml還原,不然如果下次在構(gòu)建之前自己的Nexus服務(wù)器沒有啟動,構(gòu)建將失敗。)。

    ?

    ?

    (三)在項目中配置Nexus Repository的信息

    接下來,我們開始著手如何將自己的項目部署到Nexus Repository中。這個也簡單,第一我們需要在項目中指明部署目的Repository的URL,第二我們需要提供用戶名和密碼,哪能讓你胡來。

    ?

    我們知道,對于一個Maven項目而言,如果你的項目版本號中有“SNAPSHOT”字樣,則表示此時的項目是snapshot版本,即處于開發(fā)中。否則,Maven則認為這是一個release版本。所以我們在部署時,需要分別配置這兩種發(fā)布版本所對應(yīng)的Repository。在項目的pom.xml文件中配置需要發(fā)布的目標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版本所對應(yīng)的Repository,如果你項目的版本中包含了“SNAPSHOT”,此時將發(fā)布到Nexus的Snapshots Repository,否則發(fā)布在Releases Repository。

    ?

    至于提供用戶名和密碼,這些信息當(dāng)然不能放在項目中,一是不安全,另外不同的人可能有不同的用戶名和密碼,你不至于在每次部署時都修改一次吧。所以,Maven將這些信息的存放地點放在了~/.m2/settings.xml文件中,每臺機器(基本上就是每個人啦)都可以有不同的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默認的,另外特別需要注意的是,這里的<id>需要和上面項目pom.xml文件中配置Repostory的<id>對應(yīng)起來。

    ?

    (四)發(fā)布到Nexus Repository

    萬事具備,只欠東風(fēng),在項目中執(zhí)行:

    ?

    mvn deploy?

    ?

    ?

    部署成功,再在Nexus中查看部署情況:

    ?

    此時我們部署的是項目的snapshot版本,上圖中的“me”目錄中既包含了作者所部署的項目。

    在下一篇中,我們將講到如何使用Profile。

    總結(jié)

    以上是生活随笔為你收集整理的Maven提高篇系列之(三)——使用自己的Repository(Nexus)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。