CICD: Apache Maven
- TAGS: cicd
安装 Maven
在maven官网下载安装包:http://maven.apache.org/download.cgi
MAVEN_VERSION=3.10.0 wget https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz tar xf apache-maven-${MAVEN_VERSION}-bin.tar.gz -C /usr/local/ ln -sv /usr/local/apache-maven-${MAVEN_VERSION}/ /usr/local/maven vim /usr/local/maven/conf/settings.xml <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> cat >/etc/profile.d/mvn.sh <<\eof export MAVEN_HOME=/usr/local/maven export PATH=$PATH:$MAVEN_HOME/bin eof source /etc/profile mvn -v
关于本地仓库的位置
默认位置: ${user.home}/.m2/repository
- windows:C:\Documents and Settings\Administrator\.m2
- Linux:/root/.m2
vim /usr/local/apache-maven-3.3.3/conf/setting.xml
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
私服nexus
配置在局域网环境中,为局域网中所有开发人员提供 jar 包的统一管理 本地仓库(本机)—>私服(局域网)—>中心仓库(外部网络)
官方文档: https://help.sonatype.com/en/sonatype-nexus-repository.html
安装
二进制安装
下载最新版本的nexus
NEXUS_VERSION=3.97.0 OS=$(uname -s | tr 'A-Z' 'a-z') ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') PLATFORM="$OS-$ARCH" wget https://download.sonatype.com/nexus/3/nexus-${NEXUS_VERSION}-13-${OS}-aarch_64.tar.gz tar xf nexus-${NEXUS_VERSION}-13-${OS}-aarch_64.tar.gz -C /usr/local ln -sv /usr/local/nexus-${NEXUS_VERSION}-13 /usr/local/nexus #/usr/local/nexus — 程序安装目录 #/usr/local/sonatype-work — 数据和工作目录 useradd --system --no-create-home --shell /bin/false nexus chown -R nexus:nexus /usr/local/nexus/ chown -R nexus:nexus /usr/local/sonatype-work #配置内存。推荐配置(8GB 内存服务器) vim /usr/local/nexus/bin/nexus.vmoptions -Xms512m -Xmx2048m -XX:MaxDirectMemorySize=2048m -XX:+UnlockExperimentalVMOptions -XX:+UseZGC
systemd 服务文件
cat <<EOF> /lib/systemd/system/nexus.service [Unit] Description=Nexus Repository Manager After=network.target [Service] Type=forking LimitNOFILE=65536 User=nexus Group=nexus ExecStart=/usr/local/nexus/bin/nexus start ExecStop=/usr/local/nexus/bin/nexus stop Restart=on-failure RestartSec=30 [Install] WantedBy=multi-user.target EOF #export RUN_AS_USER=root systemctl daemon-reload && systemctl enable --now nexus systemctl status nexus # admin 密码 cat /usr/local/sonatype-work/nexus3/admin.password
默认的端口是8081。可修改 nexus.properties 文件
NEXUS内部使用 Jetty 作为服务器
首次登录向导会引导你:
- 设置新的管理员密码
- 配置匿名访问(可选)
- 完成初始设置
nginx 代理
sudo apt install -y nginx
cat <<\EOF> /etc/nginx/sites-available/nexus
upstream nexus_backend {
server 127.0.0.1:8081;
}
server {
listen 80;
server_name nexus.example.com;
location / {
proxy_pass http://nexus_backend;
proxy_set_header Host $host;
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;
client_max_body_size 1G;
}
}
EOF
ln -s /etc/nginx/sites-available/nexus /etc/nginx/sites-enabled/
nginx -t && sudo systemctl reload nginx
docker compose 安装
mkdir /data/nexus-data && chown -R 200 /data/nexus-data docker run -d -p 8081:8081 --name nexus --restart=always -v /data/nexus-data:/nexus-data sonatype/nexus3 # 停止要确保有足够的时间关闭数据 docker stop --time=120 nexus # admin的初始密码 cat /data/nexus-data/admin.password # 访问 http://your_ip:8081 记得更改下密码
mkdir /opt/nexus && cd /opt/nexus cat <<\EOF> docker-compose.yml services: nexus: # 官方镜像有 linux/arm64,通过 dockerd 代理拉取 # (/etc/systemd/system/docker.service.d/http-proxy.conf) image: sonatype/nexus3:3.97.0 container_name: nexus restart: unless-stopped environment: # 注意:这个变量会整体覆盖镜像里的默认值,镜像原本只设了 javaprefs 那一条, # 自定义时必须把它带上,否则 Java Preferences 会往只读的 /opt 里写。 # 堆和堆外:镜像 nexus.vmoptions 默认 2703m x2,这台机器总共 3.2G # 还跑着 SonarQube,照抄会把整机压进 swap。 INSTALL4J_ADD_VM_PARAMS: >- -Djava.util.prefs.userRoot=/nexus-data/javaprefs -Xms1024m -Xmx1024m -XX:MaxDirectMemorySize=768m # 兜底,防止 Nexus 失控时把 SonarQube 挤死 mem_limit: 2g ports: - "8081:8081" volumes: - nexus-data:/nexus-data healthcheck: test: ["CMD-SHELL", "curl -fsS http://localhost:8081/service/rest/v1/status -o /dev/null || exit 1"] interval: 30s timeout: 10s retries: 5 start_period: 180s networks: - nexusnet networks: nexusnet: driver: bridge ipam: config: - subnet: 172.28.0.0/16 volumes: nexus-data: EOF docker compose up -d docker compose ps docker compose logs -f nexus docker compose restart nexus # 备份数据卷 docker run --rm -v nexus_nexus-data:/src:ro -v /root:/bak \ public.ecr.aws/docker/library/ubuntu:24.04 \ tar -czf /bak/nexus-data-$(date +%F).tar.gz -C /src .
仓库类型介绍
Nexus 支持三种仓库类型:
| 类型 | 说明 | 使用场景 |
|---|---|---|
| Proxy(代理) | 代理远程仓库,缓存到本地 | 加速依赖下载,节省带宽 |
| Hosted(托管) | 存储本地发布的构件 | 发布团队内部构建的包 |
| Group(组) | 将多个仓库合并为一个 URL | 简化客户端配置 |
仓库配置
Maven 仓库
Nexus 默认已创建了以下 Maven 仓库:
| 仓库 | 类型 | 作用 |
|---|---|---|
| maven-central | proxy | 代理 repo1.maven.org,缓存第三方 jar |
| maven-releases | hosted | 存你自己的正式版 |
| maven-snapshots | hosted | 存你自己的快照版 |
| maven-public | group | 把上面三个聚合成一个地址,Maven 只连这一个 |
maven-releases 默认是 Disable redeploy,同一个版本号推第二次会 400,发版时注意递增版本。
社区版有用量上限:40,000 components / 100,000 requests 每天(3.87 起从 100k/200k 降下来的),超了会在 45 天宽限期后拒绝写入新构件。代理缓存久了是会逼近 4 万的,留意 Usage Center。
访问权限
- 创建研发人员 “读+上传” 角色 dev,角色给
nx-repository-view-maven2-*-*- Security → Roles
- 创建 ci 用户,并分配开发权限
- Security → Users
省事:
- Administration → Security → Anonymous Access → 勾选 Enable,角色给 nx-anonymous。这样拉包不用密码,只有deploy 需要账号。
Maven 侧:settings.xml
加个阿里云代理加速:
- Repository → Create repository → maven2 (proxy),Remote storage 填 https://maven.aliyun.com/repository/public,然后编辑 maven-public group, 把它拖到 maven-central 前面。
mkdir -p /root/.m2
cat > /root/.m2/settings.xml <<'EOF'
mkdir -p /root/.m2 cat > /root/.m2/settings.xml <<'EOF' <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"> <localRepository>/m2/repository</localRepository> <servers> <!-- id 必须和下面 mirror 的 id 一致,凭据靠 id 匹配 --> <server> <id>nexus</id> <username>ci</username> <password>******</password> </server> <server><id>nexus-releases</id><username>ci</username><password>******</password></server> <server><id>nexus-snapshots</id><username>ci</username><password>******</password></server> </servers> <mirrors> <mirror> <id>nexus</id> <name>nexus maven-public</name> <url>http://10.0.0.105:8081/repository/maven-public/</url> <mirrorOf>*</mirrorOf> <!-- 所有仓库请求都转到 Nexus --> </mirror> </mirrors> <!--引用Nexus库jar--> <profiles> <profile> <id>nexus</id> <!-- url 是占位的,实际被上面的 mirror 接管;这里只为了打开 snapshot 开关 --> <repositories> <repository> <id>central</id><url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id><url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活下载模板--> <activeProfiles><activeProfile>nexus</activeProfile></activeProfiles> </settings> EOF #把旧本地仓库挪开 mv /root/.m2/repository /root/.m2/repository.bak-$(date +%F)
把自己的包发上去
父 pom 加:
<distributionManagement>
<repository>
<id>nexus-releases</id>
<url>http://10.0.0.105:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://10.0.0.105:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
然后 mvn deploy 。版本号带 -SNAPSHOT 自动进 snapshots,否则进 releases。
Jenkins 侧 Jenkinsfile
装 Config File Provider 插件 → Managed files → 新建 Maven settings.xml,把上面内容贴进去,serverCredentialMappings 绑 Jenkins 凭据(这样密码不落在文件里)。然后在 pipeline 里:
stage('mvn编译构建') { steps { configFileProvider([configFile(fileId: 'nexus-settings', variable: 'MVN_SETTINGS')]) { sh "mvn -s $MVN_SETTINGS -Dmaven.repo.local=/m2/repository clean package -Pdev-k8s -DskipTests" } } }
npm 仓库
创建 npm 仓库的步骤:
- 进入 Administration → Repositories → Create repository
- 选择 npm (proxy)
- 填写仓库名称,设置远程 URL 为 https://registry.npmjs.org
- 保存后,客户端即可配置使用该仓库
客户端配置:
# 设置 registry npm config set registry http://nexus.example.com/repository/npm-group/ # 如需认证 npm adduser --registry http://nexus.example.com/repository/npm-group/
Docker 仓库
配置 Docker 仓库需要额外的端口:
- 创建 docker (hosted) 仓库,设置 HTTP 端口(如 5000)
- 确保 Nginx 或 HAProxy 转发该端口
客户端配置:
# 配置 Docker 信任(如果使用自签名证书) sudo mkdir -p /etc/docker/certs.d/nexus.example.com:5000 # 复制证书到该目录 # 登录 docker login nexus.example.com:5000 # 推送镜像 docker tag my-image nexus.example.com:5000/my-image docker push nexus.example.com:5000/my-image
PyPI 仓库
配置 Python 包仓库:
- 创建 pypi (proxy) 仓库,远程 URL 设为 https://pypi.org
- 创建 pypi (hosted) 仓库用于上传私有包
- 创建 pypi (group) 仓库合并两者
pip 配置:
# 命令行方式 pip install --index-url http://nexus.example.com/repository/pypi-group/simple/ \ --trusted-host nexus.example.com package_name # 或写入 ~/.pip/pip.conf [global] index-url = http://nexus.example.com/repository/pypi-group/simple/ trusted-host = nexus.example.com
运维
清理策略
定期清理不再需要的构件以释放空间:
- Administraton → Blob Stores:查看存储空间使用情况
- Administration → Tasks:创建定时清理任务
- Administration → Cleanup Policies:配置自动清理规则
备份与恢复
备份 sonatype-work 目录即可:
# 备份前停止服务 sudo systemctl stop nexus # 备份 tar -czf nexus-backup-$(date +%Y%m%d).tar.gz /usr/local/sonatype-work/ # 恢复时解压回原位置 tar -xzf nexus-backup-20261010.tar.gz -C / # 启动服务 sudo systemctl start nexus
日志查看
# 系统服务日志 sudo journalctl -u nexus -f # Nexus 内部日志 tail -f /usr/local/sonatype-work/nexus3/log/nexus.log