如何解决Eclipse (m2e) 不考虑注解处理器
我正在尝试创建一个自定义注释处理器(使用 maven),但是当我 mvn install
时 eclipse 似乎忽略了它。
我的项目如下所示。 processorowner
是包含注解和处理器的 Maven 项目,而 processorclient
是使用处理器的项目。
我在 pom.xml
Maven 项目中的 processorowner
:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>processorowner</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<auto-service.version>1.0-rc2</auto-service.version>
</properties>
<dependencies>
<!-- For annotation processing -->
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${auto-service.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
处理器和注释:
@SupportedAnnotationTypes("com.example.MyAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class MyProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,RoundEnvironment roundEnv) {
System.out.println("I am processor.");
for (TypeElement anno : annotations) {
anno.getEnclosedElements().forEach(elemtn -> {
processingEnv.getMessager().printMessage(Kind.ERROR,"Some Error",elemtn);
});
}
return true;
}
}
@Retention(SOURCE)
@Target(TYPE)
public @interface MyAnnotation {
}
还有 SomeClass
项目中的 processorclient
:
@MyAnnotation
public class SomeClass {
}
而 processorclient
项目中的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>processorclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<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>
<encoding>UTF-8</encoding>
<generatedSourcesDirectory>${project.build.directory}
</generatedSourcesDirectory>
<annotationProcessors>
<annotationProcessor>
com.example.MyProcessor
</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>processorowner</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
在我查看了所有关于在 Eclipse 中使用项目特定选项启用自定义处理器的帖子后,项目选项 (processorclient
) 如下所示:
当我运行 6. Maven Install
processorclient
项目时,它只是构建项目而不打印“你好,我是处理器”。
但是当我从命令行 mvn clean install
时它会打印它:
我可能遗漏了什么?我的 eclipse 版本是 2020-12 (4.18.0)
,我使用 jdk 1.8。
我不想使用处理器来生成代码。我想将它用于注释验证及其正确用法。例如 MyAnnotation 不能在 final
类中使用(在实际应用中,它的保留策略是 RUNTIME
。这就是为什么我不关心 generated_sources
目录等。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。