You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何子模块POM无法继承父POM中的Lombok依赖?

问题原因及解决方案

原因分析

Maven采用单继承机制,每个项目的POM只能指定一个直接父POM。你的user子模块明确将spring-boot-starter-parent设为<parent>,这会完全替代根POM的继承关系,因此根POM中声明的Lombok依赖不会被user子模块继承。

解决方案

提供三种可行的处理方式,可根据项目结构需求选择:

1. 调整根POM继承Spring Boot父POM,子模块继承根POM

这是多模块Spring Boot项目的标准结构,既能统一管理依赖版本,又能保持子模块的继承关系:

  • 根POM配置:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>你的Spring Boot版本号</version>
    <relativePath/>
</parent>

<groupId>你的根项目GroupId</groupId>
<artifactId>根项目ArtifactId</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
    <module>user</module>
    <module>service2</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>你的Lombok版本号</version>
            <scope>provided</scope>
        </dependency>
        <!-- 可在此添加其他需要统一管理的依赖 -->
    </dependencies>
</dependencyManagement>
  • user子模块POM配置:
<parent>
    <groupId>你的根项目GroupId</groupId>
    <artifactId>根项目ArtifactId</artifactId>
    <version>1.0.0</version>
</parent>

<artifactId>user</artifactId>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- 其他Spring Boot业务依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2. 在子模块中导入根POM的依赖管理

如果不想修改根POM的父依赖,可在user子模块中通过dependencyManagement导入根POM的依赖配置,间接继承Lombok依赖:

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

<artifactId>user</artifactId>
<packaging>jar</packaging>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>你的根项目GroupId</groupId>
            <artifactId>根项目ArtifactId</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- 其他业务依赖 -->
</dependencies>

3. 直接在子模块中声明Lombok依赖

这是最直接但不利于版本统一维护的方式,适合临时场景:

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>你的Lombok版本号</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 04:00:31