0

I am trying to make annotation processor read a file from src/main/resources package. The code I am using to read the file is:

resource = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", fileName);

The annotation processor is executed by maven-processor-plugin:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.3.3</version>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
                        <outputDirectory>target/generated</outputDirectory>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
        ...
        </dependency
    </dependencies>
</plugin>

However, when triggering the processor by Maven update command (via Eclipse IDE), the processor throws an IO exception saying that the resource file does not exist.

I have to trigger each java file separately for the processor to find the resource files.

I believe this issue is due to the processor, be default, running in "generate-sources" phase, while the resources are not detected until "process-resources". Please note: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Plugins

I have tried changing the phase in the pom file but to no avail. There is a similar post but there has not provided with defined solution: Need file from src/main/resources in generate-sources phase for annotation processor config Strangely, when I run other maven commands like compile or install, the processors can read the resource files (just not through Maven Update). Are there any solutions to how this can be achieved on Maven Update command? Thank you.

J. S.
  • 89
  • 1
  • 3
  • 9

1 Answers1

0

You can use below configuration with maven-compiler-plugin.

<generatedSourcesDirectory>
     ${project.basedir}/src/main/java
</generatedSourcesDirectory>

Check below link for complete implementation. https://www.thetechnojournals.com/2019/12/annotation-processor-to-generate-dto.html

Ashok Prajapati
  • 374
  • 2
  • 7
  • This is almost surely NOT what the OP is after - this will cause generated sources to get mixed into the real project sources (which will create a mess if the project is using git or some other source control). – Tim Boudreau Jun 27 '20 at 16:11