使用 Docker 搭建 Maven 私服

1. 部署 Nexus 容器

shell 复制代码
# 创建数据目录
mkdir -p /opt/nexus/sonatype-work
chmod 777 /opt/nexus/sonatype-work

# 拉取并运行 Nexus
docker run -d \
  --name nexus \
  -p 8081:8081 \
  -p 8082:8082 \
  -v /opt/nexus/sonatype-work:/nexus-data \
  --restart=always \
  sonatype/nexus3:latest

参数说明:

  • -d: 后台运行
  • --name nexus: 容器名称
  • -p 8081:8081: Web 管理界面端口
  • -p 8082:8082: Docker 仓库端口(可选)
  • -v /opt/nexus/sonatype-work:/nexus-data: 数据持久化
  • --restart=always: 开机自启

2. 获取管理员密码

shell 复制代码
docker exec nexus cat /nexus-data/admin.password

访问 http://IP:8081/,使用账号 admin 和初始密码登录,首次登录需修改密码。

3. 配置阿里云代理仓库

  1. 创建代理仓库:S ettings → Repository → Repositories → Create repository → maven2 (proxy)
配置项 说明
Name aliyun-proxy 仓库名称
Remote storage https://maven.aliyun.com/repository/public 阿里云镜像地址
Blob store default 存储区域
  1. 添加到仓库组 :编辑 maven-public 仓库组,添加 aliyun-proxy 并调整优先级(阿里云优先)。

4. Maven 配置

settings.xml

xml 复制代码
<settings>
  <!-- 本地仓库路径 -->
  <localRepository>/path/to/your/repo</localRepository>
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>

  <!-- 配置私服访问凭证 -->
  <servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
  <!-- 配置镜像,将所有请求转发到私服 -->
  <mirrors>
    <mirror>
      <id>nexus</id>
      <name>Nexus Public Mirror</name>
      <url>http://YOUR_IP:8081/repository/maven-public/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  <!-- 配置仓库(可选,如果使用镜像则不需要) -->
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://YOUR_IP:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>
  <!-- 激活配置 -->
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

项目 pom.xml

xml 复制代码
<distributionManagement>
  <repository>
    <id>nexus</id>
    <url>http://YOUR_IP:8081/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus</id>
    <url>http://YOUR_IP:8081/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

注意 :将 YOUR_IP 替换为实际服务器 IP 地址。

相关推荐
末代程序员_C2 小时前
Maven版本管控:多分支并行开发中的API版本管理之道
大数据·elasticsearch·maven
shjita2 小时前
maven涉及的配置
java·前端·maven
MuzySuntree2 小时前
Ubuntu 下 Maven 构建 Spring Boot 项目报错 release version 17 not supported 解决方案
spring boot·ubuntu·maven
金融小白数据分析之路2 小时前
java 打包exe maven 版本
java·开发语言·maven
Elivs.Xiang2 小时前
基于docker安装MySQL、RabbitMQ、ElasticSearch、minio
linux·mysql·elasticsearch·docker·rabbitmq
雨奔2 小时前
Kubernetes StatefulSet 实战:从创建到运维的完整指南
运维·容器·kubernetes
小兔薯了2 小时前
docker
运维·docker·容器
QC班长10 小时前
Maven公司私库配置踩坑点
java·服务器·maven·intellij-idea
深色風信子12 小时前
Docker newapi
运维·docker·容器·newapi