Java 21自定义注解处理器无法访问JavacTrees问题求助
Java 21自定义注解处理器依赖时的模块访问问题解决
问题背景
用Java 21开发了一个自定义注解处理器,打包到本地仓库后,其他项目依赖该处理器时出现模块访问错误,添加JDK模块导出配置后问题仍存在;开启<fork>true</fork>后编译成功,但注解处理器无法正常工作。目标是构建类似Lombok的注解处理器,让其他项目可直接依赖使用。
处理器项目原POM配置
<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> <parent> <groupId>org.example</groupId> <artifactId>xxx-xx</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>xxx-xx-xx</artifactId> <packaging>jar</packaging> <name>async-log-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>21</source> <target>21</target> <encoding>UTF-8</encoding> <compilerArgs> <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> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg> </compilerArgs> </configuration> </plugin> </plugins> </build> </project>
依赖项目编译错误信息
class org.example.MyCustomProcessor (in unnamed module @0x3b48e183) cannot access class com.sun.tools.javac.api.JavacTrees (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.api to unnamed module @0x3b48e183
尝试过的无效配置
在依赖项目中添加相同的JDK模块导出配置,但问题未解决:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>21</source> <target>21</target> <encoding>UTF-8</encoding> <compilerArgs> <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> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>
解决方案
1. 优化注解处理器项目的POM配置
调整依赖范围和编译配置,确保auto-service正确生成服务配置文件,同时避免不必要的依赖传递:
<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> <parent> <groupId>org.example</groupId> <artifactId>xxx-xx</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>xxx-xx-xx</artifactId> <packaging>jar</packaging> <name>async-log-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>21</java.version> <auto-service.version>1.1.1</auto-service.version> <maven-compiler-plugin.version>3.12.1</maven-compiler-plugin.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 仅编译时依赖auto-service,用于生成处理器注册文件 --> <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>${auto-service.version}</version> <scope>provided</scope> </dependency> <!-- 依赖JDK工具类,仅编译时使用 --> <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>${java.version}</version> <scope>system</scope> <systemPath>${java.home}/lib/tools.jar</systemPath> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> <compilerArgs> <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> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg> </compilerArgs> <!-- 明确指定auto-service为注解处理器,确保生成META-INF/services配置 --> <annotationProcessorPaths> <path> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>${auto-service.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> </project>
2. 修正依赖项目的POM配置
开启编译fork,并通过annotationProcessorPaths显式指定注解处理器,确保参数传递到javac进程:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.1</version> <configuration> <source>21</source> <target>21</target> <encoding>UTF-8</encoding> <!-- 必须开启fork,才能将--add-exports参数传递给javac独立进程 --> <fork>true</fork> <compilerArgs> <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> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg> </compilerArgs> <!-- 显式指定注解处理器路径,确保编译器加载你的自定义处理器 --> <annotationProcessorPaths> <path> <groupId>org.example</groupId> <artifactId>xxx-xx-xx</artifactId> <version>1.0-SNAPSHOT</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
问题原因说明
- Java 9+模块系统默认不开放
com.sun.tools.javac.*等内部API到未命名模块,注解处理器运行在javac进程中,必须通过--add-exports显式授权访问。 - 未开启
fork=true时,maven-compiler-plugin复用当前JVM进程,参数无法传递给javac的实际编译进程;开启后需通过annotationProcessorPaths指定处理器,避免类加载器导致的处理器无法被发现问题。 - auto-service负责生成
META-INF/services/javax.annotation.processing.Processor文件,是编译器发现自定义处理器的关键,需确保该文件被正确打包到处理器jar中。
内容的提问来源于stack exchange,提问作者Photon
相关产品推荐
相关产品推荐

