1- # 部署并使用 Nexus 作为 Maven 私服
1+ # Nexus 运维
22
3+ > Nexus 是一个强大的 Maven 仓库管理器,可以用来搭建 Maven 私服。
4+ >
35> 关键词:maven, nexus
46>
57> 部署环境
1012
1113<!-- TOC depthFrom:2 depthTo:3 -->
1214
13- - [ 下载安装 Nexus] ( #下载安装-nexus )
14- - [ 启动停止 Nexus] ( #启动停止-nexus )
15- - [ 使用 Nexus] ( #使用-nexus )
15+ - [ 安装 Nexus] ( #安装-nexus )
16+ - [ 启动/停止 Nexus] ( #启动停止-nexus )
17+ - [ 使用 Nexus 搭建 Maven 私服] ( #使用-nexus-搭建-maven-私服 )
18+ - [ 配置仓库] ( #配置仓库 )
1619 - [ 配置 settings.xml] ( #配置-settingsxml )
1720 - [ 配置 pom.xml] ( #配置-pomxml )
1821 - [ 执行 maven 构建] ( #执行-maven-构建 )
22+ - [ 将 Nexus 设置为服务] ( #将-nexus-设置为服务 )
23+ - [ Nexus 备份和迁移] ( #nexus-备份和迁移 )
24+ - [ 备份] ( #备份 )
25+ - [ 迁移] ( #迁移 )
1926- [ 参考资料] ( #参考资料 )
2027
2128<!-- /TOC -->
2229
23- ## 下载安装 Nexus
30+ ## 安装 Nexus
2431
2532进入[ 官方下载地址] ( https://www.sonatype.com/download-oss-sonatype ) ,选择合适版本下载。
2633
@@ -40,7 +47,7 @@ tar -zxf nexus-unix.tar.gz
4047- nexus-3.13.0-01 - 包含了 Nexus 运行所需要的文件。是 Nexus 运行必须的。
4148- sonatype-work - 包含了 Nexus 生成的配置文件、日志文件、仓库文件等。当我们需要备份 Nexus 的时候默认备份此目录即可。
4249
43- ## 启动停止 Nexus
50+ ## 启动/停止 Nexus
4451
4552进入 nexus-3.13.0-01/bin 目录,有一个可执行脚本 nexus。
4653
@@ -52,31 +59,47 @@ Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
5259```
5360
5461- 启动 nexus - ` ./nexus start `
55- - 停止 nexus -
62+ - 停止 nexus - ` ./nexus stop `
63+ - 重启 nexus - ` ./nexus restart `
5664
5765启动成功后,在浏览器中访问 ` http://<ip>:8081 ` ,欢迎页面如下图所示:
5866
5967<div align =" center " ><img src =" http://dunwu.test.upcdn.net/snap/20181127203131.png " /></div >
6068
6169点击右上角 Sign in 登录,默认用户名/密码为:admin/admin123。
6270
63- 有必要提一下的是,在 Nexus 的 Repositories 管理页面,展示了可用的 maven 仓库,如下图所示:
71+ ## 使用 Nexus 搭建 Maven 私服
6472
65- < div align = " center " >< img src = " http://dunwu.test.upcdn.net/snap/20181127203156.png " /></ div >
73+ ### 配置仓库
6674
67- > 说明:
68- >
69- > - maven-central - maven 中央库(如果没有配置 mirror,默认就从这里下载 jar 包),从 https://repo1.maven.org/maven2/ 获取资源
70- > - maven-releases - 存储私有仓库的发行版 jar 包
71- > - maven-snapshots - 存储私有仓库的快照版(调试版本) jar 包
72- > - maven-public - 私有仓库的公共空间,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 中使用。
75+ Nexus 中的仓库有以下类型:
7376
74- ## 使用 Nexus
77+ - ` hosted ` - 宿主仓库。主要用于部署无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)以及自己或第三方的项目构件;
78+ - ` proxy ` - 代理仓库。代理公共的远程仓库;
79+ - ` virtual ` - 虚拟仓库。用于适配 Maven 1;
80+ - ` group ` - 仓库组。Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。
7581
76- 如果要使用 Nexus,还必须在 settings.xml 和 pom.xml 中配置认证信息。
82+ <div align =" center " ><img src =" https://gitee.com/turnon/images/raw/master/images/java/javalib/maven/nexus.png " /></div >
83+
84+ > ** 最佳实践**
85+ >
86+ > 建议配置如下:
87+ >
88+ > - hosted 仓库
89+ > - maven-releases - 存储私有仓库的发行版 jar 包
90+ > - maven-snapshots - 存储私有仓库的快照版(调试版本) jar 包
91+ > - proxy 仓库
92+ > - maven-central - maven 中央库(如果没有配置 mirror,默认就从这里下载 jar 包),从 https://repo1.maven.org/maven2/ 获取资源
93+ > - maven-aliyun - 国内 maven 仓库,提高访问速度。
94+ > - group 仓库
95+ > - maven-public - 私有仓库的公共空间,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 中使用。
96+
97+ <div align =" center " ><img src =" http://dunwu.test.upcdn.net/snap/20181127203156.png " /></div >
7798
7899### 配置 settings.xml
79100
101+ 如果要使用 Nexus,还必须在 settings.xml 和 pom.xml 中配置认证信息。
102+
80103一份完整的 ` settings.xml ` :
81104
82105``` xml
@@ -186,8 +209,59 @@ $ mvn clean package -Dmaven.skip.test=true -P zp
186209$ mvn clean deploy -Dmaven.skip.test=true -P zp
187210```
188211
212+ ## 将 Nexus 设置为服务
213+
214+ 将 Nexus 添加为服务,以便开机自启动。
215+
216+ 在 ` /lib/systemd/system ` 目录下创建 ` nexus.service ` 文件,内容如下:
217+
218+ ``` ini
219+ [Unit]
220+ Description =nexus
221+ After =network.target
222+
223+ [Service]
224+ Type =forking
225+ LimitNOFILE =65536 # 警告处理
226+ Environment =RUN_AS_USER =root
227+ ExecStart =/opt/maven/nexus-3.13.0-01/bin/nexus start
228+ ExecReload =/opt/maven/nexus-3.13.0-01/bin/nexus restart
229+ ExecStop =/opt/maven/nexus-3.13.0-01/bin/nexus stop
230+ Restart =on-failure
231+ PrivateTmp =true
232+
233+ [Install]
234+ WantedBy =multi-user.target
235+ ```
236+
237+ 保存后,可以使用以下命令应用 nexus 服务:
238+
239+ - ` systemctl enable nexus ` - 启动 nexus 开机启动
240+ - ` systemctl disable nexus ` - 关闭 nexus 开机启动
241+ - ` systemctl start nexus ` - 启动 nexus 服务
242+ - ` systemctl stop nexus ` - 停止 nexus 服务
243+ - ` systemctl restart nexus ` - 重启 nexus 服务
244+
245+ ## Nexus 备份和迁移
246+
247+ Nexus 三个重要目录:
248+
249+ | 名称 | 目录名 | 重要配置文件 |
250+ | :----------------- | :------------- | :------------------------------------------------ |
251+ | nexus 主目录 | nexus-2.6.4-02 | conf/nexus.properties 里面有 sonatype-work 的地址 |
252+ | sonatype-work 目录 | sonatype-work | nexus/conf/nexus.xml 里面有 storage 的地址 |
253+ | storage 目录 | storage | 里面主要是各种程序的 jar 包等 |
254+
255+ ### 备份
256+
257+ Nexus 的数据都存储在 sonatype-work 目录,备份 Nexus 数据只需要将其打包即可。
258+
259+ ### 迁移
260+
261+ 将原 Nexus 服务器中的 sonatype-work 目录迁移到新 Nexus 服务器的 sonatype-work 目录下。
262+
189263## 参考资料
190264
191- - https://www.cnblogs.com/hoobey/p/6102382.html
192- - https://blog.csdn.net/wzygis/article/details/49276779
193- - https ://blog.csdn .net/clj198606061111/article/details/52200928
265+ - [ maven 私库 nexus3 安装及使用 ] ( https://blog.csdn.net/clj198606061111/article/details/52200928 )
266+ - [ Nexus 安装 使用说明 ] ( https://www.cnblogs.com/jtlgb/p/7473837.html )
267+ - [ 企业级开源仓库 nexus3 实战应用–使用 nexus3 配置 yum 私有仓库 ] ( http ://www.eryajf .net/2002.html )
0 commit comments