I’m currently using AspectJ alongside Spring AOP in my Maven project. I want to enable compile-time weaving for aspects that are provided by a dependency (my-aspect-library) while retaining Spring’s proxy-based AOP for the aspects defined within my own project.
Here’s my scenario:
I have a dependency my-aspect-library with a @Aspect
like:
@Aspect
public class MyAspect {
@Around("execution(@com.example.MyAnnotation * *(..))")
public Object execute(ProceedingJoinPoint jp) throws Throwable {
// some handling
Object res = jp.proceed();
// some handling
return res;
}
}
I’m using aspectj-maven-plugin
with aspectLibrary
to enable compile-time weaving for only MyAspect
.
But during the build, I noticed the plugin is also weaving my local Spring AOP aspects (A, B, C) — which I want to avoid. Just to clarify: I see statements like
Join Point 'method-excecution(java.util.List X.myMethod(String) in Type 'X') advised by around advice from 'com/company/prod//aspects/A (from A.java)'
So here the A is Spring AOP aspect but that is also weaving its advices into classes .
My Question:
How can I configure aspectj-maven-plugin
to weave only the aspects from my dependency’s JAR and ignore the local aspects in my project?
Plugin added in my project
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<executions>
<execution>
<id>weave-main-classes</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<complianceLevel>11</complianceLevel>
<source>11</source>
<target>11</target>
<showWeaveInfo>true</showWeaveInfo>
<forceAjcCompile>true</forceAjcCompile>
<weaveDirectories>
<weaveDirectory>${project.basedir}/target/classes</weaveDirectory>
</weaveDirectories>
<aspectLibraries>
<aspectLibrary>
<groupId>com.example.group</groupId>
<artifactId>my-aspect-library</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
How can I configure aspectj-maven-plugin to weave only the aspects from my dependency’s JAR and ignore the local aspects in my project?
You basically have three options:
Use <xmlConfigured>
in your module, see also the corresponding Ajc parameter -xmlConfigured
.
If you happen to use introductions, the Spring AOP equivalent to AspectJ's inter-type declarations (ITDs), AspectJ XML configuration does not apply to them. In that case, simply move your Spring AOP aspects to a separate module and let your original module depend on it to make sure that the AspectJ compiler via AJ Maven does not see the Spring AOP aspect module's byte code. You can also do this in simpler cases, if you just want a cleaner solution.
If however your native AspectJ aspect happens to target the Spring AOP aspects and the latter use introductions at the same time, no. 2 is not an option. In this corner case, you would need either need a custom configuration tailored to your specific situation, or you could simply switch to LTW (load-time weaving) for AspectJ instead of using AJ Maven plugin. LTW would, of course, also work in the simpler cases, if compile-time weaving is not a must for you.
Probably, option 1 will suffice, unless you have rather unusual Spring AOP aspects.