Configuring Maven to use standalone aspects

Index

This post is part of an series of posts about AspectJ and AOP.

Configuring Maven to use standalone aspects

The next natural step is to separate the aspects into their own library. While doing this we can easily create generic aspects that can be reused, either by using patterns or annotations. Our goal is to create a small standalone application that uses our standalone aspect library; the application should be able to run by typing java -jar example-sample-project-1.0-SNAPSHOT.jar.

Description

The build procedure for our projects includes a normal compile step and then another step to weave the aspects into our code. The community has created plugins for our build automation systems  to help us with the weaving of our aspects. We are using Maven as our build automation system and to weave our aspects we are using a plugin called  aspectj-maven-plugin.

You can get the code for this blog series at the Git repository here.

Let’s start coding!

Requirements: Maven, Java 8, (preferably) an IDE (I’m using IntelliJ)

We have separated the code into two projects called example-sample-project and annotations-element-value-pair-without-main-class.

Let’s look at the pom.xml for the example-sample-project and the configuration needed for the  aspectj-maven-plugin.

The <version>1.7</version>  is the latest version for now and it supports up to Java 8. The complianceLevel, source and target all specify that we want the code to be built for Java 8. There are a relationship between the dependencies and aspectLibraries  where each entry in the aspectLibraries  needs to be defined as an entry in the dependencies , only difference is that the aspectLibrary don’t contain a <version> . The executions  tag contains the <goals> with the  <goal>compile</goal>  entry. By adding <goal>test-compile</goal> entry in the <goals> we enable the weaving/compiling of our aspects into our test-cases as well.

Next step is to assemble the whole project into a self sustained jar that we can run. For that task we use the maven-assembly-plugin. We specify the our <mainclass>  to be added to the <manifest> and the <goal>single</goal>  so it will assemble everything into a single jar that’s executable. The next step is to build the application with mvn clean install  command and then run the application.

Once we run the application the following result will be printed to our console.

Conclusion

By using the aspectj-maven-plugin we can easily weave/compile our external aspect project into our own project. By adding the maven-assembly-plugin we can create an independent application that has all the dependencies that are needed for it to run. 

Index

This post is part of an series of posts about AspectJ and AOP.

4 Comments

  1. David Karlsen

    We just released version 1.8 of the plugin: http://search.maven.org/#artifactdetails%7Corg.codehaus.mojo%7Caspectj-maven-plugin%7C1.8%7Cmaven-plugin
    and you find the updated documentation here: http://www.mojohaus.org/aspectj-maven-plugin/
    The plugin moved from codehaus to github under the mojohaus umbrella: https://github.com/mojohaus/aspectj-maven-plugin

  2. Ihsan

    Hello

    I followed your instruction to run AspectJ in Maven but AspectJ did not catch any method it supposed to catch. The code you can see on http://stackoverflow.com/questions/36180953/using-aspectj-without-spring-on-maven. Do you have any idea how to resolve this?

    Thanks

    • Steve Widinghoff

      Hi.
      What i can see there is some problem with your POM, i have posted a reply on stackoverflow with my POM.

      • Ihsan

        Hello

        I tried using your POM configuration but it still does not work. Do you have any idea why? Do you think I need to convert the normal Maven project to Maven AspectJ project using Right Click – Configure – Convert to AspectJ Project?

Leave a Reply