Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate annotation processor into the same project

Tags:

java

spring

maven

Is it possible to use annotation processor in the same project where it is defined?

Example:

  • src/

    • MyAnnotation.java
    • path_to_MyAnnotationProcessor.MyAnnotationProcessor.java
    • other classes
  • resources
    • META-INF/services/javax.annotation.processing.Processor
  • pom

when I will run mvn clean install, I will expect that my processor will process classes annotated with MyAnnotation.

I don`t want to import already compiled processor from another lib, I just want to use it once I defined it in my src.

For now, I get error: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project my-project: Compilation failure [ERROR] Annotation processor 'path_to_MyAnnotationProcessor' not found

part of pom.xml, where I ref. to my processors:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.plugin.compiler}</version>
            <configuration>
                <source>${version.java}</source>
                <target>${version.java}</target>
                    <annotationProcessors>
                       <proc>path_to_MyAnnotationProcessor.MyAnnotationProcessor</proc>
                    </annotationProcessors>
            </configuration>
        </plugin>

Thanks to everybody, especially to @Stefan Ferstl and @yegodm. The solution came from yegodm is: "One way is two have two modules in the same project. One module would define annotations and processor. Another would have it as a dependency to establish build order."

like image 953
Igor Dumchykov Avatar asked Apr 16 '19 20:04

Igor Dumchykov


People also ask

Why do we need annotation processor?

Annotations provide information to a program at compile time or at runtime based on which the program can take further action. An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc.

How does annotation processing work?

Annotation processing happens in a sequence of rounds. On each round, a processor may be asked to process a subset of the annotations found on the source and class files produced by a prior round.

Is Lombok annotation processor?

Javac: Launching as annotation processor. With javac (and netbeans, maven, gradle, and most other build systems), lombok runs as an annotation processor.


2 Answers

The easiest way to solve this problem is convert your project into a multi-module project where the annotation processor is in its own module. Having a different module for the annotation processor, you could use the quite new <annotationProcessorPaths> option to define the annotation processor via groupId/artifactId.

The module using the annotation processor might need a dependency to the annotation processor module to get it built first.

Note: In a previous version of this answer I described an additional way to solve this problem, which apparently didn't work out of the box. That part has been deleted.

like image 152
Stefan Ferstl Avatar answered Oct 10 '22 05:10

Stefan Ferstl


You could compile your processor earlier with a separate compiler execution.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>compile-generator</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>com/example/YourProcessor.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I've tested this, and it works - the processor does get invoked later during the actual compile phase.

If you precompile some other classes from the same project too, then you could directly reference and use them in the processor. That could be useful.

like image 1
Vsevolod Golovanov Avatar answered Oct 10 '22 03:10

Vsevolod Golovanov