SSM基础专项复习5——Maven私服搭建(2)

系列文章

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、maven 私服介绍)

[1.1 私服介绍](#1.1 私服介绍)

[1.2. Nexus 介绍](#1.2. Nexus 介绍)

[2、maven 私服实战](#2、maven 私服实战)

[2.1 nexus 安装](#2.1 nexus 安装)

[2.2 nexus 仓库类型](#2.2 nexus 仓库类型)

[2.3 将项目发布到私服](#2.3 将项目发布到私服)

[2.4. 从私服下载 jar 包](#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 提供了不同的仓库类型

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

2.3 将项目发布到私服

需要在客户端即部署要部署的工程电脑上配置 maven 环境,并修改settings.xml 文件, 配置连接私服的用户和密码 。

此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。

在 servers 节点下进行配置

XML 复制代码
<!-- 定义稳定版本的 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 仓库。

XML 复制代码
<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 配置文件配置私服的镜像文件

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 包,查看是否从私服中下载。

在其他项目中引入该坐标依赖。

XML 复制代码
<!--依赖 demo1 这个项目-->
<dependencies>
<dependency>
<groupId>cn.tx.maven</groupId>
<artifactId>txmaven_demo10413</artifactId>
<version>1.0-RELEASES</version>
</dependency>
</dependencies>

还有一种方式

可以配置仓库的方式,可以修改自己项目的 pom 配置文件,添加仓库的配置。

XML 复制代码
<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 配置文件中添加配置,完成统一的设置。

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>

激活配置

XML 复制代码
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>

进行测试

相关推荐
小学生闹钟1 分钟前
Java面向对象高级-继承、多态
java
恩师小迪2 分钟前
Apache Shiro反序列化漏洞深度剖析:从原理到利用
java·开发语言·python
结衣结衣.15 分钟前
【Qt】QWidget属性介绍
开发语言·c++·qt·c++11
m0_6640470220 分钟前
DeepSeek:为教培小程序赋能,引领行业变革新潮流
java·微信小程序·小程序·小程序开发·心理测评小程序
月临水20 分钟前
SpringCloud 学习笔记1(Spring概述、工程搭建、注册中心、负载均衡、 SpringCloud LoadBalancer)
学习·spring·spring cloud
原子一式26 分钟前
Idea运行项目报错:java.lang.OutOfMemoryError: Java heap space 解决方法
java·ide·intellij-idea
noravinsc27 分钟前
centos升级 java
java·python·centos
※※冰馨※※31 分钟前
【Python】PyQt5在PyCharm的配置与应用
开发语言·windows·python
csdn_aspnet44 分钟前
Windows 上安装配置 Maven
java·maven