Control Annotation Processing for main/production and test sources separately
Is it possible to control Annotation Processing settings of a module on a source-root level? For example, to enable it for test sources only? Below is a use case of mine.
I work with a Maven project in IntelliJ IDEA. My project contains the following configuration in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<generatedTestSourcesDirectory>${project.build.directory}/generated-test-sources</generatedTestSourcesDirectory>
<annotationProcessorPaths>
<path>
<groupId>org.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<classifier>uber</classifier>
</path>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>MyProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>It disables annotation processing for the `compile` goal which compiles *main/production* sources and enables it for the *testCompile* goal which compiles test sources. Unfortunately, IDEA does not detect this configuration, resulting in the IDEA module corresponding to that Maven project defaulting to standard settings in Annotation Processors.
Putting Maven aside, I could not find a way to configure Annotation Processors separately for main and test sources of a module inside IDEA. (1)
This indicates that even if the above Maven configuration was read by IDEA, it would not have worked, due to limitation (1). That is, IDEA would have to choose only one of the executions and apply it to the whole module (both main and test sources).
Moreover, IDEA does not provide any way to selectively control which settings should not be overriden during reloading of Maven projects. And I don't want to turn off the synchronisation-with-Maven feature either since I want my Maven dependencies to stay in sync.
Therefore, I am stuck with the following 2 choices, none of which is acceptable:
1. Disable synchronisation of IDEA project settings with Maven projects.
2. Manually adjust Annotation Processing settings each time the corresponding Maven project gets reloaded.
请先登录再写评论。
I attempted simply enabling Annotation Processing for all sources in the module (both main and test), but there is another problem. More details about my use case are provided to understand it:
My Maven project contains annotation processors developed by me. I then use those same processors on test sources of my project. The problem is that IDEA tries to be smart and instead of putting the JAR into the Processor Path, it uses the output directory of my project. This makes Rebuild impossible because it first cleans the output directory and then, naturally, can't find processors there, which leads to a compilation error.
Again:
1. I have a Maven project called M. I build it and package it into a shaded JAR called SJ that can be used to run annotation processors.
2. I specify SJ in Maven configuration of M to run during compilation of test sources.
3. IDEA sees that SJ has the same Maven coordinates as M, and replace the path to SJ in my local repository with the build output directory of M (`target/classes`).
4. Rebuild of M in IDEA fails due to the build output directory beaing cleaned and no processors found there as a result. Therefore, Rebuild of M always fails.
Nullsink Does this configuration work as intended when you run it via Maven from OS Command Prompt?
Also, you've mentioned Project Rebuild, are you referring to the option from the Build Menu? If so, the build process is handled by IDEA's Native Build System, that may lead to discrepancies for more complex Maven builds. Try using options from Maven Tool Window / Run Configurations.
Roman Vatagin
My configuration works as intended when run via Maven from the command line.
My mentioning of Project Rebuild was indeed a reference to the option from the Build Menu. I understand that discrepancies may naturally arise due to there being 2 different build systems (IDEA's native one and Maven). However, the problem I discovered pertains to incorrect interpretation of Maven configuration (i.e., the problem with Processor Path). I am hoping this will be addressed in a future release.
Also, the primal issue I described pertains to a limitation of IDEA's: lack of control of Annotation Processing settings of a module on a source-root level. I am hoping this will be addressed as well.
To make my hopes come true, I have to create a ticket on YouTrack, I guess.
Nullsink Thank you for the update!
In regards to the issue with IDEA misinterpreting the Processor Path, we would really need a reproducer Project to get to the bottom of that. It can be a simple Project with minimal code, as long as it exhibits this behavior.
For the Annotation Processing settings being tied to a Module, that is one of the basic principles of IDEA's Project Structure, so that is unlikely to change. Possible alternative to that would be IDEA automatically creating extra Modules for configurations like that.
Judging by the comment, you have already seen IDEA-348161, so developers are aware of this. I do not believe any decision has been made yet, however.