lombok未正常生效问题排查-maven编译问题排查

本文介绍了解决Maven编译过程中因Lombok引起的错误的方法。主要问题出现在maven-compiler-plugin配置不当,导致Lombok注解未能正确处理。文章详细分析了问题原因,并给出了具体的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题报错-maven编译报错

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Tailgate 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ tailgate ---
[INFO] Deleting D:\gitee\Tailgate\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ tailgate ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ tailgate ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 31 source files to D:\gitee\Tailgate\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
......
[ERROR] /D:/gitee/Tailgate/src/main/java/cn/twh/wall/ServerStart.java:[23,7] 找不到符号
[ERROR] 符号:   变量 log
[ERROR] 位置: 类 cn.twh.wall.ServerStart
[ERROR] /D:/gitee/Tailgate/src/main/java/cn/twh/wall/ServerStart.java:[26,5] 找不到符号
[ERROR] 符号:   变量 log
[ERROR] 位置: 类 cn.twh.wall.ServerStart
[ERROR] -> [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:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

原因分析-idea编译未生效(我的不是这个问题)

  • 检查lombok依赖
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <scope>provided</scope>
</dependency>
  • 检查java文件注解
package cn.twh.wall;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableDiscoveryClient
@EnableSwagger2
@Slf4j
public class ServerStart {

  public static void main(String[] args) {
    try {
      SpringApplication.run(ServerStart.class, args);
      // 可以尝试增加钩子,在jvm停止时执行指定线程方法
      Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.out.println("ShutDown hook");
      }));
    } catch (Exception e) {
      log.info("START ERROR");
      return;
    }
    log.info("START OK");
  }
}
  • 检查Idea的lombok开关
    idea-lombok配置截图

maven编译问题原因分析

lombok在idea中正常,打包不正常。从日志上也可以看出来是由于maven-compiler-plugin导致的异常,那么肯定是这里的配置问题。
配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <proc>none</proc>
        <source>${jdk.version}</source>
        <target>${jdk.version}</target>
        <encoding>utf-8</encoding>
    </configuration>
</plugin>

这块是之前拷贝的,proc这个参数作用不明,查插件资料:
链接:maven-compiler-plugin文档
得知proc作用:

Sets whether annotation processing is performed or not. Only applies to JDK 1.6+ If not set, both compilation and annotation processing are performed at the same time.
Allowed values are:
none - no annotation processing is performed.
only - only annotation processing is done, no compilation.

翻译:

是否执行注释的处理逻辑。仅适用于JDK 1.6以上版本。如果未设置,则同时执行编译和注释处理逻辑。
可选的值为:
none-不执行注释处理。
only-仅完成注释处理,不进行编译。

我们知道lombok是通过注释和修改编译逻辑实现的。
所以如果我们把proc设置为none会导致lombok无法正常使用。【阻断了lombok的实现基础,lombok肯定没办法正常发挥作用的呀!】

解决办法

知道问题原因了,那么解决办法也就很明朗了:
删除 maven-compiler-plugin 中的参数 proc 设置

D:\jdk\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\shuzhi\gps -Djansi.passthrough=true -Dmaven.home=D:\maven\apache-maven-3.6.3 -Dclassworlds.conf=D:\maven\apache-maven-3.6.3\bin\m2.conf "-Dmaven.ext.class.path=D:\Software\idea\IntelliJ IDEA 2023.2.8\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\Software\idea\IntelliJ IDEA 2023.2.8\lib\idea_rt.jar=49294:D:\Software\idea\IntelliJ IDEA 2023.2.8\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\maven\apache-maven-3.6.3\boot\plexus-classworlds-2.6.0.jar;D:\maven\apache-maven-3.6.3\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2023.2.8 -s D:\maven\apache-maven-3.6.3\conf\settings.xml -Dmaven.repo.local=D:\maven\repository clean install [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.huijie:gps-service >----------------------- [INFO] Building gps-service 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ gps-service --- [INFO] Deleting D:\shuzhi\gps\target [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ gps-service --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ gps-service --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 30 source files to D:\shuzhi\gps\target\classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /D:/shuzhi/gps/src/main/java/com/huijie/mqtt/MqttOutboundConfiguration.java:[46,44] 找不到符号 符号: 方法 getClientId() 位置: 类型为com.huijie.mqtt.MqttConfiguration的变量 mqttConfiguration [ERROR] /D:
最新发布
04-25
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值