概念
公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库,每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自 己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。
架构
私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载,如下图:
仓库类型
nexus 的仓库有 4 种类型:
1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部 分,Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库。
2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。
4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件。
私服服务器目录
- central:代理仓库,代理中央仓库
- apache-snapshots:代理仓库,存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/
- central-m1:virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件
- releases:本地仓库,存储 releases 构件。
- snapshots:本地仓库,存储 snapshots 构件。
- thirdparty:第三方仓库
- public:仓库组
向私服发布项目
企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队 或模块开发人员使用。如下图,
配置
第一步:
修改上传模块到私服的电脑 maven 的 settings.xml 配置文件,配置用户名和密码,用于登录私服,如下,
- releases 连接发布版本项目仓库
- snapshots 连接测试版本项目仓库
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
配置私服仓库
<profile>
<!--profile 的 id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
<id>nexus</id>
<!--仓库地址,即 nexus 仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载 releases 构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载 snapshots 构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
激活定义的仓库
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
第二步:
配置项目 pom.xml
配置私服仓库的地址,自己在公司开发的模块可以打成 jar 包上传到公司私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为 snapshot 则上传到私服的 snapshot 仓库,如下,
pom.xml 这里<id> 和 settings.xml 配置 <id> 对应
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
第三步:
打包
对要上传工程执行 mvn deploy
根据本项目 pom.xml 中 version 定义决定发布到哪个仓库,如果 version 定义为 snapshot, 执行 deploy 后查看 nexus 的 snapshot 仓库,如果 version 定义为 release 则项目将发布到 nexus 的 release 仓库,本项目将发布到 snapshot 仓库。
下载
没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包, 这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度,项目连接私服下 jar 包的速度要比项目连接中央仓库的速度快的多。
第三方jar包导入私服
需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息,如下,
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
以fastjson为例,
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37
-Dpackaging=jar -Dfile=fastjson-1.1.37.jar
-Durl=http://localhost:8081/nexus/content/repositories/thirdparty/
-DrepositoryId=thirdparty
参数说明:
DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。
Dfile 表示需要上传的 jar 包的绝对路径。
Durl 私服上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。
DrepositoryId 服务器的表示 id,在 nexus 的 configuration 可以看到。
Dversion 表示版本信息,
关于 jar 包准确的版本:包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件,这个文件就有描述该包的版本信息。
比如 Specification-Version: 2.2 可以知道该包的版本了。 上传成功后,在 nexus 界面点击 3rd party 仓库可以看到这包。
总结:
在项目的 pom.xml 添加一个依赖,此依赖在本地仓库和私服都不存在,maven 会先从本地仓库找,本地仓库没有再从私服找,私服没有再去中央仓库下载,jar 包下载成功在私服、 本地仓库分别存储一份。