Monday, November 12, 2012

AspectJ with Maven and Eclipse Juno

This post will discuss how to use AspectJ with an already existing Maven project in Eclipse.  
First you need to start by downloading AJDT (AspectJ Development Tools) for Eclipse.  

Install AJDT As of this writing, the latest AJDT plugin can be found at:
http://download.eclipse.org/tools/ajdt/42/update.

Convert to AspectJ Project
After downloading and installing the plugin you will need to convert your project to an AspectJ project.  To do so, right click your project in the project explorer and under "Configure" select "Convert to AspectJ Project".  This doesn't stop it from being a Maven project, it simply adds AspectJ support to your project.  

Note: You can remove AspectJ support from your project by right clicking it in the project explorer, selecting "AspectJ Tools" and selecting "Remove AspectJ Capability".

Configure Where Aspects Are Stored
Aspects are by convention in src/main/aspect, but you can configure the plugin to look for aspects by specifying the paths within a .ajproperties file or via your project properties in the "AspectJ Build" pane under "Aspect Path".

Creating a Property File
By convention the property file should be located at your project root, but you can locate it wherever you want if you tell the plugin where to look for it.  Within your pom.xml an entry needs to be added to tell the plugin where to look for it.  Inside the configuration section of the aspectj-maven-plugin add an entry like the following:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifcatId>aspectj-maven-plugin</artifactId>
  <configuration>
    <options>
        ...
      <ajdtBuilderDefFile>src/main/resources</ajdtBuilderDefFile>
    </options>
  </configuration>
  ...
</plugin>

Weaving into 3rd Party Jars
If you are going to be weaving an aspect into a 3rd party class located in a jar file, you will need to add that jar file to your InPath.  Go to your project properties and go to the "AspectJ Build" pane.  There is an InPath tab which allows you to add any jar files you want your aspects woven with.

Configure pom.xml with AspectJ Configuration for Portability
The above is fine if you are the only one developing aspects for the project, however all of the configuration is within the plugin within the IDE so every person wanting to develop aspects for the project will have to configure it.  While developers should still install the AJDT plugin, they won't have to configure the IDE if you put the configuration within the .ajproperties file and pom.xml.  

Add AspectJ Dependency to pom.xml
Within the dependencies section of the pom.xml, add the following snippet:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.11</version>
</dependency>

Add AspectJ Maven Plugin Build to pom.xml
Within the build section of your pom.xml add the following snippet:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <version>1.4</version>
  <configuration>
    <weaveDependencies>
      <weaveDependency>
        <groupId>groupId of jar</groupId>
       <artifactId>name of jar/artifact id</artifactId>
      </weaveDependency>
    </weaveDependencies>
    <ajdtBuildDefFile>build.ajproperties</ajdtBuildDefFile>
  </configuration>
  <executions>
    <execution>
      <phase>process-source</phase>
      <goals>
        <goal>compile</goal>
      </goals>
    </execution>
  </executions>
</plugin>  

The weave dependency specifies the resources that should be woven with your aspects.  The ajdtBuildDefFile property tells the plugin where to look for the property file containing the directories to include with aspect sources and which ones to exclude.

This was mainly written from memory so if you have any issues or find any missing pieces please let me know.

No comments:

Post a Comment