自定义parent模块:统一版本号

Spring Cloud微服务系列文章,点击上方合集↑

1. parent模块

在一个拥有多个模块的微服务项目中,使用parent模块来管理依赖包的版本号是非常重要的。这样做可以确保所有模块使用相同的依赖版本,避免了手动在每个模块中写具体的版本号,从而降低了出错的风险并减少了工作量。

在根目录上右键,选择 “New” -> “Module”。创建一个新的模块。这个模块只保留pom.xml文件。

创建新的模块后要在根目录pom.xml<modules>节点里面添加<module>parent</module>

2. pom.xml

parent模块目录下只有一个pom.xml文件,它在dependencyManagement节点下定义了我们需要用到的包和版本号。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>parent</name>
    <description>parent</description>

    <properties>
        <!-- spring cloud 版本 -->
        <spring-cloud.version>2021.0.5</spring-cloud.version>
        <!-- spring cloud alibaba 版本 -->
        <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
        <mybatis.version>2.3.1</mybatis.version>
        <mybatis-plus.version>3.5.3</mybatis-plus.version>
        <lombok.version>1.18.28</lombok.version>
        <shardingsphere-jdbc.version>5.4.0</shardingsphere-jdbc.version>
        <hutool-version>5.8.9</hutool-version>
        <minio-version>8.4.3</minio-version>
        <okhttp3.version>4.8.1</okhttp3.version>

        <snakeyaml.version>1.33</snakeyaml.version>
        <redisson-starter.version>3.16.8</redisson-starter.version>

        <rocketmq-starter.version>2.2.2</rocketmq-starter.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>shardingsphere-jdbc-core</artifactId>
                <version>${shardingsphere-jdbc.version}</version>
            </dependency>

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool-version}</version>
            </dependency>

            <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>${minio-version}</version>
            </dependency>

            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>${redisson-starter.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.rocketmq</groupId>
                <artifactId>rocketmq-spring-boot-starter</artifactId>
                <version>${rocketmq-starter.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
  • properties定义变量 <spring-cloud.version>2021.0.5</spring-cloud.version>,然后在dependency使用 ${spring-cloud.version}

可以看出我们自定义的parent模块也引用了spring-boot-starter-parent这个官方的parent。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.13</version>
    <relativePath/>
</parent>
  • spring-boot-starter-parent帮我们定义了很多像springmysql等包的版本号。

3. 其它模块使用parent模块

其它模块在pom.xml配置文件开头引用parent模块,然后我们的dependency就不需要写具体的版本号了。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.example</groupId>
        <version>1.0.0</version>
        <artifactId>parent</artifactId>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>provider-service</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <name>provider-service</name>
    <description>provider-service</description>

    <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-discovery</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>
</project>

4. Spring Boot、Spring Cloud、Spring Cloud等版本号

Spring Boot、Spring Cloud、Spring Cloud Alibaba等要选择对应的版本,不是对应的版本可能代码无法运行。

项目 版本号 备注
Java 8
Spring Boot 2.6.13 3.0以后的版本都要Java17
Spring Cloud 2021.0.5 微服务
Spring Cloud Alibaba 2021.0.5.0
Nacos 2.2.0 注册中心、配置中心
Sentinel 1.8.6 服务限流降级
RocketMQ 4.9.4 消息队列
Seata 1.6.1 分布式事务

自定义parent模块:统一版本号

Spring Cloud 微服务系列 完整的代码在仓库的sourcecode/spring-cloud-demo目录下。

gitee(推荐):https://gitee.com/cunzaizhe/xiaohuge-blog

github:https://github.com/tigerleeli/xiaohuge-blog

关注微信公众号:“小虎哥的技术博客”,让我们一起成为更优秀的程序员❤️!

原文始发于微信公众号(小虎哥的技术博客):自定义parent模块:统一版本号

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/169795.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!