pom管理规范

  1. 引言

在单机架构下,我们只需要将我们的依赖在pom中引入。但是过渡到微服务架构后,会涉及到多模块引用相同的依赖,多模版之间依赖的版本太过分散难以管理的问题。

这就需要我们利用maven中依赖传递的特性,结合dependencyManagement标签来做好依赖的版本管理。下面我们就通过具体的案例来向大家演示如何在微服务架构中做好pom的管理规范。

  1. 在父项目中实现版本管理

我们介绍的第一种版本管理的方案,就是在父项目中声明版本。

我们先来看我们的案例:

有一个微服务架构,包含商品模块product,订单模块order,网关模块gateway,还有一个公用模块commons。

其中商品模块、订单模块都需要引入nacos、spring web、seata、mybatis-plus、swagger、mysql、lombok依赖

订单模块中除了上述所说的依赖,还需要引入dynamic-datasource、openfeign依赖

网关模块需要引入nacos discovery、gateway、jwt、swagger、lombok依赖

1.1 在父项目中声明依赖版本

​​以下配置在父项目pom.xml中操作​​ 首先我们先将所需要的依赖的版本号定义为属性

<properties>

<java.version>1.8</java.version>

<maven.version>3.8.1</maven.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<spring-boot.version>2.3.7.RELEASE</spring-boot.version>

<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>

<seata-version>1.4.0</seata-version>

<swagger.version>2.9.2</swagger.version>

<mybatis-plus.version>3.4.2</mybatis-plus.version>

</properties>

其次我们在​​dependencyManagement​​标签中将所有的依赖做好版本声明

如果不知道这些标签的作用以及用法的,可以先看专栏的上一篇博客:

​springcloud:maven快速上手 | maven常用标签(十三)​​

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-dependencies</artifactId>

<version>${spring-boot.version}</version>

<type>pom</type>

<scope>import</scope>

</dependency>

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-alibaba-dependencies</artifactId>

<version>${spring-cloud-alibaba.version}</version>

<type>pom</type>

<scope>import</scope>

</dependency>

<dependency>

<groupId>io.seata</groupId>

<artifactId>seata-spring-boot-starter</artifactId>

<version>${seata-version}</version>

</dependency>

<dependency>

<groupId>com.alibaba.nacos</groupId>

<artifactId>nacos-client</artifactId>

<version>${seata-version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>${swagger.version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>${swagger.version}</version>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>${mybatis-plus.version}</version>

</dependency>

</dependencies>

</dependencyManagement>

这里咱们要注意的是,​​spring-boot-dependencies​​​和​​spring-cloud-alibaba-dependencies​​这两个依赖实际上并不是jar包,我们点击进去就可以知道,这是一个聚合项目,里面声明了常用的依赖的版本

我们通过引用这个线程的聚合项目,节省了大量常用依赖的版本管理

其次我们再在父项目中声明一下项目的jdk、maven版本以及编码格式。如果项目中有通用的maven插件配置,也可以在这里声明

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>${maven.version}</version>

<configuration>

<source>${java.version}</source>

<target>${java.version}</target>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

</plugins>

</build>

1.2 服务的公用依赖提取到commons模块中

​​以下配置在commons模块的pom.xml中操作​​ 基于这样的一个案例,我们首先知道的是商品模块和订单模块中都有很多相同的依赖,那么我们把这些相同的依赖添加到公用模块中,如果没有公用模块,可以创建一个,专用于存储公用的实体类、工具类、公共依赖登

首先我们需要声明父项目,以此使用父项目中声明的依赖版本

<parent>

<artifactId>springcloud2</artifactId>

<groupId>com.example</groupId>

<version>1.0.0</version>

</parent>

其次将公用的依赖声明出来,这里会发现我们这里的依赖是没有声明版本的,这是因为我们已经在父项目中将版本声明好了。这也就是我们要做版本管理的意义所在

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</dependency>

<dependency>

<groupId>io.seata</groupId>

<artifactId>seata-spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba.nacos</groupId>

<artifactId>nacos-client</artifactId>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

</dependencies>

1.3 商品、订单模块中引入commons模块

1、然后在商品模块和订单模块引入commons模块

<parent>

<artifactId>springcloud2</artifactId>

<groupId>com.example</groupId>

<version>1.0.0</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>product-server-feign</artifactId>

<version>${parent.version}</version>

<name>product-server-feign</name>

<description>product-server-feign</description>

<dependencies>

<dependency>

<groupId>com.example</groupId>

<artifactId>commons</artifactId>

<version>1.0.0</version>

</dependency>

</dependencies>

后续就可以只在commons中添加或者修改依赖,就能实现各个模块的公用依赖统一管理

需要注意的是:​​子项目中一定要用parent标签声明好父项目​​ 2、因为订单模块中还多了其他几个依赖,所以我们除了commons外还要在订单的pom中额外引入其他依赖

<parent>

<artifactId>springcloud2</artifactId>

<groupId>com.example</groupId>

<version>1.0.0</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>order-server-feign</artifactId>

<version>${parent.version}</version>

<name>${project.artifactId}</name>

<dependencies>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>dynamic-datasource-spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

<dependency>

<groupId>io.github.openfeign</groupId>

<artifactId>feign-httpclient</artifactId>

</dependency>

<dependency>

<groupId>com.example</groupId>

<artifactId>product-server-feign</artifactId>

<version>${parent.version}</version>

</dependency>

</dependencies>

3、commons中的公用依赖大部分网关模块都不需要,所以我们干脆就网关模块的依赖单独引入

<parent>

<artifactId>springcloud2</artifactId>

<groupId>com.example</groupId>

<version>1.0.0</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>gateway-token</artifactId>

<version>${project.parent.version}</version>

<name>${project.artifactId}</name>

<dependencies>

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-gateway</artifactId>

</dependency>

<!-- JWT -->

<dependency>

<groupId>io.jsonwebtoken</groupId>

<artifactId>jjwt</artifactId>

</dependency>

<!-- swagger2 -->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

</dependency>

<!-- swagger-ui -->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

</dependencies>

如上我们就针对我们整个项目的各个模块做好了依赖版本管理,如果需要查看源码可以文章最后的git地址中下载

  1. 在聚合项目中实现版本管理

2.1 什么是聚合项目

所谓聚合项目,就是单独的一个空maven项目,只有pom文件,专门用于依赖的版本声明,其他的项目通过引入该聚合项目来实现依赖版本管理

这个比在父项目中进行版本管理更加凸显模块的专业化

2.2 实操

1、创建一个空maven项目,只保留pom.xml文件。将该项目的打包方式声明为pom

<packaging>pom</packaging>

2、然后将上述的父项目中的​​dependencyManagement​​标签中的依赖管理添加到聚合项目的pom文件中

3、在商品服务、订单服务、网关服务、commons服务中引入该聚合项目,从而实现版本的统一管理

相关推荐
jimy110 分钟前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
captain37621 分钟前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
呆呆虫1231 分钟前
用 orjson 加速 SerpBase 响应解析:基准对比与注意事项
开发语言·php
心中有你021442 分钟前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
心疼你的一切1 小时前
Python自动发邮件实战定时发报表
大数据·开发语言·python
步行cgn1 小时前
Spring MVC + Thymeleaf 模板解析失败:从 500 错误到页面正常显示
java·spring·mvc
lgcgkCQ2 小时前
【AI 转型实战 1】SpringAI 入门:搭建基础大模型对话能力
java·ai·agent·rag·spring ai·mcp·langchain4j
Wang's Blog2 小时前
Java 项目实战:交友项目 - 用户体系集成总结
java·开发语言·交友
学到老活到老2 小时前
第 24 集:配置文件写了 80,Spring Boot 为什么读到 90?
java·后端·spring
tongyiixiaohuang2 小时前
供应商主数据同步实战:从易快报到金蝶云星空的单一策略落地
java·网络·数据库