I am trying to create a custom Annotation Processor (with maven) but eclipse seems to ignore it when I mvn install
.
My projects look like the following. processorowner
is the maven project that contains the annotation and the processor while processorclient
is the project that consumes the processor.
My pom.xml
in processorowner
maven project:
<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>
The processor and the annotation:
@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 {
}
And the SomeClass
in processorclient
project:
@MyAnnotation
public class SomeClass {
}
while the pom in processorclient
project is:
<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>
After I looked into probably all posts about enabling custom processors in eclipse with project specific options, the project options (processorclient
) look like:
When I run 6. Maven Install
processorclient
project, it just builds the project without printing "Hello I am processor".
But when I mvn clean install
from command line it prints it:
What am I possibly missing? My eclipse version is 2020-12 (4.18.0)
and I use jdk 1.8.
I do not want to use the processor for code generation. I want to use it for annotation validation and their correct usage. For example MyAnnotation cannot be used in final
classes (in real application, its retention policy is RUNTIME
. That's why I did not care about generated_sources
directories etc.
question from:
https://stackoverflow.com/questions/65875260/eclipse-m2e-does-not-take-annotation-processor-in-consideration 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…