使用nexus搭建maven倉庫(maven 本地私服)

 

  咱們在使用maven的時候,對於項目所依賴的jar包,maven默認會在中央倉庫下載jar包,到本地的磁盤目錄(若是沒有配置則是用戶目錄下/.m2/repository文件夾下)。若是公司內部搭了一個maven私服的話,開發人員將倉庫地址指向內網倉庫地址,須要的jar包可直接在私服取,下載速度比遠程要快,若是本地倉庫沒有則自動從遠程下載並保存在本地。本文使用nexus搭建一個私服。web

1.下載 nexus

      在瀏覽器中搜索 Sonatype Nexus 進入官網,找到下載 。下載以後解壓壓縮包以下:apache

 

2.安裝 nexus

進入nexus/bin/jws文件夾下看到以下圖:windows

個人電腦是win7 32位,因此點開windows-x86-32 文件夾,雙擊install-nexus.bat 把nexus安裝成一個服務,完成以後,在windows服務裏邊將看到以下圖所示:瀏覽器

這時候在瀏覽器輸入http://localhost:8081/nexus  將看到以下頁面,說明安裝成功maven

 

3.配置 nexus

 在第二步完成以後,點擊nexus主要右上角的 Log In按鈕,管理員初始密碼爲 admin/adming123  登錄進去之點擊 左側的 【Repositories】菜單,會看的以下幾個倉庫測試

右鍵單擊 Apache Snapshots ,和central 倉庫,在右鍵菜單中點擊 Update Index 更新jar索引this

默認狀況下,nexus下載的索引,和jar包會保存在 nexus 同級目錄下sonatype-work文件夾下url

例如個人nexus安裝目錄爲:D:\Devolopment\nexus-2.7   則 下載的jar和索引會保存在D:\Devolopment\sonatype-work 下,點開snoatype-work -->nexus 會看到 indexer、storage文件夾,可點進去看看spa

一些簡單配置:3d

點擊Apache Snapshots倉庫,進行以下配置,開啓遠程索引下載,點擊save按鈕

將現有的倉庫,Apache snapshots ,3dpart,central 等倉庫 加入 public 組中,直接在在界面中 將右側的倉庫移左邊,效果以下:

 

4.在 maven 中使用 nexus

 找到maven的配置文件,即:apache-maven-3.1.1/conf 下的settings.xml

在mirrors節點下加入以下配置

 

  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
      
    <mirror>
      <id>nexus</id>
      <mirrorOf>central</mirrorOf>
      <name>internal nexus repository</name>
      <url>http://localhost:8081/nexus/content/groups/public/</url>這一段配置,描述的即是 上一步中提到的那個Public Reposiories的信息,這個地址即是他的地址,這些寫均可以再界面上看到 
    </mirror>這裏的是localhost是在本機搭建測試用的,若是是公司內部的倉庫的話,可自行修改爲公司內網ip地址。
  </mirrors>

 

 

 

如此,配置以後,在咱們使用maven的時候,就會經過私服了。而不是直接從遠程取了。(沒有配置以前,maven默認在遠程的中央倉庫下載)

 

在這個配置文件中,有個節點 叫localRepository 是用來配置 maven下載的jar包存放的地址的,若是不配置的話,jar包將會下載到c盤用戶文件夾下.m2 文件夾下。此處可指定目錄,以下

則maven下載的jar包會保存在 D:/Devolopment/MavenRepository 下

 總結,你們nexus以後使用情形以下圖

 

5.如何發佈項目到maven私服

咱們搭建好私服以後,除了能夠快速的下載 jar 包,更重要的時候,咱們能夠發佈本身的項目/模塊到私服中,而後能夠方便你們共用。

好比,咱們的項目是一個商場項目,項目模塊是這樣的:

 

shoo-parent pom
shop-user jar
shop-search jar
shop-product jar
shop-web war

 

如圖所示,我簡單了列出了一個大項目可能具備的模塊,實際項目模塊更多。一般狀況下,每一個人負責的項目不一樣。比方說張三負責開發 shop-user,李四負責 shop-web, shop-web須要引用 shop-user 。若是張三開發完了,如何把 jar 提供給李四用呢?發到私服就行了。李四開發的 shop-web,在pom 文件中引入以下依賴:

<dependency>
            <groupId>org.myshop</groupId>
            <artifactId>shop-user</artifactId>
            <version>0.0.1</version>
</dependency>

張三將 shop-user 發到私服就好了,發佈須要執行   mvn deploy 命令。

在執行此命令以前,須要作一些配置。

5.1 修改 pom文件

比方說,咱們須要發佈shop-user到私服。咱們須要在shop-user的 pom 文件中增長以下配置:

    <distributionManagement>
        <snapshotRepository>
            <id>my-shop-snapshots</id> 這個 ID 很重要,後面要用到
            <name>snapshot repository</name>
            <url>http://192.168.xx.xx:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>my-shop-releases</id>
            <name>releases repository</name>
            <url>http://192.168.xx.xx:8081/nexus/content/repositories/releases/</url>
        </repository>
    </distributionManagement>      

 

配置中的 id 節點配置的 my-shop-snapshots 是自定義的,這個 ID 後面會用到。 url 節點對應的是你的私服鏈接。 

 5.2 修改 maven 的 setting.xml文件

繼續修改 maven 的配置文件,打開apache-maven-3.1.1/conf 下的settings.xml ,在<servers> 節點中間加入以下配置:

   <server>  
    <id>my-shop-releases</id>  
    <username>admin</username>  
    <password>admin123</password>  
   </server>  
   <server>  
    <id>my-shop-snapshots</id>  
    <username>admin</username>  
    <password>admin123</password>  
   </server>   

這裏的 id 對應的就是咱們上面5.1 中配置的 ID。username ,password對於的是咱們 nexus 私服的登陸密碼。若是這個用戶名或密碼配置錯誤的話就會報錯,mvn deploy 不成功。

配置完了以後,在 shop-user項目的目錄下執行 :   mvn delpoy -Dmaven.test.skip=true,能夠看到相似以下日誌:

Uploading to my-shop-snapshots: http://192.168.xx.xx:8081/nexus/content/repositories/snapshots/com/myshop/shop-user/0.0.5-SNAPSHOT/maven-metadata.xml (483 B at 15 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.011 s
[INFO] Finished at: 
[INFO] Final Memory: 31M/411M
[INFO] --------------------------------------------------------------------

從日誌Uploading to my-shop-snapshots: http://192.168.xx.xx:8081/ 就看到,咱們的 jar 上傳的地址是咱們本身的私服地址了。

這時去私服窗口檢索一下就能夠看到剛 deploy 的 jar 文件了。

相關文章
相關標籤/搜索