Apache Ant - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ANT Structure

ANT Structure

shape Introduction

Before going to learn about ANT Structure, one has to know about the ANT Terminology. Apache ANT Structure chapter gives an overview about:
  • ANT Terminology
  • build.xml file
  • Tasks and Targets involved.

shape Description

Any build file consists of the following three basic nodes:

shape Conceptual figure

shape Example

Each build file consists of a project and atleast one default target. The following code is considered as build.xml file for this exercise: [java] <?xml version="1.0" ?> <! - -project is the outer most tag in xml - -> <project name="FirstTest" default="hello"> <target name="hello"> <echo message="Hello.Welcome to SPLessons!!!"/> </target> </project> [/java] The following output is seen when the above code is compiled and executed. [java] C:\>ant Buildfile: C:\build.xml info: [echo] Hello.Welcome to SPLessons!!! BUILD SUCCESSFUL Total time: 0 seconds C:\> [/java]

shape More Info

Target can have dependencies. Another example is shown below. Initially, create a build directory, compile Java code, place the compiler class in the build directory, and generate the executed jar file along with the MANIFEST file that is required to execute the jar file.

shape Examples

[java] <?xml version=”1.0” ?> <project name=”JarBuild” basedir=”.” Default=”jarfile”> <target name=”init” description=”Initializes properties”> <propery name=”project.name” value=”helloWorld”/> <propery name=”src.dir” value=”src”/> <propery name=”build.dir” value=”build”/> <propery name=”classes.dir” value=”$(build.dir)/classes”/> <propery name=”etc.dir” value=”etc”/> </target> <target name=”prepare” description=”Creates the build and classes directories” depends=”init”> <mkdir dir=”$(classes.dir)”/> </target> <target name=”compile” description=”Compiles the code” depends=”prepare”> <javac srcdir=”$(src.dir)” Destdir=”$(classes.dir)”/> </target> <target name=”jarfile” description=”JARs the code” depends=”compile”> <jar destfile=”$(build.dir)/$(project.name).jar” Basedir=”$(classes.dir)” Manifest=”$(etc.dir)/MANIFEST.MF”/> </target> <target name=”clean” description=”Delete the build directory” depends=”init”> <delete dir=”$(build.dir)”/> </target> </project> [/java] In the above code,
  • The default target of this project is the jar file target.But before creating the jar file, java classes have to be compiled as the jar file target depends on the compile target.
  • In order to compile the classes, the classes directory has to be created. For that the prepare target has to be called to check whether the classes directory is created or not.
  • To prepare, project properties has to be set which is done by init target.
So, even though the default target is jar file, targets are called in init - > prepare -> compile -> jar file. Here, the build directory, classes directory and manifest file once defined properly can be used anywhere in the code, which helps in code optimization. To use again, place a $ sign enclosed in brackets like $(build.dir)
  • In the prepare target, a task called mkdir(make directory) is created in directory. It can repeatedly create its own parent directory(build directory).
  • To update the classes, clean target has to be used, which will delete the current build directory. It is used only if required and called explicitly.
  • Compile target consists of javac task, which is used to run the Java C Compiler. It compares the classes timestamps with the source code.

Summary

shape Key Points

  • Project is the collection of targets, which in turn consists of tasks.
  • There will be atleast one default project.
  • The flow of compilation goes from init-prepare-compile-jar.
  • Clean target cleans the build directory.