关于解决项目中Lombok的注解处理器没有生效的问题

❓ 发现问题

我的项目POM文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.you</groupId>
    <artifactId>mfd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mfd</name>
    <description>mfd</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

以上配置,在plugin的maven-compiler-plugin中,如果注释掉configuration中的内容,即项目中的@Data注解有效,一旦将该配置释放开,即项目中的@Data就立马失效了。


经过排查,发现是因为 Lombok 的注解处理器依赖没有加版本号,导致 maven-compiler-plugin 配置失效。


🔍 根本原因

我的 maven-compiler-plugin 中的配置是这样的(缺少 <version>):

<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <!-- ⚠️ 缺少版本号 -->
    </path>
</annotationProcessorPaths>

如果没有指定 lombok<version>,Maven 无法正确拉取 annotation processor,会导致编译器无法识别如 @Data@Getter 等注解的效果,从而看起来“失效”。


✅ 正确写法(推荐)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version> <!-- 可选但推荐 -->
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.30</version> <!-- ✅ 一定要加上版本号! -->
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

🔧 为什么注释掉就能生效?

  • 如果你完全移除 <annotationProcessorPaths> 配置,Maven 会退回使用 Lombok 在依赖中默认的 annotation processor。
  • 但一旦你显式配置 <annotationProcessorPaths>,就只会使用你配置的那部分,如果没写版本,就等于没指定依赖,导致编译失败或无效。

✅ 最佳实践建议:

  • <dependencies> 中保留:
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <scope>provided</scope>
</dependency>
  • 并在 maven-compiler-plugin 中配置 annotation processor:
<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.30</version>
    </path>
</annotationProcessorPaths>

评论

等风等雨等你来