什么是 Nexus ?
Nexus
是一个强大的仓库管理器,极大地简化了内部仓库的维护和外部仓库的访问。
2016 年 4 月 6 日 Nexus 3.0
版本发布,相较 2.x 版本有了很大的改变:
- 对低层代码进行了大规模重构,提升性能,增加可扩展性以及改善用户体验。
- 升级界面,极大的简化了用户界面的操作和管理。
- 提供新的安装包,让部署更加简单。
- 增加对
Docker
,NeGet
,npm
,Bower
的支持。 - 提供新的管理接口,以及增强对自动任务的管理。
安装
创建 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
启动时如果出现权限问题,赋予数据卷目录可读可写的权限。首先关闭docker-compose
docker-compose down ## 关闭
chmod 777 /usr/local/docker/nexus/data ## 赋予权限
docker-compose up -d ## 启动
注意我使用的是3.19版本之后的Nexus
默认的账号 admin
密码 admin123
是错误的,需要自己查看nexus
给的密码
在 服务器 /usr/local/docker/nexus/data
中
访问服务器的8081端口,查看密码,使用交互式的方法进入容器
docker exec -it nexus /bin/bash
cd /usr/local/docker/nexus/data
cat admin.password
登陆以后会修改一次密码
在项目中使用 Maven 私服
在’maven’的 setting.xml 新修改配置
<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>
</servers>
Snapshots
与 Releases
的区别
nexus-releases
: 用于发布Release
版本release
版本不允许发布相同版本号的同一jar
nexus-snapshots
: 用于发布Snapshot
版本(快照版)snapshot
允许发布相同版本号的jar
,记得去idea
设置使用最新的snapshot
版本的依赖
在开发项目的pom.xml
添加自动化部署
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.0.108:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.0.108:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意事项:
ID
名称必须要与settings.xml
中<Servers>
配置的<ID>
名称保持一致。- 项目版本号中有
SNAPSHOT
标识的,会发布到Nexus Snapshots Repository
, 否则发布到Nexus Release Repository
,并根据<ID>
去匹配授权账号。 - 发现快照版本
pom
版本号后面必须有-SNAPSHOT
,不然会发布到releases
仓库中
通过指令发布jar
调过测试
mvn deploy -Dmaven.test.skip=true
Nexus
3.0不支持页面上传,可使用 maven
命令:
mvn deploy:deploy-file
-DgroupId=com.aliyun.oss
-DartifactId=aliyun-sdk-oss
-Dversion=2.2.3
-Dpackaging=jar
-Dfile=....jar
-Durl=http://192.168.0.108:8081/repository/maven-releases/
-DrepositoryId=nexus-releases
注意事项:
- 建议在上传第三方
jar
包时,创建单独的第三方jar
包管理仓库,便于管理有维护。 -DrepositoryId=nexus-releases
对应的是settings.xml
中Servers
配置的ID
名称。
配置代理仓库
pom.xml
配置
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://192.168.0.108:8081/repository/maven-public/</url>
<snapshots>
<!--是否允许依赖快照版本-->
<enabled>true</enabled>
</snapshots>
<releases>
<!--是否允许releases快照版本-->
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<!--插件仓库-->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus Plugin Repository</name>
<url>http://192.168.0.108:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
总结
一方库:自己的代码打包成依赖
二方库:公司里同事的代码打包成依赖
三方库:maven
上别人的依赖
代码仓库有了,我们也得有一个依赖仓库,这样二方库就可以被员工使用了,且不需要知道我们的代码是如何写的。(这里可以看一下别人对于一方库二方库的理解,大概意思就是,拿了别人的工具类用)。