Maven专题(五)-maven私服

1.引言

公司在自己的局域网内搭建自己的远程仓库服务器,称为私服, 私服服务器即是公司内部的 maven 远程仓库, 每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。

私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载,如下图:
在这里插入图片描述

2. 搭建私服环境

3.1 下载 nexus

Nexus 是 Maven仓库管理器, 通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。
下载 Nexus, 下载地址: http://www.sonatype.org/nexus/archived/

在这里插入图片描述
下载: nexus-2.12.0-01-bundle.zip

3.2 安装 nexus

解压 nexus-2.12.0-01-bundle.zip,本教程将它解压在 F 盘,进入 bin 目录:
在这里插入图片描述
cmd 进入 bin 目录,执行 nexus.bat install
在这里插入图片描述
安装成功在服务中查看有 nexus 服务
在这里插入图片描述

3.3 卸载 nexus

cmd 进入 nexus 的 bin 目录,执行: nexus.bat uninstall
在这里插入图片描述
查看 window 服务列表 nexus 已被删除。

4.启动 nexus

方法 1:
cmd 进入 bin 目录,执行 nexus.bat start
在这里插入图片描述
方法 2:
直接启动 nexus 服务
在这里插入图片描述
查看 nexus 的配置文件 conf/nexus.properties

# Jetty section

# nexus 的访问端口配置
application-port=8081
# nexus 主机监听配置(不用修改)
application-host=0.0.0.0
# nexus 工程目录
nexus-webapp=${bundleBasedir}/nexus
# nexus 的 web 访问路径
nexus-webapp-context-path=/nexus

# Nexus section
# nexus 仓库目录
nexus-work=${bundleBasedir}/../sonatype-work/nexus
# nexus 运行程序目录
runtime=${bundleBasedir}/nexus/WEB-INF

访问:

在这里插入图片描述使用 Nexus 内置账户 admin/admin123 登陆:
点击右上角的 Log in,输入账号和密码 登陆
在这里插入图片描述
登陆成功:
在这里插入图片描述

3.5 仓库类型

nexus
查看 nexus 的仓库
在这里插入图片描述nexus 的仓库有 4 种类型:
在这里插入图片描述

1. hosted,宿主仓库, 部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分, Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
2. proxy,代理仓库, 用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。
4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件

nexus 仓库默认在 sonatype-work 目录中:
在这里插入图片描述
 central:代理仓库, 代理中央仓库

在这里插入图片描述
 apache-snapshots:代理仓库
存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/
在这里插入图片描述
 central-m1: virtual 类型仓库, 兼容 Maven1 版本的 jar 或者插件
 releases: 本地仓库,存储 releases 构件。
 snapshots: 本地仓库,存储 snapshots 构件。
 thirdparty:第三方仓库
 public:仓库组

4. 将项目发布到私服

4.1 需求

企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队或模块开发人员使用。

本例子假设多团队分别开发 ssm_dao、 ssm_service、 ssm_web,某个团队开发完在ssm_dao会将 ssm_dao发布到私服供 ssm_service 团队使用,本例子会将 ssm_dao 工程打成jar 包发布到私服。
在这里插入图片描述

4.2 配置

第一步: 需要在客户端即部署 ssm_dao 工程的电脑上配置 maven环境,并修改 settings.xml
文件, 配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。

 <server>
		<id>releases</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
	
	<server>
		<id>snapshots</id>
		<username>admin</username>
		<password>admin123</password>
</server>

releases 连接发布版本项目仓库
snapshots 连接测试版本项目仓库
在这里插入图片描述
第二步: 配置项目 pom.xml
配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号
决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为
snapshot 则上传到私服的 snapshot 仓库

<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>

注意: pom.xml 这里 和 settings.xml 配置 对应!

4.3 测试

将项目 dao 工程打成 jar 包发布到私服:
1、 首先启动 nexus
2、 对 ssm_dao 工程执行 deploy 命令
在这里插入图片描述
据本项目 pom.xml中 version定义决定发布到哪个仓库,如果 version定义为snapshot,
执行 deploy后查看 nexus 的 snapshot仓库,如果 version定义为 release则项目将发布到 nexus
的 release 仓库,本项目将发布到 snapshot 仓库:
在这里插入图片描述
也可以通过 http 方式查看:
在这里插入图片描述

5. 从私服下载 jar 包

5.1 需求

没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器, 有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度, 项目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多。

5.2 管理仓库组

nexus中包括很多仓库, hosted中存放的是企业自己发布的 jar包及第三方公司的jar包,proxy 中存放的是中央仓库的 jar,为了方便从私服下载 jar 包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载 jar 包。
打开 nexus 配置仓库组,如下图:
在这里插入图片描述

上图中仓库组包括了本地仓库、代理仓库等。

5.3 在 setting.xml 中配置仓库

在客户端的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置标签需要使用 profile 定义仓库。

<!-- 下载jar包配置 -->
<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>

使用 profile 定义仓库需要激活才可生效。

 <activeProfiles>
     <activeProfile>dev</activeProfile>
 </activeProfiles>

5.4 测试从私服下载 jar包

测试:需要互联网环境
在项目的 pom.xml 添加一个依赖,此依赖在本地仓库和私服都不存在, maven 会先从本
地仓库找,本地仓库没有再从私服找,私服没有再去中央仓库下载, jar 包下载成功在私服、
本地仓库分别存储一份。
在这里插入图片描述
在这里插入图片描述

发布了274 篇原创文章 · 获赞 80 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/BruceLiu_code/article/details/103986605