Ant tutorial

How JBossAOP handle with Ant?

JBoss, JBossAOP Ant background
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.
What I want to show you here?
Defining tasks, directory structure, <javac>
First, as with any custom task, one has to define it. So:
<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" />
    ...
    
..assuming, that all the *.jar files (including jboss-aop.jar) are in the '/lib' directory.

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
    
I think (and it really helps me) that such separating of your project files, will give you bonus when you have many files which can be logically divided into many categories.
<javac> compilation first

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>
    
..with 'build.properties':
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/
    
<aopc> task

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>
    
As we see <aopc> uses the bytecode (compiled *.java files) from ${build.classes.dir} - the directory where we compiled classes to - and <javac> uses 'run_classpath' which is defined in 'common:read_params' target:
    ...
    <path id="run_classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${build.classes.dir}" />
    </path>
    ...
    
<annotationc> task
That's all about <aopc>.

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>
    
As you see, you must specify all the source directories, that where used for compilation with <javac> task.
Running
Now, if you have standalone Java application, then you may run it by using <java> tasks. Remember, though to specify path to jboss-aop.xml to JVM through system property..
    <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>
    
Sources
Download sources for this JBoss' Ant tasks tutorial
Dynamic method of running JBossAOP'ed system
There is also another method of running JBossAOP'ed system: dynamic one. You only need to compile sources with standard Java compiler and later run the main class by giving "custom classloader" to JVM. You'll get the classloader from JBossAOP distribution.

Designed with CSS| Created with GIMP| Get Firefox!| | Opel Omega Forum|

Copyright © Tomasz Nazar 2007
Revision: $Id: ant_jbossaop-tasks.gtml 3768 2005-07-25 23:47:33Z nthx $