Snippet: AspectJ and Ant

· apsectj ant

I've just started working with AspectJ and despite its non-intuitive syntax, I'm pretty sold on its effectiveness at reducing development time and complexity by factoring out crosscutting concerns such as logging and security. However, the Internet is a little lacking in guides and how to build it, so here's my how to.

1) Download AspectJ tools jar and save this into your ~/.ant/lib directory.

2) Add the XML namespace to the project element:

Create a new compile target in your build.xml (you can see that I use Ivy for dependency management):

<aspectj:iajc source="1.6" destDir="${build}">
	<sourceroots>
		<pathelement location="${src}" />
	</sourceroots>
	<classpath>
		<fileset dir="${lib}">
			<include name="**/*.jar" />
		</fileset>
	</classpath>
</aspectj:iajc>

Note: you may need to tell AspectJ to use target Java 6.

3) Factor out the javac element into it's own "compile.javac" task, this will allow you to revert to javac easily:

<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
	<classpath>
		<fileset dir="${lib}">
			<include name="**/*.jar" />
		</fileset>
	</classpath>
</javac>

4) Make the "compile" target dependent on "compile.aspectj";

<copy todir="${build}">
	<fileset dir="${src}" excludes="**/*.java" />
</copy>

5) Build it.

You might want to also download the AspectJ runtime in your build (e.g. using Ivy).