一、上傳配置
1. 配置Maven的settings.xml
在~/.m2/settings.xml的<servers>標(biāo)簽中添加Nexus倉庫認(rèn)證信息:
<server>
<id>nexus-repo</id> <!-- 此ID需與pom中的repository ID一致 -->
<username>your_username</username>
<password>your_password</password>
</server>
2. 在項(xiàng)目的pom.xml中配置倉庫地址
<project >
<distributionManagement>
<repository>
<id>nexus-repo</id> <!-- 與settings.xml中的server.id一致 -->
<name>Nexus Release Repository</name>
<url>http://your-nexus-domain/repository/maven-releases/</url>
</repository>
</distributionManagement>
</project>
3. 執(zhí)行Maven部署命令
mvn clean deploy
Maven會(huì)自動(dòng)構(gòu)建項(xiàng)目并將JAR上傳到Nexus庫。
4. 下載nexus私庫的包
在項(xiàng)目的pom.xml中添加下載地址, 注意是 /maven-public/
<project >
<repositories>
<repository>
<id>nexus-repo</id>
<url>http://your-nexus-domain/repository/maven-public/</url>
</repository>
</repositories>
</project>
同樣要配置倉庫認(rèn)證信息, 在~/.m2/settings.xml的<servers>標(biāo)簽中添加Nexus倉庫認(rèn)證信息:
<server>
<id>nexus-repo</id> <!-- 此ID需與pom中的repository ID一致 -->
<username>your_username</username>
<password>your_password</password>
</server>
二、常見錯(cuò)誤
1?? 倉庫 URL 錯(cuò)誤
這是最常見的問題:
-
發(fā)布版本(Release)必須上傳到
/releases倉庫 -
快照版本(SNAPSHOT)必須上傳到
/snapshots倉庫
?? 檢查:
<!-- pom.xml 配置示例 -->
<distributionManagement>
<!-- 發(fā)布版本(如 1.0.0)必須用 releases -->
<repository>
<id>nexus-releases</id>
<url>http://nexus.example.com/repository/maven-releases/</url>
</repository>
<!-- 快照版本(如 1.0-SNAPSHOT)必須用 snapshots -->
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://nexus.example.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
?? 重要:
URL 中的 maven-releases 和 maven-snapshots 是 Nexus 默認(rèn)倉庫名,需與您的實(shí)際倉庫名一致(可在 Nexus 管理界面確認(rèn))。
2?? Nexus 倉庫未開啟寫權(quán)限
確保倉庫支持部署(Deploy)操作:
登錄 Nexus → Repository → 選擇倉庫(如
maven-releases)-
檢查配置:
-
Type:
hosted(托管倉庫) -
Deployment Policy:
Allow redeploy(允許覆蓋)或Disable redeploy(僅首次)
-
Type:
在 Security → Roles 中確認(rèn)用戶擁有
nx-repository-view-*-*-*權(quán)限
3?? 版本號(hào)不匹配
-
Release版本(如1.0.0)不能部署到snapshots倉庫 -
SNAPSHOT版本(如1.0-SNAPSHOT)不能部署到releases倉庫
?? 驗(yàn)證項(xiàng)目版本號(hào)是否匹配倉庫類型:
<version>1.0.0</version> → 必須配置 <repository>(releases)
<version>1.0-SNAPSHOT</version> → 必須配置 <snapshotRepository>(snapshots)
4?? 嘗試覆蓋不可覆蓋的版本
若倉庫設(shè)置為 Disable redeploy:
- 已存在的版本(如
1.0.0)會(huì)拒絕覆蓋 → 返回 405 - 解決方案:
a) 升版本號(hào)重新部署
b) 改為Allow redeploy(生產(chǎn)環(huán)境不推薦)
c) 在 Nexus 手動(dòng)刪除舊版本
擴(kuò)展閱讀:《Nexus 倉庫類型詳解》