在最近的一個測試項目中,由於個人比較喜歡有條不紊,所以一直是使用 Maven 作爲項目構建的工具,但是在構建 Hibernate 相關依賴的過程中,發現有一部分相關的 Jar 無法正常下載,中間也換過各種軟件倉庫,但是最後都是無法正常構建項目。
1 2 3 4 5 6 | <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友好,特別是在圖形管理界面的操作上。
首先需要到官網上下載 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 默認認證信息:
1 2 3 4 5 6 7 8 9 10 | <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> 標籤,添加鏡像:
1 2 3 4 5 6 | <mirror> <!--This sends everything else to / public --> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http: //localhost:8081/nexus/content/groups/public/</url> </mirror> |
找到 <profiles> 標籤,添加倉庫信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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> |
激活倉庫:
1 2 3 4 | <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 ,在外網上的依賴爲:
1 2 3 4 5 | <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 的參考資料: