ant Getting started with ant

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

This section provides an overview of what Ant is, and why a developer might want to use it.

It should also mention any large subjects within Ant, and link out to the related topics. Since the Documentation for Ant is new, you may need to create initial versions of those related topics.

Minimum Java Versions

Various versions of Ant require different versions of the Java runtime (the JRE) in order to run.

Ant VersionMinimum Java Version
1.1 up to 1.5.41.1
1.6.x releases1.2
1.7.x releases1.3
1.8.x releases1.4
1.9.x releases1.5
1.10.x releases1.8

Versions

VersionRelease Date
1.4.12001-10-11
1.5.02002-07-10
1.5.12002-10-03
1.5.22003-03-03
1.5.32003-04-09
1.5.42003-08-12
1.6.02003-12-18
1.6.12004-02-12
1.6.22004-07-16
1.6.32005-04-28
1.6.42005-05-19
1.6.52005-06-02
1.7.02006-12-13
1.7.12008-07-09
1.8.02010-02-02
1.8.12010-04-30
1.8.22010-12-20
1.8.32012-03-13
1.8.42012-05-23
1.9.02013-03-10
1.9.12013-05-22
1.9.22013-07-12
1.9.32013-12-29
1.9.42014-04-30
1.9.52015-06-03
1.9.62015-07-02
1.9.72016-04-12
1.9.82016-12-31
1.9.92017-02-06
1.10.02016-12-31
1.10.12017-02-06

Bootstrap Apache Ivy

Add the following target in your build.xml

<!-- Bootstrap ivy -->
<target name="ivy.bootstrap" description="Download Apache Ivy">

    <!-- Define the version to use -->
    <property name="ivy.version">2.4.0</property>

    <!-- Create directory if not exists -->
    <mkdir dir="${user.home}/.ant/lib" quiet="true" />

    <!-- Download it -->
    <echo message="Downloading Apache Ivy..." />
    <get dest="${user.home}/.ant/lib/ivy.jar" src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar" />
</target>
 

After running the task ant ivy.bootstrap , you will now be able to resolve dependencies using apache ivy.

<target name="ivy.resolve" description="Resolve all artifacts.">

    <!-- Define lib driectory -->
    <property name="dir.lib">lib</property>

    <!-- Create directory if not exists -->
    <mkdir dir="${dir.lib}" />

    <!-- Configure -->
    <property name="ivy.dep.file" value="ivy.xml" />
    <ivy:settings file="ivysettings.xml" />

    <!-- Retrieve to a defined pattern -->
    <echo message="Resolving dependencies..." />
    <ivy:retrieve pattern="${dir.lib}/[artifact](-[classifier]).[ext]" />
</target>
 

Define your resources in ivy.xml

<ivy-module version="2.0">
        <info organisation="org.apache" module="java-build-tools"/>
        <dependencies>
                <dependency org="junit" name="junit" rev="4.11" />
                <dependency org="org.apache.commons" name="commons-compress" rev="1.9" />
        </dependencies>
</ivy-module>
 

And any custom repositories in ivysettings.xml

<ivysettings>
    <settings defaultResolver="chain"/>
    <resolvers>
        <chain name="chain">
            <ibiblio name="central" m2compatible="true"/>
            <ibiblio name="github" m2compatible="true" root="http://github.com/"/>
        </chain>
    </resolvers>
</ivysettings>
 

Download your dependencies by running ant ivy.resolve .

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/Main.java 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}"/>
    </path>

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

    <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"/>
        </copy>
    </target>

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

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

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" />
    </target>
</project>
 

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

main:
     [echo] Hello World

BUILD SUCCESSFUL
 

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

main:
     [echo] Hello World

BUILD SUCCESSFUL
 

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
 

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.

  5. Set CLASSPATH=%ANT_HOME%\lib;%CLASSPATH%

  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.

The following is handy to have in build logs that identifies the build machine, and some parameters; simply make you main task depend on this task to print it before every build.

<!-- Print Environment Info -->
<target name="environment">

        <!-- Get the current timestamp -->
        <tstamp>
                <format property="TODAY_UK" pattern="yyyy-MM-dd HH:mm:ss:sss zzz" locale="cn,CN" />
        </tstamp>

        <!-- Get the hostname of the system -->
        <exec executable="hostname" outputproperty="os.hostname" />

        <!-- Print a bunch of information -->
        <echo message="" />
        <echo message="  Build Information" />
        <echo message="" />
        <echo message="  OS Information" />
        <echo message="" />
        <echo message="    User       : ${user.name}" />
        <echo message="    Hostname   : ${os.hostname}" />
        <echo message="" />
        <echo message="    Name       : ${os.name}" />
        <echo message="    Version    : ${os.version}" />
        <echo message="    Build      : ${os.arch}" />
        <echo message="" />
        <echo message="" />
        <echo message="  Java Information" />
        <echo message="" />
        <echo message="    Version    : ${ant.java.version} / ${java.version}" />
        <echo message="    Java Home  : ${java.home}" />
        <echo message="" />
        <echo message="" />
        <echo message="  Current Time : ${TODAY_UK}" />
        <echo message="" />
</target>
 

This will result in the following output,

environment:
     [echo]
     [echo]   Build Information
     [echo]
     [echo]   OS Information
     [echo]
     [echo]     User       : <User Name>
     [echo]     Hostname   : <Host Name>
     [echo]
     [echo]     Name       : Windows 8.1
     [echo]     Version    : 6.3
     [echo]     Build      : amd64
     [echo]
     [echo]
     [echo]   Java Information
     [echo]
     [echo]     Version    : 1.8 / 1.8.0_45
     [echo]     Java Home  : C:\Program Files\Java\jdk1.8.0_45\jre
     [echo]
     [echo]
     [echo]   Current Time : 2016-04-18 00:40:11:011 EDT
 

Run JUnit

The following will run JUnit on the tests matching test/**/*Test.java . This need the junit.jar to be in the lib folder.

<project name="Project" default="junit" basedir=".">
    <path id="classpath">
        <fileset dir="lib" includes="**/*.jar"/>
        <pathelement path="build"/>
    </path>

    <target name="compile">
        <javac srcdir="test" destdir="build" classpathref="classpath"/>
    </target>

    <target name="junit" depends="compile">
        <junit fork="true" logfailedtests="false">
            <classpath refid="classpath"/>
            <batchtest>
                <fileset dir="test" includes="**/*Test.java"/>
                <formatter type="plain" usefile="false"/>
            </batchtest>
        </junit>
    </target>
</project>
 


Got any ant Question?