pom管理规范

0. 引言
在单机架构下,我们只需要将我们的依赖在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地址中下载

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值