Maven 上傳jar到nexus私庫

一、上傳配置

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-releasesmaven-snapshots 是 Nexus 默認(rèn)倉庫名,需與您的實(shí)際倉庫名一致(可在 Nexus 管理界面確認(rèn))。


2?? Nexus 倉庫未開啟寫權(quán)限

確保倉庫支持部署(Deploy)操作:

  1. 登錄 Nexus → Repository → 選擇倉庫(如 maven-releases

  2. 檢查配置:

    • Type: hosted(托管倉庫)
    • Deployment Policy: Allow redeploy(允許覆蓋)或 Disable redeploy(僅首次)
  3. SecurityRoles 中確認(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 倉庫類型詳解》

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容