Apache Maven 1.x has reached its end of life, and is no longer supported. For more information, see the announcement. Users are encouraged to migrate to the current version of Apache Maven.

Maven AspectJ Plug-in

AspectJ plugin for Maven. It offers the ability to weave aspects on the classes generated and dependency libraries. This also includes the ability to add dependencies on libraries with aspects.

For more information on the functionality provided by this plugin, please see the Goals document.

For more information on how to customise the functionality provided by this plugin, please see the properties document.

Installing

To install or update the plugin do the following:

maven plugin:download -DgroupId=maven -DartifactId=maven-aspectj-plugin -Dversion=<version>

Compiling aspect sources

You must add the property aspectSourceDirectory in your project.xml

          <aspectSourceDirectory>src/aspectj</aspectSourceDirectory>

Using aspects from other libraries

You must tell Maven what aspects you want to weave project classes with in addition to the source aspects in your project. This is achieved by specifying meta-information in the POM, using the aspectj.weaveWith property

          <dependency>
            <groupId>groupid</groupId>
            <artifactId>jarid</artifactId>
            <version>jarversion</version>
            <properties>
              <aspectj.weaveWith>true</aspectj.weaveWith>
            </properties>
          </dependency>

Weaving classes from other libraries

You must tell Maven what libraries you want to weave with project aspects or aspect libraries. This is achieved by specifying meta-information in the POM, using the aspectj.weaveInto property

          <dependency>
            <groupId>groupid</groupId>
            <artifactId>jarid</artifactId>
            <version>jarversion</version>
            <properties>
                <aspectj.weaveInto>true</aspectj.weaveInto>
            </properties>
          </dependency>

The jar contents, with classes weaved, are extracted to maven.build.dest.

Creating a deployable file

If you are creating a deployable file like a war you need to add the aspectjrt library to your dependencies so it gets included in the file. You shouldn't need to add it for other operations.

           <dependency>
             <groupId>aspectj</groupId>
             <artifactId>aspectjrt</artifactId>
             <version>1.2</version>
             <url>http://www.eclipse.org/aspectj</url>
             <properties>
                <war.bundle>true</war.bundle>
             </properties>
           </dependency>

Calling AspectJ goal automatically

If you want to call the aspectj goal every time you compile add the following to your maven.xml file.

           <preGoal name="java:compile">
             <attainGoal name="aspectj"/>
           </preGoal>

If you don't want to weave test classes use the following code instead.

           <preGoal name="java:compile">
             <attainGoal name="aspectj:compile"/>
           </preGoal>

Sample project

<?xml version="1.0" encoding="UTF-8"?>

<project>

    <pomVersion>3</pomVersion>
    <groupId>examples</groupId>
    <id>example-with-aspects</id>
    <currentVersion>1.0</currentVersion>

    <dependencies>
        <dependency>
            <groupId>company-jars</groupId>
            <artifactId>jar-one</artifactId>
            <version>1.0</version>
            <properties>
                <war.bundle>true</war.bundle>
            </properties>
        </dependency>
        <dependency>
            <groupId>company-aspects</groupId>
            <artifactId>example-aspects</artifactId>
            <version>1.0</version>
            <properties>
                <aspectj.weaveWith>true</aspectj.weaveWith>
            </properties>
        </dependency>
        <dependency>
            <groupId>company-jars</groupId>
            <artifactId>jar-two</artifactId>
            <version>1.0</version>
            <properties>
                <aspectj.weaveInto>true</aspectj.weaveInto>
            </properties>
        </dependency>

        <!-- if you are creating a deployable file like a war you
             need to add the aspectjrt library -->

        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.2</version>
            <url>http://www.eclipse.org/aspectj</url>
            <properties>
                <war.bundle>true</war.bundle>
            </properties>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src/java</sourceDirectory>
        <aspectSourceDirectory>src/aspectj</aspectSourceDirectory>
    </build>

</project>