Fork me on GitHub

Weaving already compiled classes

AspectJ gives a possibility to weave already compiled classes in a folder. In order to do this with Mojo's AspectJ Maven Plugin you need to specify the folder(s) with the compiled classes via weaveDirectories and execute the plugin after maven-compiler-plugin has been run (life cycle phase "process-classes" or later).

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <executions>
          <execution>
            <!-- Modifying output directory of default compile because non-weaved classes must be stored
                 in separate folder to not confuse ajc by reweaving already woven classes (which leads to
                 to ajc error message like "bad weaverState.Kind: -115") -->
            <id>default-compile</id>
            <configuration>
              <compilerArguments>
                <d>${project.build.directory}/unwoven-classes</d>
              </compilerArguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <configuration>
          <weaveDirectories>
            <weaveDirectory>${project.build.directory}/unwoven-classes</weaveDirectory>
          </weaveDirectories>
        </configuration>
        <executions>
          <execution>
            <!-- Compile and weave aspects after all classes compiled by javac -->
            <phase>process-classes</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  <build>
  ...
</project>