前言
当使用Spring Boot 3.4.3
创建新项目时,即使正确勾选Lombok
依赖,编译时仍出现找不到符号的错误,但代码中Lombok
注解的使用完全正确。
原因
Spring Boot 3.4.3
在自动生成的pom.xml
中新增了maven-compiler-plugin
的配置,该插件用于管理注解处理器路径。但该配置存在以下两个潜在问题:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><annotationProcessorPaths><!-- 未指定Lombok版本可能导致依赖解析问题 --><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><!-- 错误排除了Lombok依赖 --><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins>
</build>
解决方案
方案一
我的解决方案,简单粗暴,直接将新增的maven-compiler-plugin
注释掉,并且重新刷新Maven
注意:
重新刷新Maven时,如果你光点击悬浮的刷新按钮,还是会报错!!
需要点击右侧的Maven图标,点击重新加载所有Maven项目,才可
但当我可以正常运行后,我把原来注释掉的代码放开,刷新后,重新运行,居然可以运行!!我也不知道为啥,很奇怪?
方案二:最佳实践
该方案是DeepSeek
给我的解决方案,亲测有效
lombok_53">1. 明确指定lombok版本
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.32</version><scope>provided</scope>
</dependency>
2.修改插件配置
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version> <!-- 明确指定插件版本 --><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.32</version> <!-- 指定Lombok版本 --></path></annotationProcessorPaths></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><!-- 移除不必要的Lombok排除配置, --></excludes></configuration></plugin></plugins>
</build>
但是这里我不是很理解,为啥spring-boot-maven-plugin
的排除配置没有必要?
这是DeepSeek
给我的回答