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

相关推荐
Patrick_Wilson3 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
Suroy4 天前
DockerView-Go:用 Go 写一个终端 Docker 监控工具,顺便做了个 Web 仪表盘
docker
云恒要逆袭4 天前
运行你的第一个Docker容器
后端·docker·容器
宋均浩5 天前
# Docker 镜像瘦身实战:从 1.2G 到 80MB 的五个优化步骤
ci/cd·docker
程序员老赵5 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程
WangMingHua1116 天前
LM Studio Docker 部署——本地大模型一键启动
docker
曲幽7 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
武子康9 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
小宇宙Zz11 天前
Maven依赖冲突
java·服务器·maven
Alsn8612 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker