Search code examples
maven-2aspectj

AspectJ weaving maven modules


I have a project that has multiple maven modules, one of which, contains my aspects. How can I take the aspects and weave multiple maven modules? The documentation for the AspectJ Maven plugin is a little sparse and haven't been able to find many examples.

I have tried putting the aspectj plugin in the parent pom but it doesn't seem to apply the advice for the modules underneath it.

I also tried specifying the aspectsDirectory property but it didn't seem to have any affect. Perhaps I did it wrong?


Solution

  • I think the mechanism is explained pretty well on this page:

    Using Aspect Libraries

    Basically:

    You put all your aspects in one project, compile it using the aspectj-maven-plugin, add a dependency to this project to all projects you want to weave and also add this config to the woven projects:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.3</version>
        <configuration>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>your.aspects.groupId</groupId>
                    <artifactId>your.aspects.artifactId</artifactId>
                </aspectLibrary>
            </aspectLibraries>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    The ugly part is: you need to add this configuration to every project, so one possibility would be to use a parent pom. Unfortunately, it would have to be at the same level as the aspects project (because a pom can't define a dependency to a project below it), so you'd have something like this

        ------- root --------
       /                     \
    aspects   -------- java-parent ----
             /     /     |      |      \
          java1  java2  java3  java4  java5
    

    or even like this

           ------- root ---------
          /                       \
    aspect-parent            --- java-parent ---
      /        \            /     |      |      \
    aspects1 aspects2     java1  java2  java3  java4 
    

    You'd add both the <dependency> and the aspectj plugin configuration to the pom of the java-parent project