Appendix C. Using Maven with AspectJ

 

The previous appendix examines various weaving options enabled through Ant. In this appendix, you perform the same tasks using Maven.[1] As in appendix B, you’ll use the classes from listings 2.1 and 2.2 and the profiling aspect from listing 2.4 to exercise various weaving possibilities.

1 Please use Maven 2.2.0 or later. I’ve encountered a few problems with older versions.

Weaving sources

Maven, through the open source codehaus.org project, provides a plugin to work with AspectJ (visit http://mojo.codehaus.org/aspectj-maven-plugin for details). Let’s see this plugin in action through the Maven pom.xml file in listing C.1.

Listing C.1. Maven pom.xml to perform source code build-time weaving
The pom.xml file declares a dependency on aspectrt.jar. It adds the Maven AspectJ plugin to participate in the compile phase. It also includes a plugin to let you run the ajia.main.Main class.

When you execute the build file to install the compiled sources and execute the main class, you get output similar to the following:

> mvn install exec:java
...
[INFO] [aspectj:compile {execution: default}]
...
Wanna learn AspectJ?
void ajia.messaging.MessageCommunicator.deliver(String) took 145549
     nanoseconds
Harry, having fun?
void ajia.messaging.MessageCommunicator.deliver(String, String)
   took 79619 nanoseconds
void ajia.main.Main.main(String[]) took 2176533 nanoseconds

Next, you’ll create an AspectJ library.

Creating an aspect library

Weaving into jar files