maven私有仓库之nexus简单使用

概述

Nexus是Sonatype公司开发的一款开源的仓库管理系统,是目前最流行的私有仓库解决方案,主要用于存储和管理各类二进制工件(Artifact),包括Java Maven依赖、npm包、Docker镜像、PyPI包、RPM包等等。

Nexus的核心价值

  • 依赖加速:缓存公共仓库的依赖包,大幅提升构建速度,减少外网带宽消耗
  • 私有包管理:存储企业内部开发的私有包,实现内部组件共享与版本管理
  • 版本可控:统一管理所有依赖版本,避免"依赖地狱"问题
  • 安全审计:对依赖包进行安全漏洞扫描,降低供应链攻击风险
  • 构建稳定性:避免公共仓库故障、网络波动导致的构建失败
  • 合规管控:控制依赖包的引入来源,满足企业合规要求

官网地址:
https://www.sonatype.com
官网下载地址
https://www.sonatype.com/download-oss-sonatype

Docker部署Nexus

# 这里指定了镜像源,拉取镜像
root@master:~# docker pull  docker.1ms.run/sonatype/nexus3

# 创建数据卷
root@master:~# docker volume create nexus-data

# 创建容器
root@master:~# docker run -d --name nexus3 -p 8081:8081 --cpus 2 --memory 4GB --restart always -v nexus-data:/nexus-data docker.1ms.run/sonatype/nexus3

访问测试,这里需要等待大概1分钟左右,服务在启动
http://${ip}:8081

ip输入你自己的

image

用户名默认为root,初始密码会自动生成在一个文件中,看下面的获取方式

# 查看数据卷的位置
root@master:/var/lib/docker/volumes/nexus-data/_data# docker volume inspect nexus-data
[
    {
        "CreatedAt": "2026-07-17T11:23:18+08:00",
        "Driver": "local",
        "Labels": null,
         # 这里是真正存在的地方
        "Mountpoint": "/var/lib/docker/volumes/nexus-data/_data",
        "Name": "nexus-data",
        "Options": null,
        "Scope": "local"
    }
]

# 查看admin.password 文件
root@master:/var/lib/docker/volumes/nexus-data/_data# cat /var/lib/docker/volumes/nexus-data/_data/admin.password 
6dcd4165-2572-4c2e-bce7-ee3035a5fce1

登录之后会让你重新输入新的密码,我这里使用huangsirDevops
image

配置nexus的镜像源

默认的nexus镜像源为apache的,在国内访问可能会访问不通或者很慢,这里替换成阿里云的镜像源

  • 依次点击settings,Repositories、maven- central
    image

  • 下拉找到proxy,配置remote storage即可

# 阿里云镜像加速地址
http://maven.aliyun.com/nexus/content/groups/public/

image

最后点击save即可

配置maven连接使用nexus

maven使用参考这篇文章:maven使用详解

连接nexus有两种方式,一个是局部连接,一个是全局连接方式

局部就是修改Java项目中的pom.xml文件
全局就是修改maven的settings.xml文件

这里我使用全局方式,修改maven配置文件

root@master:~# cat /usr/local/maven/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- 本地依赖的地址 -->
  <localRepository>/data00/maven/repository</localRepository>
  <!-- 私服账号密码,mvn deploy上传jar时鉴权用 -->
  <servers>
    <!-- 正式包仓库凭证,id要和pom distributionManagement repository id完全一致 -->
	<server>
    <id>nexus</id>
    <username>admin</username>
    <password>huangsirDevops</password>
  	</server>
  </servers>

  <!-- 镜像拦截:所有依赖下载统一走内网Nexus公共仓库 -->
  <mirrors>
    <mirror>
      <id>central</id>
      <mirrorOf>*</mirrorOf>
      <url>http://10.37.97.56:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <!-- 仓库环境配置:依赖仓库 + Maven插件仓库 -->
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <url>http://10.37.97.56:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus</id>
          <url>http://10.37.97.56:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <!-- 自动激活nexus配置,不用手动加-P参数 -->
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

</settings>

测试编译Ruoyi项目

root@master:/data00/gitcode# git clone https://gitee.com/y_project/RuoYi-Cloud.git

# 切换到springboot2分支
root@master:/data00/gitcode/RuoYi-Cloud# git checkout springboot2
Branch 'springboot2' set up to track remote branch 'springboot2' from 'origin'.
Switched to a new branch 'springboot2'
 
# 查看当前所在分支
root@master:/data00/gitcode/RuoYi-Cloud# git branch
  master
* springboot2
 
root@master:/data00/gitcode/RuoYi-Cloud# cd ruoyi-modules/ruoyi-job/
root@master:/data00/gitcode/RuoYi-Cloud/ruoyi-modules/ruoyi-job# ll
total 8
-rw-r--r-- 1 root root 3064 Jul 16 17:12 pom.xml
drwxr-xr-x 3 root root 4096 Jul 16 17:10 src

# 开始打包
root@master:/data00/gitcode/RuoYi-Cloud# mvn clean package -Dmaven.test.skip=true
### ...会发现从我们配置nexus仓库下载依赖
ownloading from nexus: http://10.37.97.56:8081/repository/maven-public/com/alibaba/druid/1.2.6/druid-1.2.6.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar (85 kB at 127 kB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/com/typesafe/config/1.2.1/config-1.2.1.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/com/alibaba/fastjson/1.2.83/fastjson-1.2.83.jar (672 kB at 992 kB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/org/antlr/antlr4-runtime/4.8/antlr4-runtime-4.8.jar (338 kB at 493 kB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/commons-pool/commons-pool/1.6/commons-pool-1.6.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/commons-pool/commons-pool/1.6/commons-pool-1.6.jar (111 kB at 145 kB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/org/apache/dubbo/extensions/dubbo-filter-seata/1.0.0/dubbo-filter-seata-1.0.0.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/com/typesafe/config/1.2.1/config-1.2.1.jar (220 kB at 284 kB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/io/seata/seata-core/1.5.2/seata-core-1.5.2.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/commons-lang/commons-lang/2.6/commons-lang-2.6.jar (284 kB at 366 kB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/io/seata/seata-common/1.5.2/seata-common-1.5.2.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/com/alibaba/druid/1.2.6/druid-1.2.6.jar (3.6 MB at 4.2 MB/s)
Downloading from nexus: http://10.37.97.56:8081/repository/maven-public/io/seata/seata-discovery-core/1.5.2/seata-discovery-core-1.5.2.jar
Downloaded from nexus: http://10.37.97.56:8081/repository/maven-public/com/ibm/icu/icu4j/61.1/icu4j-61.1.jar (12 MB at 13 MB/s)

[INFO] ruoyi .............................................. SUCCESS [  3.789 s]
[INFO] ruoyi-common ....................................... SUCCESS [  0.002 s]
[INFO] ruoyi-common-core .................................. SUCCESS [ 19.122 s]
[INFO] ruoyi-api .......................................... SUCCESS [  0.002 s]
[INFO] ruoyi-api-system ................................... SUCCESS [  0.171 s]
[INFO] ruoyi-common-redis ................................. SUCCESS [  1.205 s]
[INFO] ruoyi-common-security .............................. SUCCESS [  0.323 s]
[INFO] ruoyi-auth ......................................... SUCCESS [ 11.809 s]
[INFO] ruoyi-gateway ...................................... SUCCESS [  8.248 s]
[INFO] ruoyi-visual ....................................... SUCCESS [  0.001 s]
[INFO] ruoyi-visual-monitor ............................... SUCCESS [  3.703 s]
[INFO] ruoyi-common-datasource ............................ SUCCESS [  4.082 s]
[INFO] ruoyi-common-datascope ............................. SUCCESS [  0.094 s]
[INFO] ruoyi-common-log ................................... SUCCESS [  0.095 s]
[INFO] ruoyi-common-swagger ............................... SUCCESS [  1.727 s]
[INFO] ruoyi-modules ...................................... SUCCESS [  0.001 s]
[INFO] ruoyi-modules-system ............................... SUCCESS [  1.783 s]
[INFO] ruoyi-modules-gen .................................. SUCCESS [  1.051 s]
[INFO] ruoyi-modules-job .................................. SUCCESS [  0.786 s]
[INFO] ruoyi-modules-file ................................. SUCCESS [  5.455 s]
[INFO] ruoyi-common-seata ................................. SUCCESS [ 20.015 s]
[INFO] ruoyi-common-sensitive ............................. SUCCESS [  0.074 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:29 min
[INFO] Finished at: 2026-07-17T13:52:29+08:00
[INFO] ------------------------------------------------------------------------

查看nexus镜像仓库下载的依赖
image

posted @ 2026-07-20 10:17  huangSir-devops  阅读(122)  评论(0)    收藏  举报
作者:你的名字
出处:你的博客链接
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。