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

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. project.properties: 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 project.properties 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=”${javac.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/aspectjTaskdefs.properties”>
<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.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply

× five = 30

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: