Getting started with ant

Bootstrap Apache Ivy

Hello World

Add the following to a file named build.xml in your project directory:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld" default="main">
    <target name="main" description="this is target main">
        <echo message="Hello World" />

From a command prompt on a computer running Windows, executing ant main will display similar to the following:

$ ant main
Buildfile: C:\Users\<me>\Projects\HelloWorld\build.xml

     [echo] Hello World


Also, user can now run the command ant as default target name added to the project. When ant command is run, it looks for project’s default target and execute it.

$ ant 
Buildfile: C:\Users\<me>\Projects\HelloWorld\build.xml

     [echo] Hello World


If the build script is written by some one else and the end user like to see what target he can run, run the command which will show all the targets which has descriptions.

$ ant -p

Run JUnit

Create jar package

The following will create dist/output.jar from the source code in src and the libraries in lib, and will use src/ as the main class.

<project name="Project" default="main" basedir=".">

    <property name="src.dir"     value="src"/>
    <property name="build.dir"   value="build"/>
    <property name="dist.dir"    value="dist"/>

    <path id="classpath">
        <fileset dir="lib" includes="**/*.jar"/>
        <pathelement path="${build.dir}"/>

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

    <target name="compile">
        <mkdir dir="${build.dir}"/>
        <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"/>
        <copy todir="${build.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>

    <target name="jar" depends="compile">
        <mkdir dir="${dist.dir}"/>
        <jar destfile="${dist.dir}/${}.jar" basedir="${build.dir}">
            <fileset dir="${build.dir}"/>
                        <fileset dir="lib" includes="**/*.jar"/>
                <attribute name="Main-Class" value="Main"/>

    <target name="main" depends="clean,jar"/>

Installation or Setup

Installing Ant is very simple. Follow the steps given below to install Ant on windows platform:

  1. Download latest ant version from Apache website

  2. Unzip the file on your machine.

  3. Set ANT_HOME in environment variables

  4. Add %ANT_HOME%\bin to your PATH environment variable.


  6. Now open command prompt and enter ant command. You should see below:

    Buildfile: build.xml does not exist!
    Build failed

Alternatively, using Homebrew on macOS or Linuxbrew on Linux you can simply run: brew install ant

When using brew it isn’t necessary to set up the environment variables.

Several Linux distributions also support installing Ant from their respective package managers.

To test Ant is installed properly navigate to the command prompt and execute

ant -version

This command will print the Ant version and also shows that Ant was successfully installed.

Ant’s own installation instructions page is available on the Apache Ant website.