Maven -- <dependencyManagement>Manage sub-project versions

background:

        An old project, I want to use mybatis-plus, thinking that this is a relatively basic dependency package, so I add the corresponding dependencies in the parent project, as follows:

    <!-- 依赖声明 -->
    <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>

        Then I found that the sub-project still did not have a corresponding jar package, and it was useless to reload maven. Later I looked at the jar packages included in the project and found that there were no packages that my parent project relied on.

reason:

        It turns out that the dependent jar packages under the <dependencyManagement> tag in the parent project will not be added to the project. The function of this tag is to define the version number of the public dependency. For example, I defined the version of mybatis-plus as 3.4.1 , then if you need to use mybatis-plus in the sub-project, you do not need to enter the version number when importing dependencies, as follows:

    <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>

        The corresponding dependency package will be imported into the project, and the version number will use the version number 3.4.1 in the parent project's pom.xml file by default.

         

        The advantage of this is that if a project has many sub-projects, many basic dependencies are required, such as redis, database, middleware, http, nacos, etc. If each sub-project defines its own version number, firstly, it is difficult to maintain, and secondly, it is different. Direct version communication is also prone to problems, so such basic packages can be placed in the parent project and unified version control, so that even if you want to modify any dependent version, it is easy

おすすめ

転載: blog.csdn.net/DGH2430284817/article/details/131716882