FiguringThisOut
FiguringThisOut

Reputation: 910

AspectJ weaving from dependency not applying to project

I have a Java 8 Maven project that defines a custom annotation and an aspect. When running test code in that project itself, it is applying the aspect to the annotated classes. I am then packaging and installing project.

I then bring in that dependency into a new project (non-Spring). The new project is then not having the aspect applied to it's classes, though it does bring in the new annotation.

How do I have a single JAR to define an annotation and aspect and have it applied to all of my projects with Maven?

Upvotes: 4

Views: 1679

Answers (1)

Nándor Előd Fekete
Nándor Előd Fekete

Reputation: 7098

You need to specify your aspect project dependency as an aspect library in your aspectj-maven-plugin configuration in your pom.xml. Let's suppose your aspect module has the groupid:artifactid groupid:aspect-module. Your pom.xml should look similar to this:

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
    <dependency>
        <groupId>groupid</groupId>
        <artifactId>aspect-module</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.9</version>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>groupid</groupId>
                        <artifactId>aspect-module</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note that I'm switching off the maven-compiler-plugin because they tend do overwrite each other's output with the aspectj-maven-plugin, and the AspectJ compiler should be able to compile normal java files and weave them in the same step anyway, so using the maven-compiler-plugin is redundant. If you are using Eclipse + AJDT, this maven configuration will much better reflect what happens in your IDE while you're developing.

Upvotes: 6

Related Questions