dev-aspectj / aspectj-maven-plugin

AspectJ Maven Plugin
MIT License
48 stars 10 forks source link

AspectJ Maven Plugin

Overview

This plugin weaves AspectJ aspects into your classes using the AspectJ compiler ajc. Typically, aspects are used in one of two ways within your Maven reactors:

Documentation

Plugin documentation for all AspectJ Maven goals and usage examples can be found here.

How to use the AspectJ and Java versions of your own choice

One of the nicest features of this plugin is that there is no need to upgrade the plugin version, if you just want to use a new AspectJ version or even a completely new Java language version. The plugin will simply pass on whatever version you set for <source>, <target> or <complianceLevel> to the AspectJ compiler. The latest supported version is not hard-coded.

As described in the plugin documentation under Upgrading or downgrading AspectJ, you simply need to set your desired AspectJ tools version - ideally in sync with the AspectJ runtime you use as a module dependency - as a plugin dependency:

<project>
  <!-- ... -->
  <properties>
    <!-- Your favourite AspectJ version -->
    <aspectj.version>1.9.22</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>dev.aspectj</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14</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>

FAQ: Why does the plugin not run on my JDK version?

The plugin as such only needs JDK 8. Therefore, if you can build on JDK 8 or need a more recent JDK version only depends on the aspectjtools plugin dependency, i.e. on the AspectJ Compiler (AJC) version. See the previous paragraph for how to configure that in Maven. In a nutshell, since AJC 1.9.8 needs JDK 11, AJC 1.9.21 needs JDK 17. This is due to the upstream Eclipse Java Compiler (ECJ) which AJC depends on, not due to AspectJ or AspectJ Maven themselves.

By the way, only AJC has those JDK version requirements. The AspectJ runtime and load time weaver still only need JDK 8, and even the latest AJC can compile to target Java 8. Please refer to the AspectJ Java version compatibility matrix for more details.

Why is there a fork of the MojoHaus plugin?

The Mojohaus project was inactive for ~3.5 years: no releases, no bugs fixed, to pull requests merged, no reaction to Nick Wong's fork (see History) or my request to make him and/or me Mojohaus committers. Therefore, I launched this fork, published the first few releases on Maven Central and asked Mojohaus to retire their version. Suddenly, someone replied and refused to retire their plugin, claiming it was never dead, and work on the plugin should continue under the Mojohaus umbrella. I agreed under the condition that all of Nick's and my own changes would be merged into the upstream version, so as not to lose any features or fixes. Some minor stuff was merged, but the review processes were tedious and some features important to me were refused altogether. So I decided to continue maintaining the AspectJ.dev version, which ever since has been more up to date with AspectJ and JDK versions and also more feature-rich.

After a short initial burst of activity and release 1.14.0 in order to catch up with a subset of the changes made to this fork, not much else has happened at MojoHaus, other than merging Dependabot changes and closing stale tickets.

Feature comparison MojoHaus vs. AspectJ.dev

As far as I know, the MojoHaus plugin does not have any features that this one here does not have. The AspectJ.dev version has the following improvements compared to MojoHaus:

History

This plugin was formerly provided by Codehaus, then migrated to Mojohaus where it was no longer maintained after version 1.11.

Because Java 9+ support was missing, Nicholas Wong (nickwongdev) forked and published it under group ID com.nickwongdev. He did this until early 2020 and version 1.12.6 containing Java 13 support. Then he announced he would no longer be available to maintain the plugin and recommended forking it again.

Presently, Alexander Kriegisch (kriegaex), who is also the AspectJ maintainer and project lead, has taken responsibility for the AspectJ.dev plugin. Future plugin releases will have the group ID dev.aspectj, hoping to give the plugin a permanent home, whoever might maintain it in the future.