执行mvn clean install抛出Lombok错误的原因及解决咨询
问题:执行
mvn clean install构建失败,是否需要降级IntelliJ? 我在对项目执行mvn clean install命令,已安装最新版IntelliJ(2022.2),该版本自带Lombok插件,但执行命令时出现构建错误:
WARNING: Illegal reflective access by lombok.javac.apt.LombokProcessor to field com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs WARNING: Please consider reporting this to the maintainers of lombok.javac.apt.LombokProcessor WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.031 s [INFO] Finished at: 2022-07-29T04:25:35-04:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)
应要求附上完整pom.xml:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.visa.gis.shiftleft</groupId> <artifactId>service-registry-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-registry-server</name> <description>This is a cloud service to enable the micro services to register themselves and discover other services</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.6</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>compile</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
解答:不需要降级IntelliJ
问题核心不在IntelliJ版本,而是Lombok和Maven编译器插件的兼容性问题:
- 当前项目依赖的Spring Boot 1.5.6.RELEASE管理的Lombok版本偏旧,和IntelliJ 2022.2配套的javac版本存在反射访问兼容性问题,导致编译时触发非法反射警告,进而失败。
- 同时你使用的maven-compiler-plugin 3.1版本也比较老,对新的javac兼容性不足。
解决步骤:
- 指定兼容的Lombok版本:在pom.xml的Lombok依赖中明确添加新版本,比如
1.18.24(这个版本稳定适配Java 8):<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>compile</scope> <version>1.18.24</version> </dependency> - 升级Maven编译器插件:在pom.xml的
<build><plugins>块中添加或更新maven-compiler-plugin到3.8.1版本:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> - 确认IntelliJ配置:检查
Settings > Build, Execution, Deployment > Compiler > Annotation Processors,确保Enable annotation processing已勾选,Lombok插件处于启用状态(IntelliJ 2022.2自带的插件默认是启用的,可确认下)。
执行完以上步骤后,重新运行mvn clean install即可完成构建。
内容的提问来源于stack exchange,提问作者Jonathan
相关产品推荐
相关产品推荐

