You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Maven的pom.xml中同时引入Spring Boot父依赖与其他父依赖?

解决Spring Boot子模块无法同时配置两个父依赖的问题

这个场景我碰到太多次了——Maven本身只允许每个模块有一个直接父pom,一边要继承Spring Boot的父依赖来获得自动配置和依赖管理,另一边要作为大项目的子模块继承顶层父pom,确实有点矛盾。不过有两个非常成熟的方案可以解决:

方案1:放弃继承Spring Boot父pom,改用dependencyManagement导入依赖管理

这是最灵活的方式,不需要调整现有项目结构,只需要修改你的Spring Boot子模块的pom.xml:

原来的Spring Boot父依赖配置是这样的:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>你的Spring Boot版本</version>
    <relativePath/>
</parent>

现在把它替换成在dependencyManagement中引入Spring Boot的依赖管理,同时保留你大项目的父pom配置:

<!-- 保留大项目的父pom -->
<parent>
    <groupId>你的大项目groupId</groupId>
    <artifactId>大项目父pom的artifactId</artifactId>
    <version>大项目版本</version>
    <relativePath>../pom.xml</relativePath> <!-- 根据实际路径调整 -->
</parent>

<!-- 导入Spring Boot的依赖管理 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>你的Spring Boot版本</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

⚠️ 注意:这种方式下,Spring Boot的Maven插件(比如spring-boot-maven-plugin)不会自动继承,你需要在子模块的build/plugins里手动配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>你的Spring Boot版本</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

方案2:调整项目结构,让大项目的顶层父pom继承Spring Boot父pom

如果你的大项目所有子模块都需要Spring Boot的依赖管理,或者你希望统一管理所有模块的父依赖,可以让顶层父pom先继承Spring Boot的父pom,然后你的Spring Boot子模块和其他子模块都继承这个顶层父pom:

  1. 修改大项目的顶层pom.xml:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>你的Spring Boot版本</version>
    <relativePath/>
</parent>

<groupId>你的大项目groupId</groupId>
<artifactId>大项目父pom的artifactId</artifactId>
<version>大项目版本</version>
<packaging>pom</packaging>
<modules>
    <module>你的Spring Boot子模块</module>
    <!-- 其他子模块 -->
</modules>

<!-- 这里可以添加大项目统一的依赖管理、插件配置等 -->
  1. 然后你的Spring Boot子模块的pom.xml只需要继承大项目的顶层父pom即可:
<parent>
    <groupId>你的大项目groupId</groupId>
    <artifactId>大项目父pom的artifactId</artifactId>
    <version>大项目版本</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<artifactId>你的Spring Boot子模块artifactId</artifactId>
<packaging>jar</packaging>

<!-- 正常引入Spring Boot starter依赖即可 -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这种方式的好处是所有子模块都能自动继承Spring Boot的依赖管理和插件配置,不需要每个子模块重复配置,适合全栈基于Spring Boot的大项目。

内容的提问来源于stack exchange,提问作者user5156198

火山引擎 最新活动