INFO] Finished at: 2025-04-17T10:11:20+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project tzmap-common: Fatal error compiling: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x6e7f29d5) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x6e7f29d5 -> [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/MojoExecutionException [ERROR] 打包报错
时间: 2025-05-20 22:33:10 浏览: 13
### 问题分析
`java.lang.IllegalAccessError` 错误通常发生在模块化环境中,当某个类尝试访问另一个受限的内部API时会抛出此异常。具体到当前场景中,Lombok插件中的 `lombok.javac.apt.LombokProcessor` 类试图访问 JDK 编译器模块 (`jdk.compiler`) 中未导出的包 `com.sun.tools.javac.processing.JavacProcessingEnvironment`。
以下是可能的原因以及解决方案:
---
### 原因解析
1. **JDK 版本变化**
自 Java 9 起引入了模块化系统 (JPMS),许多以前公开的内部 API(如 `com.sun.*`)被限制访问[^1]。如果使用的 Lombok 或其他工具仍然依赖这些内部 API,则可能会引发此类错误。
2. **Maven Compiler Plugin 配置不兼容**
Maven 的 `maven-compiler-plugin` 默认配置可能无法正确处理某些复杂的编译环境,尤其是涉及注解处理器的情况[^2]。
3. **Lombok 版本过旧或不适配**
如果项目的 Lombok 版本较老或者与所使用的 JDK 不匹配,也可能导致类似的错误。例如,在高版本 JDK 上运行低版本 Lombok 可能会出现这种问题[^3]。
---
### 解决方案
#### 方法一:升级 Lombok 至最新稳定版
确保项目中使用的 Lombok 是最新的稳定版本。根据已有经验,推荐使用至少 `1.18.24` 或更高版本[^3]。修改 `pom.xml` 文件如下:
```xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
```
#### 方法二:调整 Maven Compiler 插件配置
通过显式指定目标 JDK 和源码版本来适配不同的 JDK 环境。更新 `pom.xml` 中的 `maven-compiler-plugin` 配置如下:
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<!-- 添加以下参数以支持模块化 -->
<fork>true</fork>
<compilerArgs>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
```
以上配置允许 Maven 使用额外的模块导出选项,从而绕过默认的安全限制[^1]。
#### 方法三:切换至 ECJ 编译器
作为替代方案,可以考虑将 Maven 的编译器替换为 Eclipse JDT 编译器 (ECJ),它完全独立于 JDK 并不受其模块化的限制。在 `pom.xml` 中添加以下依赖项即可启用 ECJ:
```xml
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.35.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<forceJavacCompilerUse>false</forceJavacCompilerUse>
<compilerId>eclipse</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.8.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
```
---
### 总结
上述方法分别针对不同层面提供了修复策略。优先建议升级 Lombok 到最新版本并适当调整 Maven Compiler Plugin 的配置;若仍存在问题,可进一步尝试切换至 ECJ 编译器以规避 JDK 模块化带来的限制。
---
阅读全文
相关推荐


















