Maven -- <dependencyManagement>管理子项目版本

背景:

        一个旧项目,想使用mybatis-plus,想着这是比较基础的依赖包,就在父项目中添加对应依赖,如下:

    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>
            <!-- mybatis-plus 依赖配置 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>3.4.1</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.1</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.baomidou</groupId>
                        <artifactId>mybatis-plus-generator</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

        然后我就发现,子项目还是没有对应的jar包,maven 重新加载也没用,后面我看了下项目包含的jar包,发现也没有上面我父项目依赖的包。

原因:

        原来父项目中 <dependencyManagement> 标签下依赖的jar包,是不会加人到项目中取的,这个标签的作用是定义公共依赖的版本号,比如我定义了mybatis-plus 的版本为3.4.1,那如果子项目中需要使用mybatis-plus ,导入依赖时就不需要输入版本号了,如下:

    <dependencies>
        <!-- mybatis-plus 依赖配置 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-generator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

        对应的依赖包就会导入到项目,版本号会默认使用父项目pom.xml文件中的版本号3.4.1

         

        这样的好处是,如果一个项目子项目众多,很多基本的依赖都需要,redis,数据库,中间件,http,nacos之类的,如果每个子项目都自己定义版本号,首先不好维护,其次不同版本直接的通讯也容易出问题,所以这类基础包都可以放到父项目,统一版本控制,这样就算是要修改其中哪个依赖的版本,也很容易

猜你喜欢

转载自blog.csdn.net/DGH2430284817/article/details/131716882