回顧下 Maven 的構建流程,如果沒有私服,我們所需的所有 jar 包都需要通過 Maven 的中央倉庫或者第三方的 Maven 倉庫下載到本地,當一個公司或者一個團隊所有人都重復的從 Maven 倉庫下載 jar 包,這樣就加大了中央倉庫的負載和浪費了外網的帶寬,如果網速慢的話還會影響項目的進程。
私服是在局域網的一種特殊的遠程倉庫,目的是代理遠程倉庫及部署第三方構件。有了私服之后,當 Maven 需要下載 jar 包時,先請求私服,私服上如果存在則下載到本地倉庫。否則,私服直接請求外部的遠程倉庫,將 jar 包下載到私服,再提供給本地倉庫下載。
1. nexus 安裝
我們可以使用專門的 Maven 倉庫管理軟件來搭建私服,這里我們使用 Nexus。下載地址:https://help.sonatype.com/repomanager2/download。
Nexus 專業版是需要付費的,這里我們下載開源版 Nexus OSS,最新的是 OSS3.x,我們選擇穩定的版本 2.x。
下載完成后,我們將壓縮包解壓到F盤,形成以下目錄
接下來我們啟動 nexus:
1)以管理員身份打開 cmd,進入到 bin 目錄,先執行 nexus install 命令
再執行 nexus start 命令
2)打開瀏覽器,訪問 http://localhost:8081/nexus
3)點擊右上角Log in,使用用戶名:admin,密碼:admin123登錄
2. nexus 服務器倉庫類型
1)hosted:是本地倉庫,用戶可以把自己的一些jar包,發布到hosted中,比如公司的第二方庫
2)proxy:代理倉庫,它們被用來代理遠程的公共倉庫,如maven中央倉庫。不允許用戶自己上傳jar包,只能從中央倉庫下載
3)group:倉庫組,用來合並多個hosted/proxy倉庫,當你的項目希望在多個repository使用資源時就不需要多次引用了,只需要引用一個group即可
4)virtual:虛擬倉庫基本廢棄了。
3. nexus 預置倉庫
1)Central:該倉庫代理 Maven 中央倉庫,其策略為 Release,因此只會下載和緩存中央倉庫中的發布版本構件。
2)Releases:這是一個策略為 Release 的宿主類型倉庫,用來部署正式發布版本構件
3)Snapshots:這是一個策略為 Snapshot 的宿主類型倉庫,用來部署開發版本構件。
4)3rd party:這是一個策略為 Release 的宿主類型倉庫,用來部署無法從 Maven 中央倉庫獲得的第三方發布版本構件,比如 IBM 或者 oracle 的一些jar包(比如 classe12.jar),由於受到商業版權的限制,不允許在中央倉庫出現,如果想讓這些包在私服上進行管理,就需要第三方的倉庫。
5)Public Repositories:一個組合倉庫
4. 在 nexus 建立倉庫
1)建庫,Add -->Hosted Repository
2)填寫倉庫信息
(1)Respository ID:倉庫編號
(2)Repository NAME:倉庫名稱
(3)Repository Type:倉庫類型
(4)Repository Policy:倉庫策略
(5)Default Local Storage Location:倉庫路徑(不填的話默認 nexus 安裝的路徑)
(6)Deployment Policy:發布策略
3)然后選擇 Public Repositories,打開 configuration 選項卡,將自己創建的倉庫添加到 group
5. 配置遠程倉庫的幾種方法
1)在 pom 文件里配置多個遠程倉庫
<repositories> <repository> <id>nexus</id> <name>nexus私服URL</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </repository> <repository> <id>aliyun</id> <name>阿里雲</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories>
缺點:每個項目都需要配置,固可以在 settings.xml 配置全局
2)在 settings.xml 里配置全局
<profiles> <profile> <id>my</id> <repositories> <repository> <id>aliyun</id> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> <repository> <id>nexus</id> <name>nexus私服URL</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>my</activeProfile> </activeProfiles>
6.配置鏡像
在 settings.xml 里配置阿里雲鏡像加速默認的遠程倉庫
<mirrors> <mirror> <id>aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>
全局默認的遠程倉庫:
<repository> <id>central</id> <url>https://repo1.maven.org/maven2/</url> </repository>
由於配置 <mirrorOf>central</mirrorOf>,所有從 center(默認遠程倉庫)去拉取依賴都將重定向從 aliyun 鏡像中去拉取依賴
注:mirrorOf 匹配的都是 repository 的 id
<mirrorOf>*</mirrorOf> :匹配所有倉庫請求,即將所有的倉庫請求都轉到該鏡像上
<mirrorOf>repo1,repo2</mirrorOf> :將倉庫 repo1 和 repo2 的請求轉到該鏡像上,使用逗號分隔多個遠程倉庫
<mirrorOf>*,!repo1</mirrorOf> : 匹配所有倉庫請求,repo1 除外(將繼續從 repo1 請求),使用感嘆號將倉庫從匹配中排除
7.加速遠程倉庫的3種方式
1)如果沒有 nexus 私服,可以使用 aliyun 鏡像來加速默認的官方遠程倉庫
在 settings.xml 里配置:
<mirrors> <mirror> <id>aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>
2) 如果有 nexus 私服,可以同時使用 aliyun 鏡像和私服遠程倉庫,aliyun 鏡像用於加速官方倉庫,私服用於加載第三方 jar 包和自己 deploy 的 jar 包
在 setting.xml 里配置:
<mirrors> <mirror> <id>aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>
<profiles> <profile> <id>my</id> <repositories> <repository> <id>nexus</id> <name>nexus私服URL</name> <url>http://127.0.0.1:8081/repository/maven-public/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> </profile> </profiles>
<activeProfiles> <activeProfile>my</activeProfile> </activeProfiles>
3)如果私服 nexus 里可以聯網,直接新建 repository,設置遠程代理到 aliyun,然后 settings.xml 里配置
<profiles> <profile> <id>my</id> <repositories> <repository> <id>nexus</id> <name>nexus私服URL</name> <url>http://127.0.0.1:8081/repository/maven-public/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>my</activeProfile> </activeProfiles>
8. 如果想將自己的項目打成的 jar 包 deploy 到 nexus 服務器上,需要配置 distributionManagement
<distributionManagement> <repository> <id>jwen</id> <url>http://localhost:8081/nexus/content/repositories/jwen</url> </repository> </distributionManagement>
9. 如何使用 3rd party 上傳第三方 jar 包
在Repository列表中,選中 3rd party,選中 artifact upload,如下圖所示:
選擇 GAV Defini3ion:GAV Parameters,Auto Guess 打鈎。
在下方輸入 JAR 包對應的 Group、Artifact、Version,Packaging 選擇 JAR 格式。
點擊 select Artifact(s) to upload 按鈕,選擇要上傳的 JAR 包
點擊 Add Artifact 按鈕,添加 JAR 包
點擊 Upload Artifact(s) 按鈕,加載上傳 JAR 包