java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot项目不继承parent

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

作者:匆匆忙忙游刃有余

本文介绍了如何在不直接继承spring-boot-starter-parent的情况下,通过配置dependencyManagement和spring-boot-maven-plugin来实现SpringBoot项目的功能,关键包括版本管理、基础配置和插件配置,并讨论了适用场景和注意事项,需要的朋友可以参考下

引言

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

一、主要实现方式

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

二、完整配置示例

1. 基本 POM 配置

<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. 版本管理

2. 基础配置

3. 插件配置

四、适用场景

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

五、注意事项

  1. 确保 spring-boot-dependencies 的版本与项目使用的 SpringBoot 版本一致
  2. 当需要覆盖特定依赖的版本时,可以在 properties 中定义相应的版本属性
  3. 打包时确保正确配置了主类,可通过插件配置指定:
<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 的需求。

到此这篇关于SpringBoot项目不继承parent的实现方案的文章就介绍到这了,更多相关SpringBoot项目不继承parent内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文