问题
I have searched far and wide and have not found anything that would help me resolve my problem. I am trying to implement a certain functionality and I am creating a PoC solution, here is the issue though:
it involves using both compile time weaved AspectJ and compile time annotation processors
and I have no idea how to make both of those work at the same time.
So far I have been using just AspectJ aspects in *.aj files with the aspectj-maven-plugin, and it was working fine. As soon as I tried adding annotation processing with a maven-compiler-plugin though, an issue became aparent: the weaved classes in the target directory generated in the compilation phase by the aspectj-maven-plugin were overwritten by the maven-compiler-plugin by the classes it generated during it's compilation phase.
The console output says that first the aspectj-maven-plugin does it's work and weaves all the classes:
[INFO] --- aspectj-maven-plugin:1.10:compile (default) @ demo ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
...and a list of all the Join Points and Intertypes done
But then the maven-compiler-plugin goes to work and recompiles everything (and generates classes with the annotation processor):
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to dir\demo\target\classes
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (compile-project) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 19 source files to dir\demo\target\classes
Here is the relevant part of the pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>
path/to/annotation/processor/Processor.java
</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-project</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.10</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<complianceLevel>${maven.compiler.source}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
Separately, both the aspects and the annotation processors work fine, so they specifically aren't the problem, I assume the issue is the build configuration.
I'm guessing that either I am messing something up with the executions/phases in the plugins, or maybe I don't even need to use both of them, maybe just one properly conifgured would be enough, but I have no idea how to combine compilation with ajc and running the annotation processor (and in two phases, first with proc none to compile the Annotation Processor itself and then to do the annotation processing). Please help!
回答1:
For me the best strategy so far was to disable the Java compiler altogether when using the AspectJ compiler, and let the AspectJ compiler do the annotation processing and the compilation alone. An example maven build configuration would look like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<skipMain>true</skipMain>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If that doesn't work for you for some reason, you could try using the Java compiler for annotation processing only and let the AspectJ compiler do the rest.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<proc>only</proc>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<proc>none</proc>
</configuration>
</plugin>
</plugins>
</build>
I also see you've got too many executions defined in your build file (2 for the java compiler: default-compile
+ compile-project
), with non-default execution IDs. I suggest you leave only the default executions with their default ID, unless you have a specific reason for having them.
来源:https://stackoverflow.com/questions/47290859/using-aspectj-compile-time-weaving-and-java-compile-time-annotation-processing-i