首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Nexus 3.x 搭建 Maven 私服完整指南:下載、啟動、上傳第三方 JAR、POM 配置與最佳實踐

     編輯:Java 綜合教學

版本選擇與環境準備

  • Nexus 2.x:需 JDK 7/8,輕量,適合舊環境(本文演示版)。
  • Nexus 3.x:需 JDK 8+,功能豐富(Docker、npm、PyPI 等),推薦新項目。
  • 本機 Linux:CentOS 7,JDK 1.8.0_202,防火牆開放 8081 端口。

下載與安裝 (Nexus 2.14.3 為例)

# 下載
wget https://download.sonatype.com/nexus/oss/nexus-2.14.3-02-bundle.tar.gz
# 解壓
tar -zxvf nexus-2.14.3-02-bundle.tar.gz -C /opt/
# 目錄結構
/opt/nexus-2.14.3-02/          # 程序目錄
/opt/sonatype-work/nexus/      # 數據目錄 (配置、存儲、日志)

啟動與訪問

cd /opt/nexus-2.14.3-02/bin
# 非 root 啟動 (推薦創建 nexus 用戶)
./nexus start
# 輸出: Starting Nexus OSS... Started Nexus OSS.
# 訪問: https://server-ip:8081/nexus/
# 默認賬號: admin / admin123

注意:root 啟動會報警告,需 export RUN_AS_USER=root 或切換普通用戶。

上傳第三方 JAR (以 Oracle JDBC 驅動為例)

  1. 下載 ojdbc8.jar (Oracle 官網需登錄)。
  2. 登錄 Nexus Web UI → Repositories → 3rd party → Artifact Upload。
  3. 填寫 GAV 坐標:
    • Group ID: com.oracle
    • Artifact ID: ojdbc8
    • Version: 19.3.0.0
    • Packaging: jar
  4. 選擇文件上傳 → Upload Artifact。

Maven 客戶端配置

settings.xml 全局鏡像 (推薦)

<mirrors>
  <mirror>
    <id>nexus</id>
    <mirrorOf>central</mirrorOf>
    <name>Nexus Public Mirror</name>
    <url>https://nexus-server:8081/nexus/content/groups/public/</url>
  </mirror>
</mirrors>

<profiles>
  <profile>
    <id>nexus</id>
    <repositories>
      <repository>
        <id>nexus</id>
        <url>https://nexus-server:8081/nexus/content/groups/public/</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>nexus</id>
        <url>https://nexus-server:8081/nexus/content/groups/public/</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
      </pluginRepository>
    </pluginRepositories>
  </profile>
</profiles>
<activeProfiles><activeProfile>nexus</activeProfile></activeProfiles>

pom.xml 依賴引用

<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc8</artifactId>
  <version>19.3.0.0</version>
</dependency>

常見倉庫類型說明

類型用途示例
proxy代理遠程倉庫 (中央庫、阿里云)central, aliyun
hosted本地存儲 (內部發布、第三方)releases, snapshots, 3rd party
group聚合多個倉庫統一對外public = central + releases + 3rd party

最佳實踐清單

  1. 備份策略:定期打包 /opt/sonatype-work/nexus/storage 異地存儲。
  2. 權限控制:創建角色 deployer 僅有 hosted releases/snapshots 寫權限,開發者僅讀。
  3. 清理策略:配置 Remove Snapshots 任務,保留最近 30 天/最多 100 個快照版本。
  4. 監控告警:磁盤空間、JVM 內存、請求延遲接入 Prometheus/Grafana。
  5. HTTPS + 認證:生產環境必須配置 Nginx 反向代理 SSL + Basic Auth。

遷移到 Nexus 3.x 提示

  • 安裝包 nexus-3.xx.x-xx-unix.tar.gz,需 JDK 11+ (3.40+)。
  • 數據遷移使用官方 nexus-repository-import-export 插件或手動複製 storage 目錄。
  • UI/REST API 變化大,腳本需重寫。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved