Packaging with ANT

Supun Dharmarathne
technodyne
Published in
2 min readMar 11, 2012

Suppose we need to create the following folder structure in our java application.

+---src
+---draw
-Triangle.java
+---test
-TestDraw.java
+---build
+---draw
-Triangle.class
+---test
-TestDraw.class
+---docs
+---dist
-technodyne.ant.test.jar

For this we can use ant. I create a build.xml file like this.

[sourcecode language=”xml”]
<pre><?xml version=”1.0" encoding=”UTF-8"?>
<project name=”Ant-test” default=”main” basedir=”.”>

<property name=”src.dir” location=”src”/>
<property name=”build.dir” location=”build”/>
<property name=”dist.dir” location=”dist”/>
<property name=”docs.dir” location=”docs”/>

<target name=”clean”>
<delete dir=”${build.dir}”/>
<delete dir=”${docs.dir}”/>
<delete dir=”${dist.dir}”/>
</target>

<target name=”makedir”>
<mkdir dir=”${build.dir}”/>
<mkdir dir=”${docs.dir}”/>
<mkdir dir=”${dist.dir}”/>
</target>

<target name=”compile” depends=”clean, makedir”>
<javac srcdir=”${src.dir}” destdir=”${build.dir}”>
</javac>
</target>

<target name=”docs” depends=”compile”>
<javadoc packagenames=”src” sourcepath=”${src.dir}” destdir=”${docs.dir}”>
<fileset dir=”${src.dir}”>
<include name=”**”/>
</fileset>
</javadoc>
</target>

<target name=”jar” depends=”compile”>
<jar destfile=”${dist.dir}technodyne.ant.test.jar” basedir=”${build.dir}”>
<manifest>
<attribute name=”Main-Class” value=”test.Main”/>
</manifest>
</jar>
</target>

<target name=”main” depends=”compile,jar,docs”>
<description>Main target</description>
</target>

</project></pre>
[/sourcecode]

According to the above xml, i created four directories using ANT. Before that , if there are directories with the same name, they are deleted by clean command. Then the java files in the src directory are compiled into class files and they are stored in the build directory with the same folder structure as in the src directory. Using javadoc command in ANT, the documents have generated. For that all the files (include name=”**”) in src folder is being used. Finally, the jar is created according to the content in build directory. We can define our own name for the jar file. Here is the result i got.

Buildfile: D:Eclipse_Indigo_workspacetechnodyne.antbuild.xml
clean:
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antbuild
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antdocs
[delete] Deleting directory D:Eclipse_Indigo_workspacetechnodyne.antdist
makedir:
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antbuild
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antdocs
[mkdir] Created dir: D:Eclipse_Indigo_workspacetechnodyne.antdist
compile:
[javac] D:Eclipse_Indigo_workspacetechnodyne.antbuild.xml:24: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to D:Eclipse_Indigo_workspacetechnodyne.antbuild
jar:
[jar] Building jar: D:Eclipse_Indigo_workspacetechnodyne.antdisttechnodyne.ant.test.jar
docs:
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source file D:Eclipse_Indigo_workspacetechnodyne.antsrcdrawTriangle.java...
[javadoc] Loading source file D:Eclipse_Indigo_workspacetechnodyne.antsrctestTestDraw.java...
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.6.0_27
[javadoc] Building tree for all the packages and classes...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
main:
BUILD SUCCESSFUL
Total time: 7 seconds

This is very simple and, once you find the pattern, you can do more and more things with ant. Bye till next post. :)

--

--