我在 Maven 共享库中有一个自定义注释处理器,它扫描实现特定接口的所有子项目的类:
@SupportedAnnotationTypes("mypackage.annotation")
@SupportedSourceVersion(SourceVersion.RELEASE_17)
public class MyProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forClassLoader(new URLClassLoader(new URL[]
{new File(System.getProperty("user.dir")).toURI().toURL()
})))
.setScanners(new SubTypesScanner(false)));
...
...
//other code
return true;
}
}
我将该类打包为 jar 文件并将其添加到我的子项目的依赖项中。
<dependency>
<groupId>my.processor</groupId>
<artifactId>Processor</artifactId>
<version>0.0.1</version>
</dependency>
为了让这个注释处理器在子类中工作,我需要在运行时禁用注释处理,以便首先编译项目类:
子项目的pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</execution>
<execution>
<id>compile-project</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
到目前为止,自定义注释处理器按预期工作。
但是,假设在子项目中我想启用@Slf4j 日志记录:
@Slf4j
public class MyConfigSource implements ConfigSource {
public MyConfigSource() throws IOException {
log.info("hello");
}
}
在这种情况下,当我编译代码时会引发错误:
[ERROR] MyConfigSource.java:[53,9] cannot find symbol
[ERROR] symbol: variable log
[ERROR] location: class MyConfigSource.java
我相信这是因为我在第一个编译阶段禁用了注释处理,但需要完成此操作才能使我的自定义注释正常工作。
是否有解决此问题的方法,以便我也可以拥有 @Slf4j 日志记录和自定义注释处理器?