Integrate annotation processor into the same project

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.


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.

Tags:

Java

Maven

Spring