Fork me on GitHub

Usage

Basic usage

This plugin weaves AspectJ aspects into your classes using the AspectJ compiler ajc.

There are typically two ways to package and use aspects in your Maven reactors: either as a stand-alone project where aspects and code are defined within the same Maven project or as part of a multi-module Maven reactor where one or more modules contain aspects and other modules within the Maven reactor contain code affected ("woven") by those aspects.

Below follows a standard single-project usage example.

<project>
  <!-- ... -->
  <dependencies>
    <!-- ... -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectjVersion}</version>
    </dependency>
    <!-- ... -->
  </dependencies>
  <!-- ... -->
  <build>
    <plugins>
      <plugin>
        <groupId>${project.groupId}</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>${project.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- ... -->
    </plugins>
  </build>
  <!-- ... -->
</project>

Upgrading or downgrading AspectJ

This plugin’s release frequency is lower than AspectJ’s. Therefore, if for example

  • you wish to upgrade to a more recent AspectJ version, or

  • you need to run your build on JDK 8 or 11 instead of JDK 17 and wish to downgrade to an AspectJ compiler running on that platform,

you need to adjust the aspectjtools compiler dependency within the plugin and ideally also the aspectjrt runtime used by the compiled module in sync with it.

<project>
  <!-- ... -->
  <properties>
    <!-- Your favourite AspectJ version -->
    <aspectj.version>1.9.21</aspectj.version>
  </properties>

  <dependencies>
    <!-- ... -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <!-- AspectJ runtime version, in sync with compiler -->
      <version>${aspectj.version}</version>
    </dependency>
    <!-- ... -->
  </dependencies>
  <!-- ... -->
  <build>
    <plugins>
      <plugin>
        <groupId>${project.groupId}</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>${project.version}</version>
        <dependencies>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <!-- AspectJ compiler version, in sync with runtime -->
            <version>${aspectj.version}</version>
          </dependency>
        </dependencies>
        <configuration>
          <!-- Your favourite Java source/target version -->
          <complianceLevel>21</complianceLevel>
        </configuration>
      </plugin>
      <!-- ... -->
    </plugins>
  </build>
  <!-- ... -->
</project>