系列文章
1、SSM基础专项复习1——SSM项目整合-CSDN博客
2、SSM基础专项复习2——Spring 框架(1)-CSDN博客
3、SSM基础专项复习3——Spring框架(2)-CSDN博客
4、SSM基础专项复习4——Maven项目管理工具(1)-CSDN博客
文章目录
系列文章
1、maven 私服介绍
1.1 私服介绍
1.2. Nexus 介绍
2、maven 私服实战
2.1 nexus 安装
2.2 nexus 仓库类型
2.3 将项目发布到私服
2.4. 从私服下载 jar 包
1、maven 私服介绍
1.1 私服介绍
正式开发,不同的项目组开发不同的工程。maven-dao 工程开发完毕,发布到私服maven-service 从私服下载 dao。
公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库, 每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。
私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载。
1.2. Nexus 介绍
Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 maven 仓库,同时nexus 还提供强大的仓库管理功能,构件搜索功能等。
2、maven 私服实战
2.1 nexus 安装
这个是nexus官网
Sonatype Nexus Repository Manager Community Edition | Downloadhttps://www.sonatype.com/download-nexus-repo-oss?submissionGuid=e225567e-4323-4049-870c-478afc2a7bfe
解压 nexus-2.12.0-01-bundle.zip 文件,存放到一个不含中文的目录下。查看 conf 文件下的 nexus.properties 配置文件,可以修改对应的配置:
nexus 的 安 装 命 令 : 使 用 管 理 员 运 行 cmd 命令窗口,切换目录nexus\nexus-2.12.0-01\bin 目录下,执行 nexus.bat install 进行安装。执行nexus.bat start 启动服务 执行 nexus.bat stop 停止服务。
nexus 的 卸 载 命 令 : 使 用 管 理 员 运 行 cmd 命 令窗口,切换目录nexus\nexus-2.12.0-01\bin 目录下,执行 nexus.bat uninstall 进行卸载
访问图形化界面:打开浏览器,输入http://localhost:端口号/nexus 访问
点击 log in,进行登录。用户名:admin 密码:admin123
2.2 nexus 仓库类型
nexus 提供了不同的仓库类型
- hosted,宿主仓库, 部署自己的 jar 到这个类型的仓库,包括releases 和snapshot 两部分, Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
- proxy,代理仓库, 用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
- group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的maven连接仓库组。Group 仓库组也是可以自己进行定制的。
- virtual(虚拟),兼容 Maven1 版本的 jar 或者插件
2.3 将项目发布到私服
需要在客户端即部署要部署的工程电脑上配置 maven 环境,并修改settings.xml 文件, 配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。
在 servers 节点下进行配置
<!-- 定义稳定版本的 id 名称,用户名密码 -->
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!-- 定义开发版本的 id 名称,用户名密码 -->
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
配置项目 pom.xml,配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release 则上传到私服的 release 仓库,如果版本为 snapshot 则上传到私服的 snapshot 仓库。
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8079/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8079/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:这里的 id 标签的值要和 settings.xml 配置文件中的id 值保持一致。
在该工程中执行 deploy 命令,发布项目到私服上。
查看私服结果
可以发布 RELEASES 稳定版本的 jar 包到私服。
2.4. 从私服下载 jar 包
在 settings.xml 配置文件配置私服的镜像文件
<mirror>
<!-- id 名称 -->
<id>nexusmaven</id>
<!-- 表示拦截所有的请求,都重定向到私服,从私服下载jar 包,私服没有再去中央仓库下载 -->
<mirrorOf>*</mirrorOf>
<name>nexus maven</name>
<!-- 私服的组地址 -->
<url>http://localhost:8079/nexus/content/groups/public/</url>
</mirror>
进行测试
先把自己的某个项目发布到私服中,然后删除掉本地仓库中的jar 包,再使用其他项目去依赖该 jar 包,查看是否从私服中下载。
在其他项目中引入该坐标依赖。
<!--依赖 demo1 这个项目-->
<dependencies>
<dependency>
<groupId>cn.tx.maven</groupId>
<artifactId>txmaven_demo10413</artifactId>
<version>1.0-RELEASES</version>
</dependency>
</dependencies>
还有一种方式
可以配置仓库的方式,可以修改自己项目的 pom 配置文件,添加仓库的配置。
<repositories>
<repository>
<id>nexus</id>
<name>nexusmaven</name>
<url>http://localhost:8079/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<url>http://localhost:8079/nexus/content/groups/public/</url>
<name>pluginRepositories</name>
</pluginRepository>
</pluginRepositories>
上面的方式不是特别的理想,需要在每一个项目的 pom 文件中都添加相同的配置,比较麻烦。
可以在 settings.xml 配置文件中添加配置,完成统一的设置。
<!-- 下载 jar 包配置 -->
<profile>
<!--profile 的 id -->
<id>dev</id>
<repositories>
<repository> <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复 -->
<id>nexus</id> <!--仓库地址,即 nexus 仓库组的地址--><url>http://localhost:8079/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:8079/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
激活配置
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
进行测试