Apache Ant is a command line Build tool used to build java applications. Ant supports number of built-in tasks to compile, run java applications…etc. Every action should be in the form of tasks embedded in targets, one target can depend on other targets.

Installing Apache Ant
  • The latest  version is available from Ant binary download page http://ant.apache.org/bindownload.cgi
  • Download .zip file and extract to a directory
  • Set the environment variable ANT_HOME to the directory
    • Open System properties by right clicking on My computer and select properties
      • System properties window can also be opened directly from Run window by executing the command sysdm.cpl
    • Click Advanced system settings –> open Advanced  tab –> environment variables

ANT_HOME environment variable setup

  • Set the JAVA_HOME environment variable, set to the directory where JDK had been installed.
  • Add ANT bin directory(%ANT_HOME%\bin) to “Path” variable.
VERIFY Apache Ant Installation
  • Open command  prompt, use command ant -version to see the version details. It should give an output like this
Apache Ant(TM) version 1.9.4 compiled on April 29 2014

Note: If this does not work make sure your environment variables are set to correct one.

Apache Ant Build file
  • Ant build files are written in XML
  • Build file contains one project and at least one target.
  • Target is a sequence of tasks to be executed to perform build process, targets can depends on other targets.
<target name="A">
  ...
</target>
<target name="B" depends="A>
  ...
</target>
  • Task represents real work, Ant contains lots of built-in tasks that can help to do all type of things, like create directories, copy files, compile java code, create JAR file, run java file..etc.

Here is the task to compile java code. This task compiles all java classes that are in source directory and then places all .class files into destination directory.

<target name="compile"> 
 <javac srcdir="${src.dir}" destdir="${classes.dir}" fork="true"/>
</target>
  • Ant properties are key, value pairs configured inside the Ant build script, which in turn used to refer multiple times. For instance, the source directory of your Java code, or the build output directory.
<property name="src.dir" value="${basedir}/src" />
  • If java code uses some other third party libraries, to compile code we need to provide those dependent libraries. Hence should mention dependent libraries in class path.
    To get this, you can use <path> element with resource collection <fileset>

    • FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns.
<path id="classpath">
   <fileset dir="${lib.dir}">
     <include name="*.jar"/>
   </fileset>
   <pathelement path="${classes.dir}"/>
 </path>

 You can use path id “classpath” while compiling and executing java code by using <classpath refid=”classpath”/>

Build file example

AntTest is a Java project, it has TestBuild class, using Log4j for logging purpose.  To compile and execute Java code, use build.xml

Project structure:

java-project-for-ant-build-test

build.xml file

  • It has targets – exec, compile, init
  • exec target — executes the java code, it depends on compile target.
  • compile target — compiles the java code, it depends on init target.
  • init target — deletes the bin directory and creates bin directory for storing class files.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project name="AntTest" default="exec" basedir=".">
    <property name="classes.dir" value="${basedir}/bin" />
    <property name="src.dir" value="${basedir}/src" />
    <property name="lib.dir" value="${basedir}/lib"/>

    <path id="classpath">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${classes.dir}"/>
    </path>

    <target name="init">
        <delete dir="${classes.dir}" />
        <mkdir dir="${classes.dir}"/>
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}"        fork="true">
            <classpath refid="classpath"/>
        </javac>
    </target>

    <target name="exec" depends="compile">
        <java classname="com.exploreselenium.TestBuild"  fork="true">
            <classpath refid="classpath"/>
        </java>
    </target>

</project>
Run Ant targets from command line
  • Open command line, navigate to the path where build.xml is located.
  • Run command  ant <target name>
    • ant exec  
  • Once this target is executed, it deletes existing bin directory and compiles the java code and execute the program.

Here is the output:

execution of apache ant targets in build file

As a summary, we learned about Ant tool and its details.

Continuous Delivery(CD) via TeamCity and Jenkins
0