搭建nexus私服总结
为什么搭建私有仓库
- 提高下载ar包或者plugin的速度,因为不用每次从远程的中央仓库或镜像网站拉取jar包,如果私有仓库有的话直接从私有仓库拉取,没有maven会从中央仓库缓存到私有仓库供下次使用;
- 可以安装中央仓库没有的jar包或plugin供用户使用,如第三方sdk包或者本公司的二方库等;
安装时遇到的问题
下载nexus
- sonatype官网需要翻墙才能下载
启动nexus
- 在windos中启动neuxs私服是必须使用win自带的命令行工具cmd或者powershell都可以,使用git bash这样的第三方工具会无任何响应
- 将nexus服务安装成一个windows服务时,必须将使用的命令行工具用管理员身份打开,否则会报错。
nexus配置
- nexus无需做过多的配置,简单的配置在主目录下的etc中,如修改端口号等;
- 启动后工作的目录为解压缩时产生的sonatype-work目录,该目录下保存工作时的数据如日志jar包仓库等;
nexus的使用
1. 第一次启动的默认密码及如何修改
第一次启动后的nexus默认的用户名是admin,密码在sonatype-work中的一个password文件中保存,第一次登录后需修改密码;
2. 仓库分类
三种仓库分类:
- hosted 宿主仓库:主要用于部署无法从公共仓库获取的包如自己公司的二方库或第三方的sdk等
- proxy 代理仓库:可以代理公共的远程仓库
- group 逻辑组仓库:逻辑上的仓库,可以管理多个仓库直接请求该仓库组就可以获取他其他仓库的构建,要注意的是添加其他仓库的顺序也是查询索引的顺序
3. Maven中的配置
参考链接:配置Maven使其能从Nexus下载jar包等组件
组件包括jar包和plugin等,从参考文档知得知,需要在pom中配置repositories和pluginRepositories才会从nexus中下载,pluginRepositories是下载插件包,repositories是下载jar包,如下所示:
<repositories>
<repository>
<id>sxtx-nexus</id>
<name>sxtx-nexus-public</name>
<url>http://localhost:8888/groups/icetea-public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sxtx-nexus</id>
<name>sxtx-nexus-public</name>
<url>http://localhost:8888/groups/icetea-public</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
4. 使用deploy插件上传本地包到nexus
deploy插件是将项目打包成jar包上传到nexus中,与install不同install只会打包到本地仓库中。使用deploy必须在maven的setting文件中配置对应server的id、username、password,然后在pom文件中配置相应的仓库,如下所示:
<!--setting.xml文件中配置的server-->
<servers>
<server>
<id>icetea-nexus</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
<!--POM 文件中配置deploy仓库地址,id必须与setting中的server id一致-->
<distributionManagement>
<repository>
<id>sxtx-nexus</id>
<name>Sxtx Nexus Release Repository</name>
<url>http://localhost:8888/repositories/icetea-releases/</url>
</repository>
<snapshotRepository>
<id>sxtx-nexus</id>
<name>Sxtx Nexus Snapshot Repository</name>
<url>http://localhost:8888/repositories/icetea-snapshots/</url>
</snapshotRepository>
</distributionManagement>