Igor Dumchykov
Igor Dumchykov

Reputation: 397

Integrate annotation processor into the same project

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

Example:

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."

Upvotes: 14

Views: 3399

Answers (2)

Vsevolod Golovanov
Vsevolod Golovanov

Reputation: 4206

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.

Upvotes: 2

Stefan Ferstl
Stefan Ferstl

Reputation: 5255

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.

Upvotes: 5

Related Questions