私服概述
- 私服是一种特殊的远程仓库,它一般假设在公司局域网内。当
Maven
下载构件时从私服上取,如果不存在则私服从远程仓库中取缓存在私服上,方便后续人员取用。它的优点如下:- 节省外网宽带,构件高效。
- 有利于在中央仓库不存在的第三方构件和自己生成的构件。
- 加速构建过程,提供稳定性以及降低中央仓库的负荷等。
私服搭建
-
选择
Ubuntu 18.04
平台,openjdk-8
以及使用Nexus OSS 3.13.0
版本库管理工具, 搭建过程如下:#1 下载nexus-3.13.0-01-unix.tar.gz文件至家目录下 官网下载速度奇慢,使用Baidu网盘下载 #2 解压缩至/opt/目录下, yjf@v-box:~$ sudo tar -zxvf nexus-3.13.0-01-unix.tar.gz -C /opt/ #3 进入/opt/nexus-3.13.0-01/bin目录,执行以下命令运行nexus yjf@v-box:/opt/nexus-3.12.0-01/bin$ sudo ./nexus start #4 nexus的常用命令 #4.1 start 开启 #4.2 stop 停止 #4.3 status 状态
-
成功启动后,使用
http://192.168.0.210:8081/#browse/welcome
进行访问,进行配置需要登录,默认的用户名和密码为admin:admin123
如下图: -
Nexus
仓库的分类:- Nexus宿主仓库,主要用于管理第三方构件,分为RELEASE和SNAPSHOT版本。
- 代理仓库,主要代理远程中央仓库,在本私服上不存在指定构件时从中央仓库上直接下载。
- 仓库组:可以聚合以上两种仓库形成列表,作为仓库供
maven
配置下载,仓库便利的顺序按配置从上到下遍历。
-
Nexus宿主仓库建立
-
点击上图的配置按钮,选择
Repositories
连接,并点击Create Repository
,在弹出的选择列表中选择maven2(hosted)
即可进行创建,配置如下:
-
-
Nexus代理仓库建立
-
步骤同上,在点击
Create Repository
,在弹出的选择列表中选择maven2(proxy)
即可进行创建,配置如下:
-
-
Nexus 仓库组建立
-
步骤同上,在点击
Create Repository
,在弹出的选择列表中选择maven2(group)
即可进行创建,配置如下:
-
Maven配置私服
-
拉取构件,主要分在项目中配置和setting.xml中配置两种方式。
<!-- 项目中配置,只针对此项目有效 1 未配置任何仓库的情况下,由于超级POM的作用,默认配置中央仓库。 2 在项目中覆盖配置中央仓库,配置如下: 该方式配置与超级POM中的中央仓库合并 首先在该仓库中找 找不到则在中央仓库中找。 --> <repositories> <repository> <id>jboss</id> <name>JBoss Repository</name> <url>http://192.168.0.210:8081/repository/jboss-central/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> <layout>default</layout> </repository> </repositories> <!-- 3 在setting.xml中配置profile, 覆盖超级POM中的中央仓库和插件仓库的配置 --> <profiles> <profile> <id>nexusProfile</id> <repositories> <repository> <id>central</id> <name>Central Repository</name> <url>http://192.168.0.210:8081/repository/maven-public/</url> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>Central Repository</name> <url>http://192.168.0.210:8081/repository/maven-public/</url> <snapshots><enabled>false</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexusProfile</activeProfile> </activeProfiles>
-
推送构件到私服仓库,需要配置如下:
<!-- setting.xml 配置如下:--> <servers> <server> <id>nexus-server</id> <username>admin</username> <password>admin123</password> </server> </servers> <distributionManagement> <snapshotRepository> <id>nexus-server</id> <name>snapshots-hosted</name> <url>http://192.168.0.210:8081/repository/snapshot-hosted/</url> </snapshotRepository> <!-- 推送正式版本时请务必在pom.xml文件中版本号中删除SNAPSHOT字样 否则产生400错误 --> <repository> <id>nexus-server</id> <name>release-hosted</name> <url>http://192.168.0.210:8081/repository/release-hosted/</url> </repository> </distributionManagement>
网友评论