Fork me on GitHub

Weaving classes in directories

Weaving already compiled classes

AspectJ can weave already compiled classes inside folder or JAR. To do this with AspectJ Maven Plugin, you need to specify the folder(s) with the compiled classes via weaveDirectories and execute the plugin after maven-compiler-plugin, i.e.

  • either the two plugins must both appear in chronological order in the POM, if both plugins are to be run in their default phases compile and test-compile,

  • or you select later phases for AspectJ Maven Plugin, i.e. process-classes and process-test-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>${project.groupId}</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>