1. 程式人生 > >Maven之使用nexus搭建maven倉庫(本地私服)

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

對於maven專案所依賴的jar包,maven預設會在中央倉庫下載jar包,到本地的磁碟目錄(如果沒有配置則是使用者目錄下/.m2/repository資料夾下)。如果公司內部搭了一個maven私服的話,開發人員將倉庫地址指向內網倉庫地址,需要的jar包可直接在私服取,下載速度比遠端要快,如果本地倉庫沒有則自動從遠端下載並儲存在本地。本文使用nexus搭建一個私服。

 一、下載nexus

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

   二、安裝nexus

進入nexus/bin/jws資料夾下看到如下圖:

我的電腦是win7 32位,所以點開windows-x86-32 資料夾,雙擊install-nexus.bat 把nexus安裝成一個服務,完成之後,在windows服務裡邊將看到如下圖所示:

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

 三、配置nexus

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

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

預設情況下,nexus下載的索引,和jar包會儲存在 nexus 同級目錄下sonatype-work資料夾下

例如我的nexus安裝目錄為:D:\Devolopment\nexus-2.7   則 下載的jar和索引會儲存在D:\Devolopment\sonatype-work 下,點開snoatype-work -->nexus 會看到 indexer、storage資料夾,可點進去看看

一些簡單配置:

點選Apache Snapshots倉庫,進行如下配置,開啟遠端索引下載,點選save按鈕

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

四、在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之後使用情形如下圖