一、概述
Nexus概述
- Nexus 是 Sonatype 公司发布的一款仓库(Repository)管理软件,常用来搭建 Maven 私服,所以也有人将 Nexus 称为“Maven仓库管理器”。
- Maven 私服其实并不是 Maven 的核心概念,它仅仅是一种衍生出来的特殊的仓库,但这并不代表它不重要,相反由于私服具有降低中央仓库负荷、节省外网带宽、以及提高项目稳定性等优点,使得私服在实际开发过程中得到了相当普遍地使用。
- 能够帮助我们建立私服的软件被称为 Maven 仓库管理器,主要有以下 3 种:
- Apache Archiva
- JFrog Artifactory
- Sonatype Nexus
- 其中,Sonatype Nexus 是当前最流行,使用最广泛的 Maven 仓库管理器。Nexus 分为开源版和专业版,其中开源版足以满足大部分 Maven 用户的需求。
二、在Docker
中使用Nexus
搭建Maven
私服
1、搜索查找 docker 的 nexus 镜像:
[root@192 ~]# docker search sonatype/nexus
NAME DESCRIPTION STARS OFFICIAL
sonatype/nexus3 Sonatype Nexus Repository 3 1387
sonatype/nexus Sonatype Nexus 450
sonatype/nexus-iq-server Sonatype Nexus IQ Server 28
sonatype/nexus-iq-cli 5
sonatype/nexus-platform-cli 0
[root@192 ~]#
2、下载 docker 的 nexus 镜像:下载指定的版本命令为 docker pull sonatype/nexus3:版本号
[root@192 ~]# docker pull sonatype/nexus3
Using default tag: latest
latest: Pulling from sonatype/nexus3
a15b996d0c1b: Already exists
106172438eb3: Pull complete
......
Status: Downloaded newer image for sonatype/nexus3:latest
docker.io/sonatype/nexus3:latest
[root@192 ~]#
3、运行 docker 命令构建镜像实例:
# -p 9090:8081 把镜像实例的 8081 端口映射成 物理主机的 9090 端口
[root@192 ~]# docker run -d --name=nexus3 -p 9090:8081 sonatype/nexus3
至此Maven私有仓库搭建完成,直接访问 ip:9090 就可以进入私有仓库的展示界面。
三、Nexus
配置
查看登录 Nexus 的密码:
密码存放在docker实例的 /nexus-data
目录下面名称为 admin.password 使用命令进入docker实例查看即可
使用admin登录之后展示界面:
开发工具中配置使用私有仓库
1、找到本地的maven文件
2、在 servers 标签下添加如下配置:
<server>
<!-- 上传的仓库:一定要和私有仓库中的保持一致 -->
<id>maven-releases</id>
<username>用户名</username>
<password>密码</password>
</server>
<server>
<!-- 上传的仓库:一定要和私有仓库中的保持一致 -->
<id>maven-snapshots</id>
<username>用户名</username>
<password>密码</password>
</server>
3、在 mirrors 标签添加如下配置
<mirror>
<!-- 配置下载依赖包的仓库地址,公共仓库组地址 -->
<id>maven-public</id>
<mirrorOf>*</mirrorOf>
<name>maven-public</name>
<url>http://192.168.1.110:9090/repository/maven-public/</url>
</mirror>
4、在开发的项目的 pom.xml
配置上传的仓库地址,用于发布自己的代码到私有仓库。
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>maven-releases</name>
<url>http://192.168.1.110:9090/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>maven-snapshots</name>
<url>http://192.168.1.110:9090/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
5、使用 maven 命令把 jar 发布到私有仓库
6、命令运行之后看到提示发布成功即可: