Sonatype Nexus 实践指南:从安装到 Maven 依赖管理

1. 简介

Sonatype Nexus Repository Manager 是一款强大的仓库管理工具,用于存储、管理和发布软件组件。它能够支持多种格式的仓库,如 Maven、npm、Docker 等。在企业开发中,私有 Maven 仓库常用于存储自定义依赖和发布组件,确保代码安全性和内部共享。

本文将从服务器环境搭建、Nexus 安装与配置、仓库创建、依赖上传,再到 Maven 项目中使用私有仓库的全过程,帮助你掌握如何高效管理 Maven 依赖。


2. 服务器环境准备与 Nexus 安装

2.1 系统要求

  • 操作系统:Linux 或 Windows(推荐使用 Linux,如 CentOS、Ubuntu)
  • Java JDK:Nexus 需要 Java JDK 8 以上版本支持(推荐 JDK 11)。
  • 硬件需求:至少 4GB 内存,50GB 硬盘空间,取决于你要管理的包的数量。

2.2 Nexus 安装步骤

2.2.1 下载 Nexus Repository Manager

前往 Sonatype Nexus 官网 下载最新版的 Nexus Repository Manager:

wget http://download.sonatype.com/nexus/3/latest-unix.tar.gz
2.2.2 安装与配置

解压安装包:

tar -zxvf latest-unix.tar.gz

进入解压目录,启动 Nexus:

cd nexus-<version>

./bin/nexus start

设定 Nexus 为系统服务,以便服务器启动时自动启动:

ln -s /path/to/nexus-<version>/bin/nexus /etc/init.d/nexus

浏览器访问 http://<服务器IP>:8081,默认端口是 8081。初次登录使用默认管理员账户:

  • 用户名:admin
  • 密码:admin123(第一次登录后系统会强制要求更改密码)

3. Nexus 基本配置

3.1 初次登录与管理界面介绍

进入 Nexus 后,会看到以下几个重要的模块(不同版本功能菜单位置可能不同):

  • Repositories(仓库):管理各种格式的仓库,包括 Blob Store(存储)、仓库清理策略配置等功能。
  • Security(安全设置):管理用户、角色和权限。
  • System(系统设置):配置 Nexus 服务器的高级设置,包括定时任务、允许管理员配置和管理系统功能,从而扩展和优化 Nexus 的使用体验等功能。

3.2 创建用户和角色

  1. 在 Nexus 界面,选择 Security > Users,可以添加新用户。
  2. 可以设置不同角色的权限,如管理员、开发者等。
  3. 通过配置不同角色,可以控制哪些用户可以上传或下载依赖,确保仓库安全。

4. 创建 Maven 仓库

4.1 创建 Releases 和 Snapshots 仓库

Nexus 提供了多种仓库格式,本次主要配置 Maven 仓库,区分发布版(Releases)和快照版(Snapshots)。

  • 点击 Repositories,选择 Create repository
  • 选择 Maven2 (hosted),创建发布版本仓库:
    • Repository ID:bzkj-repo-release
    • Version policy:Release
    • Deployment policy:Allow redeploy
    • Blob store:default

  • 同样方式创建 Snapshots 仓库,注意选择 Version policySnapshot

  • 最后,创建一个 Group 仓库,用于将多个仓库(如 Snapshots、Releases、以及其他远程仓库)整合在一起。
    • Repository ID:maven-public
    • Member repositories:将 Releases 和 Snapshots 仓库加入。

4.2 配置 Blob Store 和 Cleanup 策略

Blob Store 用于存储上传到 Nexus 的组件。可以配置多个 Blob Store 来管理不同类型的数据,减少存储开销。

  1. 进入 Repository > Blob stores,查看和管理 Blob 存储。
  2. 设置 Cleanup 策略,定期清理不再需要的快照版本,节省存储空间。

5. 上传与管理依赖包

5.1 手动上传依赖包

  1. 登录 Nexus 后,选择 Browse server contents,点击Upload,选择创建的 Releases 仓库。
  2. 填写上传的坐标信息(groupId、artifactId、version 等)并选择相应的文件进行上传。
  3. 点击 Upload 完成上传操作。

 

5.2 使用 Maven 命令行上传

Maven 提供了上传依赖的命令行工具,通过配置项目的 pom.xml 文件,可以直接上传到 Nexus 仓库。

  • pom.xml 中配置 distributionManagement
<distributionManagement>
  <repository>
    <id>nexus-releases</id>
    <url>http://<服务器IP>:8081/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus-snapshots</id>
    <url>http://<服务器IP>:8081/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>
  • 使用以下命令将组件上传到 Nexus:
mvn deploy

5.3 批量上传本地仓库依赖包

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
    case $opt in
        r) REPO_URL="$OPTARG"
        ;;
        u) USERNAME="$OPTARG"
        ;;
        p) PASSWORD="$OPTARG"
        ;;
    esac
done
 
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

 创建import.sh文件,将以上内容黏贴到文件中。将文件放到需要上传的依赖包的路径下:

 右键选择Open Git Bash here,执行以下命令:

./import.sh -u admin -p admin123 -r http://<仓库IP>:8081/repository/bzkj-repo-snapshot/

等待上传完成即可。
注意:

  • SNAPSHOT 仓库只能接受以 -SNAPSHOT 结尾的版本。
  • Maven 仓库有特定的文件结构和命名规则,文件路径或文件名格式不正确无法上传成功。


6. Maven 项目依赖配置

6.1 配置 Maven settings.xml

为了让 Maven 优先使用配置的私有仓库,可以在 settings.xml 中添加私有仓库的配置。

<servers>

	<!-- Nexus 仓库的认证信息 -->
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors>
   
   <!-- 优先使用 Nexus 仓库 -->
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf> <!-- 代理所有仓库 -->
      <url>http://<仓库IP>:8081/repository/maven-public/</url>
      <mirrorOfReleases>true</mirrorOfReleases>
      <mirrorOfSnapshots>true</mirrorOfSnapshots>
    </mirror>

    <!-- 如果 Nexus 中没有,再从阿里云获取 -->
    <mirror>
      <id>aliyun</id>
      <mirrorOf>external:*</mirrorOf> <!-- external:* 表示只在 Nexus 仓库之外获取 -->
      <url>https://maven.aliyun.com/repository/public/</url>
    </mirror>
  </mirrors>

  <profiles>

	<profile>
      <id>nexus</id>
      <repositories>
        <!-- Nexus 仓库的发布版本仓库 -->
        <repository>
          <id>nexus-releases</id>
          <url>http://<仓库IP>:8081/repository/bzkj-repo-release/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>

        <!-- Nexus 仓库的快照版本仓库 -->
        <repository>
          <id>nexus-snapshots</id>
          <url>http://<仓库IP>:8081/repository/bzkj-repo-snapshot/</url>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>

        <!-- 阿里云的仓库作为备选 -->
        <repository>
          <id>aliyun-central</id>
          <url>https://maven.aliyun.com/repository/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

 maven依赖加载顺序:

  • 优先从nexus 的maven public仓库组中获取依赖,maven public中仓库依赖加载顺序为:bzkj-repo-release -> bzkj-repo-snapshot
  • nexus maven public仓库组中找不到依赖情况下,从阿里云仓库加载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值