Configuring Maven to use standalone aspects


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.


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.


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. 


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

This Post Has 4 Comments

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

      1. 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

Close Menu