使用 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 地址。

相关推荐
todoitbo14 小时前
群晖NAS部署JmalCloud:Docker安装、文件管理、博客与公网访问完整教程
运维·docker·容器
是枚小菜鸡儿吖21 小时前
Home Assistant跨品牌智能家居教程:Docker部署、米家接入、vivo桥接与远程访问
docker·容器·智能家居
螺蛳粉 螺蛳粉1 天前
第 02 篇:镜像分层与 containerd 2.x 实战:SystemdCgroup 与 pause 镜像配置踩坑(底座认知 02)
docker·云原生·容器·kubernetes·etcd·containerd·kube-apiserver
wefg11 天前
【Docker】镜像相关指令
运维·docker·容器
java_logo1 天前
Docker 部署 DPanel:轻松搭建轻量容器管理平台
运维·docker·容器·podman·容器管理·dpanel·轩辕镜像
死磕音视频1 天前
Open WebUI 入门:用 Docker 装上本地对话界面并连接 Ollama
docker·ollama·本地大模型·open webui
分布式存储与RustFS1 天前
CI 老被 Docker Hub 的 429 卡住,自建 Registry 把镜像搬进内网
运维·docker·开源·镜像仓库·对象存储·ci·rustfs
n112121 天前
Docker 容器时区错 8 小时:3 个坑
运维·docker·容器·故障排查·服务器运维·容器时区
小白的码BUG之路1 天前
K8s -- 搭建过程中经常遇到的问题
云原生·容器·kubernetes
慈云数据1 天前
用 DSH 辅助部署一个网站监控平台:Uptime Kuma + Docker 实战
运维·docker·容器