4

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?

FiguringThisOut
  • 810
  • 2
  • 9
  • 18
  • Do you have compile time weaving enabled in your new project's build process? Or do you have load-time weaving enabled when you run your new project? – Nándor Előd Fekete May 22 '18 at 14:15
  • @NándorElődFekete I am trying compile time, the same as the original project containing the aspect itself. That project compiles and executes the aspects fine. I have the same build plugin configured on the project bringing in the aspect as a dependency, but the aspects are not being applied. – FiguringThisOut May 22 '18 at 14:32
  • Build configuration is not necessarily trivial, so please add the relevant build configuration to your question. – Nándor Előd Fekete May 22 '18 at 14:37
  • @NándorElődFekete Do you know of any examples out there where the aspect used for a build is brought in as a dependency? I feel it is a common use case, I just have been unable to search for either instructions or examples on how to weave an aspect from a dependency. – FiguringThisOut May 22 '18 at 16:35
  • Here's an [anwer](https://stackoverflow.com/a/36023217/2699901) I made earlier to an unrelated question, but it could be useful for you because it contains an example on how to specify aspect libraries in a maven build. – Nándor Előd Fekete May 22 '18 at 16:42
  • 1
    @NándorElődFekete Thank you very much! Configuring aspectLibraries was the answer. If you copy-paste your referenced info here I'll mark it as the answer. Thank you for your help. – FiguringThisOut May 22 '18 at 18:29

1 Answers1

6

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.

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47