一、概述
私服的好處
- 緩存Maven中央倉庫的jar包,這樣避免每次本地倉庫沒有jar包直接去中央倉庫下載,而是先到私服下載。
- 解決公司無法上網,而又要連接中央倉庫的問題。只要連接私服,確保私服可以連接到中央倉庫。
- 方便公司內部不同團隊或者項目共享jar包。需要共享jar包的,可以上傳到私服,通過私服共享。
二、Nexus安裝與倉庫概念
Nexus下載地址:https://www.sonatype.com/nexus-repository-oss
1. 安裝
- 解壓文件
- 啓動服務 nexus.exe /run (啓動成功後不要關閉命令行窗口)
- 安裝服務 nexus.exe /install (重新使用新得命令行安裝,可能出現權限不足,請以管理員身份運行命令行)
- 啓動服務 net start nexus
- 停止服務 net stop nexus
- 訪問頁面 http://localhost:8081
- 初始登錄用戶名和密碼:admin/admin123
注:啓動nexus.exe不能使用傳統得鼠標雙擊來運行啓動,而是要通過如下的命令行鍵入命令啓動
2. 查看修改Nexus端口
- nexus-3.14.0/etc/nexus-default.properties ——application-port=8081
3. 倉庫
- 從Nexus私服下載資源(找到maven配置文件settings.xml,添加如下配置)
// 添加鏡像配置,將所有訪問外網倉庫的請求指向私服
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
// 添加profiles,所有請求均通過鏡像
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
// 生效配置
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
- 上傳資源到Nexus私服(添加路徑同上)
<distributionManagement>
<repository>
<id>release></id>
<name>Internal Release</name>
<url>http://192.168.11.203:8081/nexus/content/repositories/release</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.11.203:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
在maven的配置文件中配置上傳權限
<server>
<id>release</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
通過mvn deploy命令將工程打包成jar發佈到nexus私服中