1. 什么是Nexus?
Nexus 是一個強(qiáng)大的倉庫管理器,極大地簡化了內(nèi)部倉庫的維護(hù)和外部倉庫的訪問。
2016 年 4 月 6 日 Nexus 3.0 版本發(fā)布,相較 2.x 版本有了很大的改變:
- 對低層代碼進(jìn)行了大規(guī)模重構(gòu),提升性能,增加可擴(kuò)展性以及改善用戶體驗。
- 升級界面,極大的簡化了用戶界面的操作和管理。
- 提供新的安裝包,讓部署更加簡單。
- 增加對 Docker, NeGet, npm, Bower 的支持。
- 提供新的管理接口,以及增強(qiáng)對自動任務(wù)的管理。
2. 基于 Docker 安裝 Nexus
我們使用 Docker 來安裝和運行 Nexus,docker-compose.yml配置如下:
version: '3.1'
services:
nexus:
restart: always
image: sonatype/nexus3
container_name: nexus
ports:
- 8081:8081
volumes:
- /usr/local/docker/nexus/data:/nexus-data
注: 啟動時如果出現(xiàn)權(quán)限問題,可以先執(zhí)行:chmod 777 /usr/local/docker/nexus/data 賦予數(shù)據(jù)卷目錄可讀可寫的權(quán)限,然后再執(zhí)行docker-compose up的命令
如果你使用的是阿里云云服務(wù)器,啟動時控制臺沒有報錯,但是瀏覽器訪問不了,就要看看防火墻有沒有配置規(guī)則
登錄驗證是否安裝成功:
地址:http://ip:port/ 用戶名:admin 密碼:密碼在 /usr/local/docker/nexus/data/目錄下的admin.password文件中,直接復(fù)制登錄即可

3.倉庫介紹
3.1 代理倉庫(Proxy Repository)
意為第三方倉庫,如:maven-central nuget.org-proxy,用來和中央倉庫同步,比如我們需要一個jar包,我們自己的倉庫中沒有,此時就通過代理倉庫來和中央倉庫同步,先將這個jar包同步到代理倉庫,然后再從代理倉庫下載到本地,這樣就可以依賴了
- 版本策略(Version Policy):
Release: 正式版本
Snapshot: 快照版本
Mixed: 混合模式 - 布局策略(Layout Policy):
Strict:嚴(yán)格
Permissive:寬松
3.2 宿主倉庫(Hosted Repository)
存儲本地上傳的組件和資源的,如:
maven-releases
maven-snapshots
nuget-hosted部署策略(Deployment Policy):
Allow Redeploy:允許重新部署
Disable Redeploy:禁止重新部署
Read-Only:只讀
3.3 倉庫組(Repository Group)
通常包含了多個代理倉庫和宿主倉庫,在項目中只要引入倉庫組就可以下載到代理倉庫和宿主倉庫中的包,如:
maven-public
nuget-group
4. 在項目中使用Maven私服
step 1 配置認(rèn)證信息
在 Maven settings.xml中添加 Nexus 認(rèn)證信息(servers 節(jié)點下):
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
作用:指定了id、登錄私服的用戶名和密碼,其中私服的地址在下面配置
step 2 配置自動化部署
在自己項目中的pom.xml中配置如下信息
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://127.0.0.1:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://127.0.0.1:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:ID必須要和server中配置的一致
作用:主要是存放我們自己項目打包后上傳的包,是真正達(dá)到私有化目的的配置
step 3 配置代理倉庫
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus Plugin Repository</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
作用:用于和中央倉庫進(jìn)行同步。例如:我們需要一個common-logging.jar的包,就會去代理服務(wù)器目錄下去找,如果代理倉庫沒有,代理倉庫會自己去中央倉庫去同步下載到代理倉庫,再供項目下載使用。