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


 

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

  

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

 

   

 

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

 

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

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

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

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

  • 發布一些購買的第三方軟件產品以供公司所有人使用,比如Oracle的數據庫Driver。

 

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

 

 

(一)下載並安裝Nexus

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

 

 

 

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

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

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

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

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

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

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

  

(二)用Nexus Repository取代Maven Central Repository

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

 

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

 

 

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

 

 

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

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

 

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

 

至於提供用戶名和密碼,這些信息當然不能放在項目中,一是不安全,另外不同的人可能有不同的用戶名和密碼,你不至於在每次部署時都修改一次吧。所以,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>對應起來。

 

(四)發布到Nexus Repository

萬事具備,只欠東風,在項目中執行:

 

mvn deploy 

 

 

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

 

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM