01.Nexus 简介
Nexus 是一个强大的仓库管理器(主要用于协同开发),极大地简化了内部仓库的维护和外部仓库的访问。2016 年 4 月 6 日 Nexus 3.0 版本发布,相较 2.x 版本有了很大的改变:
- 对低层代码进行了大规模重构,提升性能,增加可扩展性以及改善用户体验。
- 升级界面,极大的简化了用户界面的操作和管理。
- 提供新的安装包,让部署更加简单。
- 增加对 Docker, NeGet, npm, Bower 的支持。
- 提供新的管理接口,以及增强对自动任务的管理。
02.Nexus 安装
# 拉取镜像
root@demo:~# docker pull sonatype/nexus3
root@demo:/usr/local# cd /usr/local/docker/
root@demo:/usr/local/docker# mkdir nexus
root@demo:/usr/local/docker# cd nexus
root@demo:/usr/local/docker/nexus# vi docker-compose.yml
version: '3.1'
services:
nexus:
restart: always
image: sonatype/nexus3
container_name: nexus
ports:
- 8081:8081
volumes:
- /usr/local/docker/nexus/data:/nexus-data
root@demo:/usr/local/docker/nexus# docker-compose up -d
Creating network "nexus_default" with the default driver
Creating nexus ... done
root@demo:/usr/local/docker/nexus# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5859dc8def4 sonatype/nexus3 "sh -c ${SONATYPE_DI…" About a minute ago Restarting (1) 28 seconds ago nexus
# 显示日志
root@demo:/usr/local/docker/nexus# docker logs c5859dc8def4
...
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied
...
root@demo:/usr/local/docker/nexus# docker-compose down
Stopping nexus ... done
Removing nexus ... done
Removing network nexus_default
root@demo:/usr/local/docker/nexus# chmod 777 data/
root@demo:/usr/local/docker/nexus# docker-compose up -d
Creating network "nexus_default" with the default driver
Creating nexus ... done
# 排查
root@demo:/usr/local/docker/nexus# docker-compose up
root@demo:/usr/local/docker/nexus# free -h
total used free shared buff/cache available
Mem: 2.9G 1.5G 849M 1.2M 637M 1.4G
Swap: 0B 0B 0B
root@demo:/usr/local/docker/nexus# htop
发现资源不够,关闭虚拟机给机器添加足够的资源。启动成功后Nexus地址:http://ip:port/ 用户名:admin 密码:admin123
03.Maven 仓库介绍
代理仓库(Proxy Repository):意为第三方仓库,如:maven-central和nuget.org-proxy,其版本策略(Version Policy)分为Release(正式版本)、Snapshot(快照版本)、Mixed(混合模式),其布局策略(Layout Policy)分为Strict(严格)
、Permissive(宽松)
宿主仓库(Hosted Repository):存储本地上传的组件和资源的,如:maven-releases、maven-snapshots和nuget-hosted,其部署策略(Deployment Policy)分为Allow Redeploy(允许重新部署)、Disable Redeploy(禁止重新部署)、Read-Only(只读)
仓库组(Repository Group):通常包含了多个代理仓库和宿主仓库,在项目中只要引入仓库组就可以下载到代理仓库和宿主仓库中的包,如:maven-public和nuget-group
04.在项目中使用 Maven 私服
作用:只给开发人员使用依赖,但不提供源码
原理:图示如下
功能:以下是Nexus常用到的功能
项目配置:以下以myshop项目为例
配置认证信息:在 Maven settings.xml 中添加 Nexus 认证信息(servers 节点下):
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
Snapshots 与 Releases 的区别:nexus-releases(定义Release: 1.0.0/1.0.0-RELEASE)用于发布 Release 版本,在项目 pom.xml 中设置的版本号添加 SNAPSHOT 标识的都会发布为 SNAPSHOT 版本,没有 SNAPSHOT 标识的都会发布为 RELEASE 版本。nexus-snapshots: 用于发布 Snapshot 版本(快照版,定义Snapshot: 1.0.0-SNAPSHOT),SNAPSHOT 版本会自动加一个时间作为标识,如:1.0.0-SNAPSHOT 发布后为变成 1.0.0-SNAPSHOT-20180522.123456-1.jar
配置自动化部署:在 pom.xml 中添加如下代码,注意ID 名称必须要与 settings.xml 中 Servers 配置的 ID 名称保持一致,并且项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://127.0.0.1:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://127.0.0.1:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
部署到仓库:mvn deploy
上传第三方 JAR 包:Nexus 3.0 支持页面上传,同时也可使用如下 maven 命令,注意建议在上传第三方 JAR 包时,创建单独的第三方 JAR 包管理仓库,便于管理有维护。(maven-3rd),并且-DrepositoryId=nexus-releases 对应的是 settings.xml 中 Servers 配置的 ID 名称。(授权)
mvn deploy:deploy-file
-DgroupId=com.google.code.kaptcha
-DartifactId=kaptcha
-Dversion=2.3
-Dpackaging=jar
-Dfile=D:\kaptcha-2.3.jar
-Dur1=http://192.168.197.134:8081/repository/maven-releases/
-DrepositoryId=nexus-releases
配置代理仓库:如下
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<!-- 在maven-releases和maven-snapshots的资源都会放在maven-public中 -->
<url>http://192.168.197.134:8081/repository/maven-public/</url>
<snapshots>
<!-- 控制是否在maven-public中显示 -->
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus Plugin Repository</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
注意:快照版本(例如1.0.0)会随时变化(在私服中常以1.0.0-1进行迭代),因此开发人员不用更改pom.xml就可以随时下载到最新的依赖,而发新版本(例如1.0.0)确定后内容不可改变,在IDEA中快照配置如下