|
Some time ago I started to research JBoss'
AOP implementation: JBossAOP. They provide tutorial and several samples (sources
and Ant files) for using their tool.
They use their own two custom tasks for compiling Java sources and jboss-aop.xml file. They _do_not_ provide any information about how to configure these tasks, nor how to use them. The samples they provide are very simple and do not cover custom source directories, for example. |
<target name="common:read_params" description="init some params">
...
<path id="compile_classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef name="aopc" classname="org.jboss.aop.ant.AopC"
classpathref="compile_classpath" />
...
The same goes for <annotationc> task:
<taskdef name="annotationc" classname="org.jboss.aop.ant.AnnotationC"
classpathref="compile_classpath"/>
...
</target>
Now we may look at our project. Assume that we have directory structure, similar to this one:
build/
classes/
... compiled classes...
tests-reports/
docs/
etc/
... configuration files..
lib/
jboss-aop.jar
... other libraries ...
src/
main/
org/
com/
... other source files
aspects/
persistence/
org/... this aspect source files
transactions/
...
build.xml
build.properties
First we need to compile all sources with normal javac compiler..
<target name="compile" depends="common:read_params"
description="Java sources compilation">
<javac
destdir="${build.classes.dir}"
debug="on"
debuglevel="lines, vars, source"
deprecation="on"
depend="on"
nowarn="on"
optimize="off"
>
<include name="**/*.java" />
<src path="${src.main.dir}" />
<src path="${src.aspects.dir}/persistence" />
<src path="${src.aspects.dir}/transactions" />
<classpath refid="compile_classpath"/>
</javac>
</target>
build.dir=build
build.classes.dir=build/classes
etc.dir=etc/
src.dir=src/
src.main.dir=src/main/
src.aspects.dir=src/aspects/
lib.dir=lib/
So here is how to tell <aopc> weaver (compiler) where are your sources, and how to compile them:
<!--
| Weave all the sources with JBossAOP compiler
-->
<target name="aopc" depends="common:read_params">
<aopc compilerclasspathref="run_classpath" classpathref="run_classpath"
verbose="false">
<classpath path="run_classpath" />
<src path="${build.classes.dir}" />
<aoppath path="${etc.dir}/jboss-aop.xml" />
</aopc>
</target>
...
<path id="run_classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${build.classes.dir}" />
</path>
...
If you want to use JDK 1.4 style annotations within your code, then you must use <annotationc> task before <aopc>, to prepare the sources. It's simple, and here is an example:
<!--
| Make annotations valid, by pre compiling them
-->
<target name="annotationc" depends="common:read_params">
<annotationc compilerclasspathref="run_classpath" classpathref="run_classpath" bytecode="true">
<src path="${src.main.dir}" />
<src path="${src.aspects.dir}/persistence" />
<src path="${src.aspects.dir}/transactions" />
</annotationc>
</target>
<target name="run"
depends="common:init,
compile,
annotationc,
aopc">
<java classname="org.nthx.pat.demo.Driver"
classpathref="run_classpath"
fork="yes" failonerror="true"
dir="${build.classes.dir}">
<classpath refid="run_classpath" />
<sysproperty key="jboss.aop.path"
value="${etc.dir}/jboss-aop.xml" />
</java>
</target>