Nexus的安装和使用
基于Docker安装Nexus
因为Nexus的安装比较复杂所以基于docker安装更为简单
1.在安装了docke的虚拟机(或者是主机)上按照如下的路径创建相应的文件夹
/usr/local/docker/nexus/
2.在nexus该文件夹下面创建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
3.在运行后docker-compose up后在docker-compose.yml的文件夹目录下会有配置生成的一个data文件夹,这里需要给data文件夹赋与可读可写可执行的权限
chmod 777 data/
4.再次运行dicker-compose up命令启动等待一会就可以根据hostname和端口号(这里时8081)访问到Nexus的界面
Nexus的使用
1.看到了nexus后第一步就是登录,以下是第一次登录时后的默认用户名和密码
admin
admin123
2.登录后可以点击左侧的user进行用户名和密码的修改
3.要使用私服nexus首先,配置认证信息
在 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>
注意: 这里的用户名和密码要和你的nexus私服的用户名密码相互对应
4.要在项目中使用Nexus私服仓库需要在依赖管理中或者单独的依赖中(也就是pom.xml中)加入以下的依赖
<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>
注意:
- 这里的id需要和setting中配置的相同否则无法使用。
- 这里的url是你的私服中对应快照或发行版中的url,是用来上传下载的
- 项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。
5.在idea的控制台使用以下的命令可以将依赖推到自己的私服仓库中
mvn deploy
6.当然这里只是将依赖推送到私服仓库,如果需要下载私服中的依赖还需要在总pom.xml中配置以下代理仓库
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<snapshots>
<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>
同理
- 以上的url也是相对应的私服中快照和发行版的url
7.如果有需要自行上传的jar包,可以用cmd命令窗口用以下的命令来进行上传(相应的路径必须改成jar包所在的位置,url也要改成相应的私服上传位置的url)
mvn deploy:deploy-file
-DgroupId=com.aliyun.oss
-DartifactId=aliyun-sdk-oss
-Dversion=2.2.3
-Dpackaging=jar
-Dfile=D:\aliyun-sdk-oss-2.2.3.jar
-Durl=http://127.0.0.1:8081/repository/maven-3rd/
-DrepositoryId=nexus-releases
8.如果需要在项目中使用还需要进行以下的设置,在idea中点击File -> setting -> 搜索maven -> 点击maven 在将右侧的Always update snapshots勾上
注意: 有了私服过后下载会先从本地仓库寻找依赖,如果没有就会到私服中去寻找,还没有就会到官方服务器中去寻找,这样就当公司有人员流动时候需要开启项目就不用在去官方下载而是直接从私服中下载就可以,这样就节约了大量的时间。当然推送到私服中也是一样的.