The Holy Java

Building the right thing, building it right, fast

Compiling with AspectJ’s ajc compiler from Maven

Posted by Jakub Holý on December 10, 2009

I needed to compile AspectJ classes (.java with annotations) with ajc and I wanted to have that integrated into the Maven build process. I tried several means, finally finding the one that worked.

Note: I had to use ajc even though I was using the pure java 5 syntax based on annotations instead of the legacy AspectJ syntax due to bug in AspectJ (fixed in v. 1.6.6).

Failed Attempts

aspectj-maven-plugin v1.2

First I tried the AspectJ Maven plugin but it didn’t work for me because I had a special need – a project containing only an aspect while the plugin requires, if I remember correctly, also the advised sources to be in the project. A fix is available and should be included in the version 1.3 but the project doesn’t seem to be very active so who knows when it will be released. See MASPECTJ-7.

maven-compiler-plugin for aspectj

Next I tried the maven-compiler-plugin, which supports various back-ends including AspectJ with the dependency on plexus-compiler-aspectj and compilerId set to aspectj. Unfortunately the plexus-compiler-aspectj is quite out of date, supporting only AspectJ 1.3 while I needed 1.6.

Finally the Success: maven-antrun-plugin

Being failed by Maven plugins, I had to resort to the standard way of running ajc via Ant. Fortunately Maven has a very good integration of Ant and its tasks.

The only problem here was that Ant requires ${java.home} to point to JDK (and not JRE) to find javac. It doesn’t help to set the envrionmental variable JAVA_HOME to point to the JDK because Maven resets it to $JAVA_HOME/jre in the case – see MANTRUN-91. The solution is to add tools.jar (which includes javac classes) to maven-antrun-plugin’s dependencies.

The corresponding piece of pom.xml:

    <!-- Tell javac not to compile sources for antrun will do it -->

        <artifactId>aspectjtools</artifactId> <!-- apectj ant plugin -->

            <echo>AntRun: Compiling AspectJ classes with ajc, java.home=${java.home}</echo>
              classpathref="maven.plugin.classpath" />

              Xlint="ignore" >

              <exclude name="eu/ibacz/pbns/util/aspect/"/>


Note: maven-antrun-plugin v1.3 uses Ant 1.7.1 – see


It’s a bit tricky but very well possible to run AspectJ compiler instead of javac during a maven build. The most flexible way is to use the AspectJ Ant task and maven-antrun-plugin though in more standard cases the aspectj-maven-plugin can serve you well too.

About these ads

One Response to “Compiling with AspectJ’s ajc compiler from Maven”

  1. ginko said

    I have read the article based on the Java coding and its success rate for making the interactive plug-ins services.Its true that the most flexible way is to use the AspectJ Ant task and maven-antrun-plugin though in more standard cases the aspectj-maven-plugin can serve you well too.In the field of the coding,best method to adopt is Try and error method in which after correcting the bugs and defect, the final product is publish.

Sorry, the comment form is closed at this time.

%d bloggers like this: