|做者:RexFangjava
|出處:http://www.cnblogs.com/rexfang/程序員
|關於做者:Java 程序員一枚web
|版權:本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。若有問題,能夠郵件:fangruitao.work@foxmail.comapache
Maven 是平常開發過程當中,都會接觸到的項目管理工具。Maven 能夠將開發人員從繁雜的文檔管理、項目打包等工做中解放出來,把更多的精力放在業務開發上。Maven 帶來的便利這裏就很少說了,可是隻是單單使用 Maven,有時候也會遇到依賴文件沒法下載的狀況,緣由有不少,有多是中央倉庫的問題,也有多是網絡問題等等;與此同時,有一部分的第三方的類庫是沒有發佈到 Maven 上的,所以並無 Maven 相關的依賴信息,這就給咱們使用 Maven 構建項目帶來了必定的麻煩。例如如下的這個例子,就是常常會遇到的問題。瀏覽器
在最近的一個測試項目中,因爲我的比較喜歡有條不紊,因此一直是使用 Maven 做爲項目構建的工具,可是在構建 Hibernate 相關依賴的過程當中,發現有一部分相關的 Jar 沒法正常下載,中間也換過各類軟件倉庫,可是最後都是沒法正常構建項目。網絡
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-distribution</artifactId> <version>3.6.0.Beta2</version> <type>pom</type> </dependency>
最後,實在沒轍了,想起了要搭建一個私有倉庫。本文主要介紹 Nexus 私有倉庫的搭建過程,以及 Nexus 與 Maven 的整合。本文使用的 Nexus 版本爲 Nexus Repository Manager OSS 2.14.5-02 ,之因此不使用最新的 Nexus Repository Manager OSS 3 是由於我的感受版本3尚未版本2友好,特別是在圖形管理界面的操做上。maven
首先須要到官網上下載 Nexus Repository Manager OSS 2.14.5-02 的安裝包,下載地址爲:https://www.sonatype.com/download-oss-sonatype,解壓後的目錄結構:編輯器
在命令行窗口,進入 \nexus-2.14.5-02-bundle\nexus-2.14.5-02\bin 目錄,輸入 nexus install 命令,安裝 Nexus(須要等待一小段時間)。安裝成功後,進入服務管理界面,找到一個名爲 nexus 的服務,啓動該服務,服務啓動完成後,打開瀏覽器,輸入 http://localhost:8081/nexus ,若是能夠看到歡迎界面,說明 Nexus 安裝成功了。工具
點擊右上角的登陸按鈕,輸入管理員帳號和密碼(默認帳號:admin,密碼:admin123)登陸。測試
登陸成功後,選擇左邊 Views/Repositories 菜單下的 Repositories,能夠看到一些預設的倉庫,咱們會用到的通常只有 Public Repositories 和 3rd party , Public Repositories 爲公共倉庫,3rd party 爲第三方倉庫,能夠上傳第三方的 Jar (固然也能夠是本身封裝的 Jar)。
Nexus 安裝成功後,接下來須要修改 Maven 的配置文件(settings.xml),整合 Nexus。
找到 <servers> 標籤,添加 Nexus 默認認證信息:
<server> <id>my-nexus-releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>my-nexus-snapshot</id> <username>admin</username> <password>admin123</password> </server>
找到 <mirrors> 標籤,添加鏡像:
<mirror> <!--This sends everything else to /public --> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror>
找到 <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>
激活倉庫:
<activeProfiles> <!--make the profile active all the time --> <activeProfile>nexus</activeProfile> </activeProfiles>
配置完成後保存,並重啓 nexus 服務。
重啓 nexus 服務 完成後,在命令行窗口進入一個使用 Maven 構建的項目,輸入 mvn package clean 命令,清理完成後,登陸 nexus 的 admin 帳號能夠看到 Public Repositories 下出現了一些 Jar 包。
此時刷新以前報錯的項目,發現 Maven 依賴仍然有問題,沒法正常下載 jboss 的一個 Jar 包。這個時候咱們能夠利用咱們剛剛搭建的 Nexus 私有倉庫,在網上本身下載缺失的 Jar 包,而後以第三方 Jar 的形式上傳到私有倉庫,依賴信息能夠設置跟中央倉庫同樣。例如添加 jbosscache-core-3.2.1.GA.jar ,在外網上的依賴爲:
<dependency> <groupId>org.jboss.cache</groupId> <artifactId>jbosscache-core</artifactId> <version>3.2.1.GA</version> </dependency>
選擇 3rd party,把咱們本身下載的 jbosscache-core-3.2.1.GA.jar 上傳到 3rd party。在 Artifact Upload 選擇卡頁面,點擊 Select Artifact(s) to Upload 按鈕,選擇須要上傳的 Jar 包,選擇完成後點擊 Add Artifact 按鈕。而後設置 Jar 的 Maven 依賴信息(後續引用 Jar 包須要用到),其中 Maven 依賴的相關信息能夠設置成和中央倉庫一致,以下圖:
Jar 包上傳成功後,切換到 Browse Index 選項卡頁面,點擊刷新按鈕,能夠看到咱們剛剛上傳的 Jar 包,選中 Jar 包咱們能夠在右邊看到 Jar 包對應的 Maven 依賴信息,能夠用於項目的構建。
進入本地倉庫刪除 \.m2\org\jboss\cache\jbosscache-core\3.2.1.GA 目錄下的全部文件(重要,不然會構建失敗),此時,再次回到剛剛構建失敗的項目,刷新 Maven,能夠看到項目已經能夠正常構建了。
若是本地安裝了 Maven,能夠在 Maven 的安裝目錄找到一些經常使用的軟件倉庫,位置爲:${MAVEN_HOME}\lib\maven-model-builder-3.3.3.jar 下 \org\apache\maven\model\pom-4.0.0.xml ,其中 ${MAVEN_HOME} 爲 Maven 的安裝目錄,用編輯器打開 pom-4.0.0.xml 能夠看到不少預設的軟件倉庫。
此處,也提供一些經常使用的軟件倉庫:
http://maven.aliyun.com/nexus/content/groups/public(阿里雲,推薦)
這裏,也提供一些 Maven 的參考資料: