springboot多模块中,常常使用dependencyManagement来统一依赖版本

一 dependencyManagement 的用处

在创建springboot多模块的项目中,为了保持各模块的相同依赖保持一致,通常会在项目级的POM.XML中使用 dependencyManagement 节> 点来实现这个一致性。

  • 1 项目级pom.xml
 <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot的依赖配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.12.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 通用工具-->
            <dependency>
                <groupId>com.wycms</groupId>
                <artifactId>wycms-vmodel</artifactId>
                <version>${wycms.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <!--系统模块-->
    <modules>
        <module>wycms-admin</module>
        <module>wycms-framework</module>
        <module>wycms-system</module>
        <module>wycms-quartz</module>
        <module>wycms-generator</module>
        <module>wycms-common</module>
        <module>wycms-vmodel</module>
        <module>wycms-remoteapi</module>
    </modules>
    <packaging>pom</packaging>
  • 2 wycms-admin模块的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wycms</artifactId>
        <groupId>com.wycms</groupId>
        <version>4.2.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>wycms-admin</artifactId>

    <description>
        web服务入口
    </description>

    <dependencies>

        <!-- SpringBoot集成thymeleaf模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- SpringBoot集成 validation -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <!-- spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 表示依赖不会传递 -->
        </dependency>
 

        <!-- Mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- 核心模块-->
        <dependency>
            <groupId>com.wycms</groupId>
            <artifactId>wycms-framework</artifactId>
        </dependency>

        <!-- 配置文件加密-->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!-- 定时任务-->
        <dependency>
            <groupId>com.wycms</groupId>
            <artifactId>wycms-quartz</artifactId>
        </dependency>

        <!-- 代码生成-->
        <dependency>
            <groupId>com.wycms</groupId>
            <artifactId>wycms-generator</artifactId>
        </dependency>

        <!--工具类-->
        <dependency>
            <groupId>com.xiaoleilu</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>

        <!--网页抓取-->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>${jsoup.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.1.RELEASE</version>
                <configuration>
                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>${project.artifactId}</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                        <nonFilteredFileExtension>eot</nonFilteredFileExtension>
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                        <nonFilteredFileExtension>svg</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>

</project>

可以看到,在模块级的pom.xml中引用的模块,此时都不需要指定version,因为在项目级的pom.xml中已经做了总结的约定。

二 dependencyManagement与dependencies的区别

dependencyManagement只做申明,项目在打包的时候,并不会将dependencyManagement配置节点下的依赖申请最终打到jar 包中去。而> dependencies是会把节点下的依赖真实的打包到最终的JAR里面去的。