Maven 私服Nexus的搭建与应用

89 阅读8分钟

私服 Nexus 搭建与应用

一、为什么需要私服?

在企业级开发中,直接使用公共Maven中央仓库会面临网络、权限等诸多的挑战,私服(Private Repository)应运而生。

1. 加速构建过程

问题:从Maven Central下载依赖速度慢,特别是跨国网络
解决方案:私服缓存常用依赖,本地网络高速访问

2. 部署私有组件

问题:公司内部开发的组件不能上传到公共仓库
解决方案:私服提供内部组件托管,实现组织内共享

3. 审计和安全控制

问题:无法控制开发人员使用哪些第三方依赖
解决方案:私服可以设置白名单,只允许使用经过审核的依赖

4. 构建稳定性

问题:公共仓库不稳定或宕机导致构建失败
解决方案:私服提供高可用的依赖服务

5. 离线开发支持

问题:网络中断时无法下载新依赖
解决方案:私服缓存所有使用过的依赖,支持离线开发

二、Nexus 3 快速搭建

1. 使用Docker快速搭建(推荐)

1.1 环境要求
  • Docker & Docker Compose
  • 至少4GB内存
  • 20GB磁盘空间
1.2 Docker Compose部署
# docker-compose.yml
version: '3.8'

services:
  nexus:
    image: sonatype/nexus3:3.42.0
    container_name: nexus3
    restart: unless-stopped
    ports:
      - "8081:8081"
      - "8082:8082"  # 如果需要额外的端口
    volumes:
      - nexus-data:/nexus-data
      - ./nexus-config:/nexus-config:ro
    environment:
      - INSTALL4J_ADD_VM_PARAMS=-Xms2048m -Xmx2048m -XX:MaxDirectMemorySize=2048m
    networks:
      - nexus-net

volumes:
  nexus-data:
    driver: local

networks:
  nexus-net:
    driver: bridge
1.3 启动Nexus
# 创建目录
mkdir nexus-setup && cd nexus-setup
mkdir -p nexus-data nexus-config

# 启动服务
docker-compose up -d

# 查看启动日志
docker logs -f nexus3

# 等待初始化完成(约2-3分钟)
# 访问 http://localhost:8081
1.4 初始配置
  1. 登录管理界面

    • 地址:http://localhost:8081
    • 默认用户名:admin
    • 初始密码:查看日志获取 docker logs nexus3 | grep "admin.password"
  2. 修改管理员密码

  3. 配置匿名访问(根据安全要求决定)

2. 二进制部署方式

2.1 系统要求
  • Java 8或11(推荐OpenJDK)
  • Linux/Windows/macOS
  • 4GB+内存,20GB+磁盘空间
2.2 安装步骤
# 下载Nexus(以Linux为例)
cd /opt
wget https://download.sonatype.com/nexus/3/nexus-3.42.0-01-unix.tar.gz

# 解压
tar -zxvf nexus-3.42.0-01-unix.tar.gz
ln -s nexus-3.42.0-01 nexus

# 创建nexus用户(安全考虑)
useradd -M -s /bin/bash nexus
chown -R nexus:nexus /opt/nexus*

# 配置服务
vim /opt/nexus/bin/nexus.vmoptions

修改内存配置:

-Xms2048m
-Xmx2048m
-XX:MaxDirectMemorySize=2048m
2.3 创建系统服务
# /etc/systemd/system/nexus.service
[Unit]
Description=Nexus Service
After=network.target

[Service]
Type=forking
User=nexus
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
Restart=on-abort
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
2.4 启动服务
systemctl daemon-reload
systemctl enable nexus
systemctl start nexus
systemctl status nexus

三、Nexus仓库类型详解

Nexus 3支持多种仓库类型,理解这些类型是正确配置的关键。

1. 代理仓库(Proxy Repository)

  • 代理远程仓库(如Maven Central)
  • 缓存下载的构件
  • 提供访问控制和审计

2. 宿主仓库(Hosted Repository)

  • 存储内部发布的构件
  • 分为Release和Snapshot两种
  • 支持部署操作

3. 仓库组(Repository Group)

  • 聚合多个仓库,提供统一访问入口
  • 按顺序搜索构件
  • 简化客户端配置

四、Maven客户端配置

1. settings.xml配置详解

1.1 配置镜像(加速依赖下载)
<!-- ~/.m2/settings.xml -->
<settings>
    <mirrors>
        <!-- 配置Nexus仓库组作为中央仓库的镜像 -->
        <mirror>
            <id>nexus-central</id>
            <name>Nexus Central Mirror</name>
            <url>http://nexus-server:8081/repository/maven-public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        
        <!-- 镜像所有仓库(推荐) -->
        <mirror>
            <id>nexus-all</id>
            <name>Nexus All Repository</name>
            <url>http://nexus-server:8081/repository/maven-public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
</settings>
1.2 配置服务器认证(部署权限)
<settings>
    <servers>
        <!-- Snapshots仓库部署认证 -->
        <server>
            <id>nexus-snapshots</id>
            <username>deployment-user</username>
            <password>deployment-password</password>
        </server>
        
        <!-- Releases仓库部署认证 -->
        <server>
            <id>nexus-releases</id>
            <username>deployment-user</username>
            <password>deployment-password</password>
        </server>
        
        <!-- 第三方依赖仓库认证(如果需要) -->
        <server>
            <id>nexus-thirdparty</id>
            <username>deployment-user</username>
            <password>deployment-password</password>
        </server>
    </servers>
</settings>
1.3 配置Profile(可选)
<settings>
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>Nexus</name>
                    <url>http://nexus-server:8081/repository/maven-public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
                    <name>Nexus</name>
                    <url>http://nexus-server:8081/repository/maven-public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

2. 项目pom.xml配置

2.1 配置部署仓库
<project>
    <!-- 其他配置 -->
    
    <distributionManagement>
        <!-- 发布版本仓库 -->
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus-server:8081/repository/maven-releases/</url>
        </repository>
        
        <!-- 快照版本仓库 -->
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://nexus-server:8081/repository/maven-snapshots/</url>
            <uniqueVersion>false</uniqueVersion>
        </snapshotRepository>
        
        <!-- 站点文档仓库(可选) -->
        <site>
            <id>nexus-site</id>
            <name>Nexus Site Repository</name>
            <url>dav:http://nexus-server:8081/repository/site/</url>
        </site>
    </distributionManagement>
</project>

五、mvn deploy部署组件

1. 部署配置验证

# 检查部署配置是否正确
mvn help:effective-pom | grep -A 10 -B 10 distributionManagement

# 验证仓库连接
mvn deploy:deploy-file -Durl=http://nexus-server:8081/repository/maven-releases/ \
  -DrepositoryId=nexus-releases \
  -Dfile=test.jar \
  -DgroupId=com.test \
  -DartifactId=test \
  -Dversion=1.0.0 \
  -Dpackaging=jar

2. 标准部署流程

# 1. 清理并部署快照版本
mvn clean deploy

# 2. 仅部署到快照仓库(跳过测试)
mvn deploy -DskipTests

# 3. 部署源码和Javadoc
mvn clean deploy -DskipTests -P attach-sources,attach-javadoc

3. 部署配置示例

<!-- 配置源码和Javadoc附件 -->
<build>
    <plugins>
        <!-- 源码打包插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        
        <!-- Javadoc打包插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.4.0</version>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <doclint>none</doclint>  <!-- 忽略文档检查错误 -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
        
        <!-- GPG签名插件(Release版本需要) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>sign-artifacts</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>sign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

六、Snapshot vs Release版本详解

1. 版本号约定

<!-- 快照版本(开发阶段) -->
<version>1.0.0-SNAPSHOT</version>

<!-- 发布版本(稳定版本) -->
<version>1.0.0</version>

2. Snapshot版本特性

2.1 唯一性处理
# 每次部署生成带时间戳的版本
1.0.0-20231215.083042-1.jar
1.0.0-20231215.083042-2.jar  # 同一天多次部署
1.0.0-20231216.093015-1.jar  # 第二天部署
2.2 缓存策略
<!-- Maven客户端配置Snapshot更新策略 -->
<settings>
    <profiles>
        <profile>
            <id>snapshot-policy</id>
            <repositories>
                <repository>
                    <id>snapshots</id>
                    <snapshots>
                        <updatePolicy>always</updatePolicy>  <!-- 总是检查更新 -->
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
</settings>

更新策略选项

  • always:每次构建都检查更新
  • daily:每天第一次构建检查更新(默认)
  • interval:XXX:每隔XXX分钟检查更新
  • never:从不检查更新

3. Release版本特性

3.1 版本不可变
# Release版本部署后不能修改
1.0.0.jar  # 部署后永远保持不变

# 如果发现bug,必须发布新版本
1.0.1.jar  # bug修复版本
1.1.0.jar  # 新功能版本
2.0.0.jar  # 不兼容的API修改
3.2 部署限制
# 同一个Release版本只能部署一次
mvn deploy  # 第一次部署成功
mvn deploy  # 第二次部署失败:409 Conflict

4. 使用场景对比

特性Snapshot版本Release版本
版本号以-SNAPSHOT结尾稳定版本号
部署频率频繁部署,多次覆盖一次性部署,不可覆盖
使用场景开发阶段、CI/CD流水线生产环境、正式发布
稳定性不稳定,随时可能变化稳定,经过测试验证
依赖更新可配置更新策略需要明确版本升级

5. 最佳实践

5.1 版本管理策略
<!-- 开发阶段使用Snapshot -->
<version>2.1.0-SNAPSHOT</version>

<!-- 发布时移除SNAPSHOT后缀 -->
<version>2.1.0</version>

<!-- 发布后立即升级到下一个Snapshot -->
<version>2.1.1-SNAPSHOT</version>
5.2 自动化版本管理
<!-- 使用versions插件管理版本 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.11.0</version>
        </plugin>
    </plugins>
</build>
# 版本管理命令
mvn versions:set -DnewVersion=2.1.0           # 设置Release版本
mvn versions:set -DnewVersion=2.1.1-SNAPSHOT  # 设置Snapshot版本
mvn versions:commit                           # 确认版本变更
mvn versions:revert                          # 回滚版本变更
5.3 CI/CD集成
# GitLab CI示例
stages:
  - snapshot
  - release

deploy-snapshot:
  stage: snapshot
  script:
    - mvn clean deploy -DskipTests
  only:
    - develop

deploy-release:
  stage: release  
  script:
    - mvn versions:set -DnewVersion=$RELEASE_VERSION
    - mvn clean deploy -DskipTests -P release
    - mvn versions:set -DnewVersion=$NEXT_SNAPSHOT_VERSION
  only:
    - tags

七、高级特性与最佳实践

1. 仓库清理策略

# 定期清理旧的Snapshot版本
# Nexus管理界面 → Repository → 选择仓库 → Cleanup Policies

# 设置保留策略:
# - 保留最近10个版本
# - 删除超过30天的Snapshot

2. 安全配置

# 创建专用部署账户
# Nexus管理界面 → Security → Users → Create User

# 权限配置:
# - nx-repository-view-*-*-*: 仓库浏览权限
# - nx-repository-admin-*-*-*: 仓库管理权限  
# - nx-repository-view-*-*-* + nx-repository-write: 部署权限

3. 高可用配置

# 生产环境Docker Compose配置
version: '3.8'

services:
  nexus:
    image: sonatype/nexus3:3.42.0
    deploy:
      replicas: 2
      restart_policy:
        condition: on-failure
    volumes:
      - nexus-data:/nexus-data
    environment:
      - INSTALL4J_ADD_VM_PARAMS=-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m
    networks:
      - nexus-net

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - nexus
    networks:
      - nexus-net

volumes:
  nexus-data:
    driver: local

networks:
  nexus-net:
    driver: bridge

八、故障排查

1. 常见问题解决

# 1. 部署认证失败
# 检查settings.xml中的server配置是否与pom.xml中的id匹配

# 2. 依赖下载失败
# 检查网络连接和Nexus服务状态
curl -I http://nexus-server:8081/repository/maven-public/

# 3. 版本冲突
# 检查依赖树,排除冲突版本
mvn dependency:tree -Dverbose

# 4. 磁盘空间不足
# 清理旧的Snapshot版本或调整保留策略

2. 监控和日志

# 查看Nexus日志
docker logs nexus3

# 监控磁盘使用
df -h /var/lib/docker/volumes/

# 检查服务健康状态
curl http://nexus-server:8081/service/rest/v1/status

九、总结

通过本章学习,你应该能够:

  1. 理解私服的价值:加速构建、内部组件管理、安全控制
  2. 熟练搭建Nexus:使用Docker或二进制方式部署
  3. 配置Maven客户端:正确设置镜像和部署认证
  4. 掌握部署流程:使用mvn deploy发布组件到私服
  5. 理解版本管理:清晰区分Snapshot和Release版本的使用场景

私服是企业级Maven应用的核心基础设施,正确的搭建和配置能够显著提升开发效率和系统稳定性。在实际应用中,建议根据团队规模和安全要求,制定相应的使用规范和管理流程。