javamavenaspectjaspectj-maven-plugin

Compiling with maven-compiler-plugin and aspectj-maven-plugin


When i use maven-apsectj-plugin and maven-compiler-plugin compile phase will execute both plugins compile goal. This results in compilation with javac first and then full recompilation with ajc.

Is this double compilation necessary? It seems like i can just turn maven-compiler-plugin off and everything works just fine.

I'm using "default" configuration as stated in the usage of maven-compiler-plugin:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.13</version>
    </dependency>
    ...
  </dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.11</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>

Solution

  • Yes, you could deactivate Maven Compiler plugin because the AspectJ compiler is a regularly refreshed fork of Eclipse Java Compiler. Therefore, it can also compile your Java files.

    But if the situation is more complex, e.g. you use Maven Compiler to also compile Groovy files or files in other modules and want to configure it only once in <pluginManagement>, maybe deactivating it is not such a nice option. There is a way to make both plugins play nice together, see my other answers

    Basically you configure Maven Compiler to use <useIncrementalCompilation>false</useIncrementalCompilation> and AspectJ Maven to use <phase>process-sources</phase>. More information is in the linked answers.

    Then you will see output like this:

    [INFO] --- aspectj-maven-plugin:1.12.1:compile (default) @ openbook_cleaner ---
    [INFO] Showing AJC message detail for messages of types: [error, warning, fail]
    [INFO] 
    [INFO] --- aspectj-maven-plugin:1.12.1:test-compile (default) @ openbook_cleaner ---
    [WARNING] No sources found skipping aspectJ compile
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ openbook_cleaner ---
    [INFO] Nothing to compile - all classes are up to date