Applying AspectJ advice to classes allows logic to be indirectly applied to target classes without direct knowledge. This is especially useful when working with libraries that are not directly part of a project, yet advice still needs to be applied.
In this recipe we will cover how to add AspectJ advice to Jar libraries using compile-time weaving.
In order to weave AspectJ advice at compile-time, we need to introduce the AspectJ compiler to compile aspect advice into the target library. Using the aspectj-maven-plugin
, we can execute this compile task during one of the Maven build lifecycle phases. We compile the source aspects during the compile phase, and then compile the test aspects during the test-compile phase.
In order to weave advice into an existing library, the library or libraries need to be added to the plugin as a <weaveDependency>
as depicted in the following listing:
<plugin> <groupId>org.codehaus.mojo...