原文:https://yq.aliyun.com/articles/5981
第8章 私服nexus
本章詳細介紹了nexus的安裝過程,設置maven從私服下載構件,以及發布構件至nexus。
8.1 什么是nexus
nexus是一個web版的倉庫管理軟件
8.2 nexus安裝
nexus安裝非常簡單。
下載地址:http://www.sonatype.org/nexus/go
本文以Linux環境為例,安裝nexus:
1. 首先在/usr/local下建nexus目錄
2. 將下載的zip包解壓,將其中的nexus-2.11.4-01解壓至nexus如圖:
- 進入bin目錄,將nexus設置為可執行權限:
cd bin
chmod +x nexus
-
設置root可執行nexus
默認的nexus是不讓root用戶直接啟動的,會報出如下錯誤`WARNING - NOT RECOMMENDED TO RUN AS ROOT` `If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.`
需要設置root執行權限
export set RUN_AS_USER=root
此語句僅臨時設置root變量,重啟linux后會無效,如需持久設置,需要修改環境變量,見以下步驟。
- 此時執行
./nexus start
即可啟動nexus。
- 停止nexus
./nexus stop
-
配置環境變量,設置開機啟動
- 設置環境變量:
vim /etc/profile
在最后一行添加export set RUN_AS_USER=root
- 設置開機啟動
進入到配置目錄:
cd /etc/init.d
復制腳本cp /usr/local/nexus/bin/nexus nexus
添加系統服務:chkconfig --add nexus
設置啟動級別為345:chkconfig --levels 345 nexus on
查看是否添加成功:chkconfig --list|grep nexus
,如圖
- 修改腳本
vim /etc/init.d/nexus
第一行添加:
RUN_AS_USER="root"
然后,將NEXUS_HOME=".."
修改為NEXUS_HOME="/usr/local/nexus"
如圖:
- 重啟Linux
reboot
- 訪問地址:
http://<LinuxIP>:8081/nexus/#welcome
注意:重啟Linux需要一段時間,不斷刷新此地址即可。
查看nexus是否啟動,`ps –ef|grep nexus`。

8.3 nexus倉庫設置
8.3.1 登錄系統,修改密碼
nexus默認的管理員密碼是admin/admin123,為了安全起見,第一件事就是上來修改管理員密碼。
8.3.2 nexus內置倉庫介紹
1、 倉庫類型介紹
倉庫類型 | 介紹 |
---|---|
group | 倉庫組 |
hosted | 宿主倉庫 |
proxy | 代理倉庫 |
virtual | 虛擬倉庫 |
2、 倉庫介紹
倉庫 | 介紹 |
---|---|
3rd party | 這是一個策略為Releases的宿主倉庫,用來部署無法從公共倉庫獲得的第三方發布版本構件 |
Apache Snapshots | 這是一個策略為Snapshots的代理倉庫,用來代理ApacheMaven倉庫的快照版本構件 |
Central | 這是一個策略為Releases的代理倉庫,用來代理maven中央倉庫中發布的版本構件 |
Central M1 shadow | 這是一個策略為Releases的虛擬倉庫,用來提供中央倉庫中M1格式的發布版本的構件 |
Codehaus Snapshots | 這是一個策略為Snapshots的代理倉庫,用來代理Codehaus Snapshots倉庫的快照版本構件的倉庫 |
Releases | 這是一個策略為Releases的宿主倉庫,用來部署管理內部的發布版本構件 |
Snapshots | 這是一個策略為Snapshots的宿主倉庫,用來部署管理內部的快照版本構件 |
8.3.3 索引
-
開啟遠程索引
一個新搭建的nexus,是一個空的倉庫,nexus默認是關閉遠程索引下載,打開步驟如圖:注意:3個proxy類型的遠程索引都需要打開
然后在3個proxy類型的倉庫上,點擊右鍵,Repair Index,nexus就會去下載遠程的索引文件。
測試
在Browse Remote 中搜索任意jar
8.3.4 創建宿主倉庫
8.3.5 倉庫組設置
- 什么是倉庫組?
倉庫組是將多個倉庫聚合成一個倉庫地址,為maven提供服務。maven配置中,只需要配置倉庫組的鏈接,即可同時得到多個倉庫的構件。
倉庫組中倉庫的順序決定了倉庫組遍歷其所含倉庫的次序,所以應該將常用的倉庫放在前面。
倉庫組設置很簡單:
點擊type為group類型的倉庫,configuration標簽頁中Ordered Group Repositories為當前組包含倉庫,available repositories為可繼續添加的倉庫。
8.4 配置maven從私服下載構件
前面安裝了nexus,目的就是讓開發人員的maven鏈接私服下載構件。
修改maven配置文件setting.xml
找到profiles標簽,設置代碼:
<profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus</id> <name>crop-nexus</name> <url>http://你的NexusIP:8081/nexus/content/groups/public/</url> <releases> <!-- true表示開啟倉庫發布版本下載,false表示禁止 --> <enabled>true</enabled> </releases> <snapshots> <!-- true表示開啟倉庫快照版本下載,false表示禁止 --> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <url> http://你的NexusIP:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <!-- 禁止快照版本,防止不穩定的插件影響項目構建 --> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活nexus私服 --> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
這樣配置,maven就可以從私服下載構件了,但是會有個問題,他還會時不時的請求中央倉庫,如果想完全讓maven訪問nexus私服,需要設置鏡像mirrors來支持。
8.5 maven的鏡像配置
maven配置鏡像后,所有的構件都只會從鏡像服務器下載。
<mirrors> <mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <name>crop-nexus</name> <url>http://你的nexusIP:8081/nexus/content/groups/public/</url> </mirror> </mirrors>
8.6 發布構件至nexus
8.6.1 配置distributionManagement
發布倉庫一般分為Releases版和snapshot版,所以要配置2個倉庫地址
注意:這個是在項目的pom.xml中配置的
<!-- 發布構件至私服nexus --> <distributionManagement> <repository> <id>nexus-releases</id> <name>corp nexus-releases</name> <url>http://你的nexusIP:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>nexus-snapshot</id> <name>corp nexus-snapshot</name> <url>http://你的nexusIP:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
8.6.2 設置用戶
配置好pom.xml后,需要指定發布用戶,在setting.xml中設置:
<servers> <!-- 發布Releases版的賬號,ID要與distributionManagement中的Releases ID一致 --> <server> <id>nexus-releases</id> <username>admin</username> <password>******</password> </server> <!-- 發布snapshot版的賬號,ID要與distributionManagement中的snapshot ID一致 --> <server> <id>nexus-snapshot</id> <username>admin</username> <password>******</password> </server> </servers>
設置完成,使用命令發布至nexus
mvn deploy
在nexus中查看