I use an annotation processor that creates sources in target/generated-sources/annotations/
When these generated sources don't exist I get an error if I run mvn compile
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project project-name: Compilation failure
[ERROR] /path/src/main/java/org/acme/dao/SomeClass.java:[39,19] cannot find symbol
It fails, but it creates the sources, so if I run mvn compile
again it works.
Is there a plugin/configuration that I can add to pom.xml
so it works the first time, please?
Here's the section of my pom.xml
that might be relevant:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>some-group</groupId>
<artifactId>some-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/annotations/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<versionRange>[1.0,)</versionRange>
<goals>
<goal>parse-version</goal>
<goal>add-source</goal>
<goal>maven-version</goal>
<goal>add-resource</goal>
<goal>add-test-resource</goal>
<goal>add-test-source</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnConfiguration>true</runOnConfiguration>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
CodePudding user response:
I found a workaround for this problem here: https://community.oracle.com/tech/developers/discussion/comment/6500306/#Comment_6500306
I basically split the compiler execution and ignore the failure of the first compilation (compilation should not happen anyway).
So the important piece here is <failOnError>false</failOnError>
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
<execution>
<id>process-annotations</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<failOnError>false</failOnError>
<compilerArgs>
<arg>-proc:only</arg>
<arg>-implicit:none</arg>
<arg>-processor</arg>
<arg>com.github.tivrfoa.mapresultset.MappingProcessor</arg>
</compilerArgs>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<compilerArgs>
<arg>-proc:none</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
</plugin>
It's not exactly the solution that I wanted, but it does the job.
I had to this way because javac
is not respecting -proc:only
But maybe I'm missing something. Let's hope someone has a better answer. =)
CodePudding user response:
the "modern" way to plug annotation processors in ~recent (>5 years old) jdk and maven is following:
<dependencies>
...
<dependency>
<groupId>some-group</groupId>
<artifactId>some-processor</artifactId>
<version>${version}</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
i.e. just add annotation processor as a dependency with scope=provided
and remove outdated configurations.