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):

aspectjcompiler=required

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”/>
</depend>
<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”/>
</nb-javac>
<copy todir=”${build.classes.dir}”>
<fileset dir=”${src.dir}” excludes=”${jar-excludes}”/>
</copy>
</target>

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”/>
</depend>
<antcall target=”compile-nb-javac” inheritAll=”true” />
<antcall target=”compile-aspectj” inheritAll=”true” />
</target>

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”>
<classpath>
<pathelement path=”${aspectjtools.jar}”/>
</classpath>
</taskdef>
<iajc destdir=”${build.classes.dir}” source=”${javac.source}” fork=”true”
forkclasspath=”${aspectjtools.jar}” classpath=”${aspectjrt.jar};${cpProperty}”
failonerror=”false” >
<sourceroots>
<pathelement location=”${src.dir}”/>
</sourceroots>
</iajc>
</target>

Conclusion

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.
 

Do you want to know how to develop your skillset to 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!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× 6 = fifty four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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:
Close