SpringBoot 项目不继承 parent 的实现方法

SpringBoot 项目不继承 parent 的实现方法

当项目需要继承自定义的 parent POM 而不能直接继承 spring-boot-starter-parent 时,可以通过以下方式实现 SpringBoot 项目的功能。

一、主要实现方式

SpringBoot 官方提供了不继承 parent 的替代方案,主要通过在 dependencyManagement 中引入 spring-boot-dependencies 来实现版本仲裁功能。

二、完整配置示例

1. 基本 POM 配置

xml 复制代码
<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">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承自定义 parent POM -->
    <parent>
        <groupId>com.yourcompany</groupId>
        <artifactId>your-company-parent</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>springboot-without-parent</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <!-- 手动配置基础属性(原本由 spring-boot-starter-parent 提供) -->
    <properties>
        <java.version>1.8</java.version>
        <resource.delimiter>@</resource.delimiter>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring.boot.version>2.7.0</spring.boot.version>
    </properties>

    <!-- 引入 Spring Boot 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot 依赖管理,提供版本仲裁 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 项目依赖 -->
    <dependencies>
        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 不需要指定版本,由 dependencyManagement 中的 spring-boot-dependencies 控制 -->
        </dependency>
        
        <!-- Spring Boot 测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- 配置 Maven 插件 -->
    <build>
        <plugins>
            <!-- Spring Boot Maven 插件,用于打包可执行 JAR -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

三、关键说明

1. 版本管理

  • 当不继承 spring-boot-starter-parent 时,需要在 dependencyManagement 中显式引入 spring-boot-dependencies 并使用 import 范围
  • 这样可以获得与继承 parent 相同的版本仲裁能力,引入 starter 依赖时无需指定版本

2. 基础配置

  • 需要手动配置原本由 parent 提供的基础属性:
    • Java 版本
    • 编译参数
    • 编码格式
    • 资源分隔符等

3. 插件配置

  • 必须手动配置 spring-boot-maven-plugin 插件以支持打包功能
  • 该插件负责将应用打包成可执行的 JAR 文件

四、适用场景

  1. 项目需要继承自定义的 parent POM(如公司内部统一的 parent)
  2. 需要在多个项目间共享公司级别的 Maven 配置
  3. 需要自定义或覆盖 SpringBoot 默认配置

五、注意事项

  1. 确保 spring-boot-dependencies 的版本与项目使用的 SpringBoot 版本一致
  2. 当需要覆盖特定依赖的版本时,可以在 properties 中定义相应的版本属性
  3. 打包时确保正确配置了主类,可通过插件配置指定:
xml 复制代码
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.boot.version}</version>
    <configuration>
        <mainClass>com.example.Application</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

通过以上配置,即使不直接继承 spring-boot-starter-parent,也能正常使用 SpringBoot 的所有核心功能,同时满足项目继承自定义 parent 的需求。

相关推荐
weixin_4316004412 分钟前
前端对接 SSE 的两种常见方式
前端·后端·学习·ai·sse·nest.js
Larcher6 小时前
React Router 不只是页面跳转:从 SPA 路由到权限守卫的完整实践
javascript·后端
Larcher6 小时前
大模型为什么每次回答都不一样?一文搞懂 Temperature、Top K 与 Top P
javascript·后端
LucianaiB8 小时前
毕业老学长给我留的最后一句话是:“AI 写的,别挂我名。” 我直接 神(Seed Evolving) 来!助我!
后端
IvanCodes9 小时前
RAG 实战教程(一):RAG 工作原理与完整流程——分片、索引、召回、重排和生成
人工智能·后端·agent
·薯条大王9 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
CodeSheep9 小时前
稚晖君公司人事大变动,来了!
前端·后端·程序员
小满zs10 小时前
Go语言第八章(函数)
后端·go
stark张宇11 小时前
实战Go高级特性:Context超时控制、defer资源回收与Channel通信的关键避坑点
后端·go
ZJH__GO12 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络