使用nexus搭建maven倉庫(本地私服)
原文地址:http://www.cnblogs.com/demingblog/p/3840174.html
我們在使用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之後使用情形如下圖