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

如何引用Spring BOM的dependencyManagement中定义的lombok.version属性?

问题:如何引用Spring Boot BOM中定义的lombok.version属性?

我在项目的dependencyManagement中引入了Spring Boot的BOM:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

该BOM中定义了<lombok.version>1.18.24</lombok.version>属性,我需要在自己的pom.xml的maven-compiler-plugin配置中引用这个属性,配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <release>${java.version}</release>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </annotationProcessorPath>
                    <annotationProcessorPath>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

执行mvn compile时提示找不到该属性,报错信息如下:

~/projects/cultura/git/repositories/backend develop* ❯ mvn compile                                                                                                                                                                                                     14:56:41

[INFO] Scanning for projects...

[INFO] 

[INFO] ---------------------< cat.gencat.clt.git:backend >--------------------- 

[INFO] Building backend 0.0.1-SNAPSHOT

[INFO] --------------------------------[ jar ]---------------------------------

[INFO] 

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ backend ---

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] Copying 3 resources

[INFO] 

[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ backend ---

[INFO] ------------------------------------------------------------------------

[INFO] BUILD FAILURE

[INFO] ------------------------------------------------------------------------

[INFO] Total time:  5.092 s

[INFO] Finished at: 2022-08-23T14:56:52+02:00

[INFO] ------------------------------------------------------------------------

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project backend: Resolution of annotationProcessorPath dependencies failed: For artifact {org.projectlombok:lombok:null:jar}: The version cannot be empty. -> [Help 1]

[ERROR] 

[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

[ERROR] Re-run Maven using the -X switch to enable full debug logging.

[ERROR] 

[ERROR] For more information about the errors and possible solutions, please read the following articles:

请问有没有办法引用Spring BOM的dependencyManagement中定义的lombok.version属性?


解决方案

方法一:显式声明属性到项目properties中

Spring Boot BOM内的属性不会自动暴露给当前项目直接引用,你可以在自己的pom.xml的<properties>节点中手动声明该属性,直接复用BOM里的版本值:

<properties>
    <lombok.version>1.18.24</lombok.version>
</properties>

这样就能正常在编译器插件中使用${lombok.version}了。注意后续如果Spring Boot BOM更新了lombok版本,需要同步修改此处的版本号。

方法二:通过dependencyManagement间接复用版本(推荐)

无需直接引用属性,而是利用Maven dependencyManagement的版本管理能力,间接复用BOM中的lombok版本:

  1. 在项目的dependencyManagement中添加lombok的依赖声明(版本会自动继承BOM中的值):
<dependencyManagement>
    <!-- 原有的Spring Boot BOM导入 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.7.0</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <!-- 添加lombok的依赖声明 -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencyManagement>
  1. 修改maven-compiler-plugin的annotationProcessorPath配置,去掉version节点:
<annotationProcessorPath>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</annotationProcessorPath>

这种方式更优雅,版本完全由Spring Boot BOM管控,后续更新BOM版本时无需修改其他配置。

方法三:用properties-maven-plugin导出BOM属性

借助properties-maven-plugin将Spring Boot BOM中的属性读取到当前项目中:

  1. 在pom.xml的<build>中添加该插件:
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <id>load-spring-boot-properties</id>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>${settings.localRepository}/org/springframework/boot/spring-boot-dependencies/2.7.0/spring-boot-dependencies-2.7.0.pom</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. 注意:此方法依赖本地仓库已存在Spring Boot BOM的pom文件,首次构建前需先执行mvn dependency:resolve下载BOM。后续更新Spring Boot版本时,需要同步修改插件配置中的文件路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 16:06:25