Monday 7 February 2011

Using Ant Properties

In this Ant tutorial installment, we will use ant Properties to specify the work directory structure that will contain our .class and .jar files. The work directory structure tends to be a personal preference, and properties will allow us to centralize the structure definition so that any change will be relatively pain-free. We specify the directories with a location attribute, thereby binding the property to a file system location. This allows us to pass the property to another ant process without any local directory side effects.

<project default="all">
<property name="obj-dir" location="obj" />
<property name="lib-dir" location="lib" />

<target name="init">
<mkdir dir="${obj-dir}" />
<mkdir dir="${lib-dir}" />
</target>
<target name="clean-init">
<delete dir="${obj-dir}" />
<delete dir="${lib-dir}" />
</target>

<target name="all" depends="init"/>
<target name="clean" depends="clean-init"/>
</project>
This build file features:
  • 4 targets:
    1. init: to build the work directory structure.
    2. clean-init: to remove the work directory structure.
    3. all: the build roll-up target.
    4. clean: to clean roll-up target.
    5. Note that my target naming convention is to pair a target with a clean-target. I reserve the all and the clean targets as roll-up targets for all build and clean targets respectively.
  • 2 properties:
    1. obj-dir: the root directory for our .class files.
    2. lib-dir: the root directory for our .jar files.
We can now execute the init target to create the work directories, and the clean-init target to remove the work directories.

$ ls
build.xml
$ ant
Buildfile: build.xml

init:
[mkdir] Created dir: /Tutorial/Ant/Properties/obj
[mkdir] Created dir: /Tutorial/Ant/Properties/lib

all:

BUILD SUCCESSFUL
Total time: 3 seconds
$ ls
build.xml obj/ lib/
$ ant clean
Buildfile: build.xml

clean-init:
[delete] Deleting directory /Tutorial/Ant/Properties/obj
[delete] Deleting directory /Tutorial/Ant/Properties/lib

clean:

BUILD SUCCESSFUL
Total time: 2 seconds
$ ls
build.xml
Now, let's add targets to compile, jar and execute a java class. We'll just copy over the hello.java class from our previous installment and place it in the src directory. The compile and jar targets from before remain largely the same; but we add the destdir attribute to the javac task to specify where the .class files are to be stored and we can remove the includes attribute from the jar task. Our new build.xml looks like this:

<project default="all">
<property name="obj-dir" location="obj" />
<property name="lib-dir" location="lib" />
<property name="src-dir" location="src" />

<target name="init">
<mkdir dir="${obj-dir}" />
<mkdir dir="${lib-dir}" />
</target>
<target name="clean-init">
<delete dir="${obj-dir}" />
<delete dir="${lib-dir}" />
</target>

<target name="compile" depends="init">
<javac srcdir="${src-dir}"
destdir="${obj-dir}"
/>
</target>
<target name="clean-compile">
<delete>
<fileset dir="${obj-dir}" includes="**/*.class" />
</delete>
</target>

<target name="jar" depends="compile">
<jar destfile="${lib-dir}/hello.jar"
basedir="${obj-dir}"
/>
</target>

<target name="clean-jar">
<delete file="${lib-dir}/hello.jar" />
</target>
<target name="run" depends="jar">
<java classname="hello"
classpath="${lib-dir}/hello.jar"
fork="true"
/>
</target>


<target name="all" depends="run"/>

<target name="clean" depends="clean-init"/>
</project>
And now we can execute our class and clean up our work directories.

$ ant run
Buildfile: build.xml

init:
[mkdir] Created dir: /Tutorial/Ant/Properties/obj
[mkdir] Created dir: /Tutorial/Ant/Properties/lib

compile:
[javac] Compiling 1 source file to /Tutorial/Ant/Properties/obj

jar:
[jar] Building jar: /Tutorial/Ant/Properties/lib/hello.jar

run:
[java] Hello World

BUILD SUCCESSFUL
Total time: 4 seconds
$ ant clean
Buildfile: build.xml

clean-init:
[delete] Deleting directory /Tutorial/Ant/Properties/obj
[delete] Deleting directory /Tutorial/Ant/Properties/lib

clean:

BUILD SUCCESSFUL
Total time: 2 seconds

No comments:

Post a Comment