Apache Ant

From Bauman National Library
This page was last modified on 21 January 2019, at 13:10.
Apache Ant
(Another Neat Tool)
Developer(s) Apache Software Foundation
Stable release
Repository {{#property:P1324}}
Development status Retired
Written in Java
Operating system Cross-platform software
Type Assembly automation
License Apache 2.0 License
Website http://ant.apache.org/

Apache Ant — a utility for automating the software build process. It is platform independent analogue utilities make, where all commands are written in a XML-format.
Ant was created as part of the project Jakarta, today - the first independent project-level Apache Software Foundation.
The first version was developed by Sun Microsystems engineer James Davidson (James Davidson (Eng.) Russian.), Who needed a utility similar to make, developing the first reference implementation of J2EE.
An Ant, unlike other collector Apache Maven project provides an imperative rather than declarative assembly project.[Reference 1]


Unlike make, Ant tool is completely independent of the platform, but requires a system installed on the used Java operating environment - JRE. Failure to use the operating system commands and scripts in XML format provides portability.
Control the build process occurs through XML-script, also known as Build-file. First of all, this file contains the definition of the project, consisting of individual targets (Targets). Objectives are comparable to the procedures in programming languages ​​and include calls to teams tasks (Tasks). Each task is indivisible, atomic command that performs an elementary action.
Between the goals can be defined depending on - each target is performed only after you have completed all the goals on which it depends (if they have already been performed previously, the redo is not performed).
Typical examples of goals are clean (the removal of intermediate files), compile (compile all classes), deploy (deployment of applications on the server). A specific set of objectives and their relationship depend on the specifics of the project.
Ant lets you define your own types of assignments by creating a Java-classes that implement certain interfaces.

Principles of work

The build scenario – XML-file. The text is opened (and closed) by the project tag, in which you can specify the name of the project and the default goal. Further, it contains definition of goals (target), dependencies (depends) and properties (property). The simplest scenario should have at least one goal. You can set a name for the target using the name attribute (name = name_of_target). The specified name becomes a command for our script and you can call the corresponding target like this: ant name_of_target The target has the ability to specify a dependency using the depends attribute. Dependencies link target to each other. For example, there is a target with the name compile, and there is - with the name run, dependent on the compile. And if we want to run, the compile is executed first.

Frequently used tasks (Tasks)

The package includes a set of libraries, affecting a wide range of tasks. In addition to that extend the functionality of the product web site of the project are additional libraries, eg, for integration with third-party systems. The standard version contains more than 150 types of tasks. Examples of these commands are shown below.

Code Act
javac Java-code compilation
copy Copying files
delete Deleting files and directories
move Moving files and directories
replace Replacement of fragments of text in files
junit Automatic start unit tests
exec Execution of external commands
zip The creation of an archive in the format Zip
cvs Perform CVS -commands
mail Sending e-mail
xslt Imposition of XSLT metabolism

Project example

Let`s create a little project based on html, css, js to demonstrate the possibilities of Apache Ant.[Reference 2]

Creating of html-file

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <title>Dummy Project</title>
    <link rel="stylesheet" href="css/reset.css"  />
    <link rel="stylesheet" href="css/grid.css"   />
    <link rel="stylesheet" href="css/custom.css" />
        <h1> Demo Page </h1>
        <p> There are three CSS files and three JavaScript files in this project.
            <li><a href="http://meyerweb.com/eric/tools/css/reset/">Eric Meyer's Reset</a></li>
            <li><a href="http://www.blueprintcss.org/">Grid.css from the Blueprint CSS</a></li>
            <li>A custom stylesheet</li>
            <li> <a href="http://jquery.com">jQuery</a></li>
            <li> <a href="http://javascript.crockford.com/tdop/index.html">Douglas Crockford's Top Down Operator Precedence files tokens.js and parse.js.</a></li>
        <p> All these files are uncompressed; that's the point of this tutorial, after all. </p>
    <script src="js/jquery-latest.js"></script>
    <script src="js/tokens.js"></script>
    <script src="js/parse.js"></script>

Creating of XML-file

In the next step create a file that is used by Ant by default. It must be named build.xml:

<?xml version="1.0"?>
<project name="Compress, Concatenate, and FTP CSS and JS" default="initialize">

Creating the properties file

Ant can work with the properties which look like a usual variable. Create the target to import properties:

<target name="get_properties">
    <property file="ant.properties" />
    <echo>Imported Properties</echo>

Now create properties file named ant.properties:

css_dir = css
js_dir = js
assets_dir = assets

Creating of the targets

Now create targets which would be used by maker. To do this include next code which collect css and js to the build.xml:

<target name="concat_css">
    <concat destfile="${assets_dir}/style.css">
        <filelist id="files" dir="${css_dir}">
            <file name="reset.css" />
            <file name="grid.css" />
            <file name="custom.css" />
<target name="concat_js">
    <concat destfile="${assets_dir}/script.js">
        <filelist id="files" dir="${js_dir}">
            <file name="jquery-latest.js" />
            <file name="tokens.js" />
            <file name="parse.js" />

Launching the target

In the final step we should create a target which would launch all others':

<target name="initialize" depends="get_properties">
    <mkdir dir="${assets_dir}" />
    <antcall target="concat_css" />
    <antcall target="concat_js"  />

Installing and configuring Apache Ant

Additionally, we set the JDK with the official site.

  • Unpack the archive in a convenient place for us.

The archive is apache-ant-1.9.3-bin.zip folder apache-ant-1.9.3.
I threw this folder to the root of the C: drive, and renamed it the ant.
Those. I Ant is in the C: \ ant - it will need in the future.

  • We set up the environment variables.

!!! You have the way to the JDK and Ant can be different. Before you copy-paste commands and settings, check all the way and adjust them for yourself.
In my case, Ant is in the C: \ ant and the JDK in C: \ Program Files \ Java \ jdk1.7.0_51

The easiest way - to collect three teams in CMD:

set ANT_HOME = C: \ ant <br />
set JAVA_HOME = C: \ Program Files \ Java \ jdk1.7.0_51 <br />
set PATH =% PATH%;% JAVA_HOME% \ bin;% ANT_HOME% \ bin

Here is another option settings. Maybe so someone will be more convenient.

  • Open My Computer - Properties - Advanced System Settings. In the window that opens, select the tab Advanced - Environment Variables

Under System Variables, click the Create button and add two new variables:
ANT_HOME first variable with a value of C: \ ant
second variable JAVA_HOME and its value: C: \ Program Files \ Java \ jdk1.7.0_51
Click OK

  • Next you need to add the path to the Ant and JDK to your PATH environment variable. In the list of system variables find the PATH and click the Edit button.

The important point. If the PATH, in the value of the variable is something inscribed neatly put at the end of the line with a semicolon, and appends:
% JAVA_HOME% \ bin;% ANT_HOME% \ bin
I have a value for the variable PATH looks like this:
C: \ adb;% JAVA_HOME% \ bin;% ANT_HOME% \ bin
If you have not found at PATH, create a new variable is similar to item 3.1.

  • Check work Ant and JAVA.

Run CMD and write in it

ant -version <br />
java -version

The answer should get something like:

Figure 1 - checking correctivity of installation


  1. ApacheAnt Documentation // ApacheAnt official site [2013–2019]. Date of update: 26.11.2018. URL: https://ant.apache.org/manual/index.html (date of access: 05.01.2019)
  2. ApacheAnt Tutorial // Ruseller information portal [2015–2019]. Date of update: 21.10.2018. URL: https://ruseller.com/lessons.php?rub=28&id=1051 (date of access: 05.01.2019)