Core Java

Integrate AspectJ with NetBeans Platform Development

Are you developing your project using the NetBeans Platform? Are you willing to use AspectJ to use AOP? You do not know how to integrate the AspectJ compiler into the builds of NetBeans?

If your answer is yes, this post is for you.

I decided to write this technical post because I have been struggling some time before to find this solution, and so I would like to share it.

Tell the story

Sometime ago I had to face the integration of AspectJ into a Rich Client Application implemented with NetBeans Platform source code. The first decision taken it was to integrate AspectJ at the level of the compilation, so to create already compiled source code that contains the AOP.

The main question was so, how to integrate this post compilation with the netbeans ant compilation files.

The solution

First of all let’s identify the files that have to be modified:

  1. common.xml: it is located in the harness folder of your NetBeans installation
  2. of the module that contains the source code that must be compiled with AspectJ

Step 1

Download the AspectJ libraries and put them in a folder located inside the “harness” folder of your NetBeans installation. Let’s say this folder is called: aspectj-x.x.x/lib.

Step 2

Go in the module that contains the source code to compile with AspectJ and add the following line in its file (in the important files):


Step 3

Now is time to configure the common.xml file. This is where actually is located the ant target called by the NetBeans IDE when the build action is launched.

This example is done using NetBeans 7.3.1 but for previous or future changes differences are few. Modifications are highlighted in blue.

Change the target compile as follows:

<target name=”compile-nb-javac” depends=”init,up-to-date” unless=”is.jar.uptodate”>
<mkdir dir=”${build.classes.dir}”/>
<depend srcdir=”${src.dir}” destdir=”${build.classes.dir}” cache=”${build.dir}/depcache”>
<classpath refid=”cp”/>
<nb-javac srcdir=”${src.dir}” destdir=”${build.classes.dir}” debug=”${build.compiler.debug}” debuglevel=”${build.compiler.debuglevel}” encoding=”UTF-8″
deprecation=”${build.compiler.deprecation}” optimize=”${build.compiler.optimize}” source=”${javac.source}” target=”${}” includeantruntime=”false”>
<classpath refid=”cp”/>
<compilerarg line=”${javac.compilerargs}”/>
<processorpath refid=”processor.cp”/>
<copy todir=”${build.classes.dir}”>
<fileset dir=”${src.dir}” excludes=”${jar-excludes}”/>

Add a new target compile as follows:

<target name=”compile” depends=”init,up-to-date” unless=”is.jar.uptodate”>
<mkdir dir=”${build.classes.dir}”/>
<depend srcdir=”${src.dir}” destdir=”${build.classes.dir}” cache=”build/depcache”>
<classpath refid=”cp”/>
<antcall target=”compile-nb-javac” inheritAll=”true” />
<antcall target=”compile-aspectj” inheritAll=”true” />

Add a new target compile-aspectj as follows:

<target name=”compile-aspectj” depends=”init,up-to-date” unless=”is.jar.uptodate” if=”aspectjcompiler”>
<property name=”cpProperty” refid=”cp”/>
<property name=”aspectj.lib.dir” location=”${harness.dir}/aspectj-x.x.x/lib”/>
<property name=”aspectjtools.jar” location=”${aspectj.lib.dir}/aspectjtools.jar”/>
<property name=”aspectjrt.jar” location=”${aspectj.lib.dir}/aspectjrt.jar”/>
<taskdef resource=”org/aspectj/tools/ant/taskdefs/”>
<pathelement path=”${aspectjtools.jar}”/>
<iajc destdir=”${build.classes.dir}” source=”${javac.source}” fork=”true”
forkclasspath=”${aspectjtools.jar}” classpath=”${aspectjrt.jar};${cpProperty}”
failonerror=”false” >
<pathelement location=”${src.dir}”/>


Now, when you will do clean and build, you will see that the source code of the chosen module is compiled against aspectj compiler right after the normal compilation.

All you have to make sure is that the aspect and the source code that have to be compiled with it are together in the same module.

Marco Di Stefano

Marco is a software engineer specialized in software architecture and process automation. He has been involved in all the software v-cycle phases and actually he works for the railway industry.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button