使用 Nexus3 部署 maven 私服

使用容器方式部署 nexus3, docker 镜像站点

  1. 准备 docker-compose.yaml 文件

bash

mkdir /opt/nexus3
cd /opt/nexus3
cat >docker-compose.yaml<<EOF
version: '2.10.2'

services:
  nexus3:
    image: sonatype/nexus3:3.42.0
    container_name: nexus3
    restart: always
    volumes:
    - ./data:/nexus-data
    ports:
    - 8081:8081
EOF
  1. 启动 nexus3 容器

bash

# 由于 nexus3 容器用户 id 为 200, 我们需要将数据目录权限属主进行修改
mkdir /opt/nexus3/data && chown -R 200 /opt/nexus3/data
# 启动 nexus3
docker compose up -d
  1. 配置 nginx 反向代理

bash

cat > /etc/nginx/conf.d/nexus3.conf <<EOF
server {
   listen 80;
   server_name nexus.liwanggui.com; # 注意替换为自己的域名

   location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_next_upstream off;
        proxy_redirect      off;
        proxy_connect_timeout 300s;
        proxy_send_timeout   900;
        proxy_read_timeout   900;
        proxy_buffer_size    32k;
        proxy_buffers        4 32k;
        proxy_busy_buffers_size 64k;
        proxy_hide_header  Vary;
        proxy_set_header   Accept-Encoding '';
        proxy_set_header   Host   $http_host;
        proxy_set_header   Referer $http_referer;
        proxy_set_header   Cookie $http_cookie;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
   }
}

登录 nexus 仓库, 默认用户名: admin, 密码在数据目录的 admin.password 文件中

创建代理阿里仓库

最后我们将 aliyun 这个仓库加入 maven-public 中

  1. 点击 maven-public 进入设置,将 aliyun 移到右侧
  2. 最后保存即可

在 maven 的 settings.xml 配置文件中添加以下配置

xml

    <mirror>
      <id>nexus3</id>
      <name>内部仓库</name>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus.liwanggui.com/repository/maven-public/</url>
    </mirror>