两种方式实现SpringBoot脚手架的依赖配置

在基于Spring Boot脚手架创建项目时,通常会直接将spring-boot-starter-parent作为<parent>,如下示例:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.10</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

再添加其他相关stater组件依赖时可以不用再明确指定版本号了,非常方便:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

但是,当项目中存在多个Maven模块时,通常项目中的<parent>节点是自定义的,并不能直接设置为spring-boot-starter-parent,但是又需要便于使用Spring Boot脚手架,此时该如何处理呢?

实际上,跟踪spring-boot-starter-parent的pom文件配置后发现,它将<parent>设置为了spring-boot-dependencies

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.10</version>
</parent>

真正的组件依赖是在spring-boot-dependencies中使用<dependencyManagement>进行管理的。

参考Spring Boot自身的组件依赖管理办法,针对在日常开发中既要便于使用Spring Boot脚手架,又不能直接将项目<parent>设置为spring-boot-starter-parent的场景,有2种解决办法:

方式一:将父项目的<parent>设置为spring-boot-starter-parent,然后在子项目中添加starter组件的依赖。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.10</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<packaging>pom</packaging>
<modules>
    <module>xxx</module>
</modules>

方式二:在父项目中使用<dependencyManagement>管理spring-boot-dependencies依赖,这种方式就使用不到spring-boot-starter-parent中的一些基础配置了。

当父项目不设置<parent>时,可以通过<dependencyManagement>管理spring-boot-dependencies依赖,然后在子项目中添加starter组件的依赖。

<packaging>pom</packaging>
<modules>
    <module>xxx</module>
</modules>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.10</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

上述2种方式的区别在于:使用第一种方式可以直接使用Spring Boot脚手架的全部基础配置,更加便捷。

例如:使用第一种方式在子项目中使用spring-boot-maven-plugin插件打包时只需要添加依赖即可,无需其他任何配置就可以将项目打包为可执行jar包。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

在使用第二种方式时使用spring-boot-maven-plugin插件打包为可执行jar包还需要额外的配置,并且必须明确指定插件版本号。

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 明确指定插件版本号,不指定会报错 -->
            <version>2.7.10</version>
            <executions>
                <execution>
                    <id>repackage</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- 明确指定启动类 -->
                <mainClass>org.test.springboot.implementation.App</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

当然,使用Spring Boot框架也可以不使用其任何基础配置,直接在依赖管理中明确指定各种starter组件的版本即可。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.7.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.7.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.7.10</version>
        <scope>test</scope>
    </dependency>
</dependencies>

但是这显然是一件吃累不讨好的事情,不是万不得已不要使用这种方式!

【参考】
你真的理解 Spring Boot 项目中的 parent 吗?
不使用 parent 方式创建SpringBoot项目篇
spring boot 项目不使用 spring-boot-starter-parent 构建,使用自己项目的parent构造

posted @ 2024-04-09 16:36  nuccch  阅读(36)  评论(0编辑  收藏  举报