Apache Ant should be familiar to every Java programmer: it is a popular build tool written entirely in Java. Ant uses a script, which is a regular XML file. Despite its Java focus, web developers use this tool too.

Download, install, test

Linux: install from the repository with a command like sudo apt-get install ant (replace apt-get with yum if necessary). Important: we need a version at least 1.8.*. The CentOS 6.8 repository has version 1.7.1, so it is better to use the script described in the previous article.

Windows: go to ant.apache.org, go to Download/Binary Distributions and download the apache-ant-1.10.1-bin.zip archive (there may be a more recent version available now). Copy the contents of the archive to any directory, for example to “C:\Program Files\Apache Ant”. Then add the path to the bin directory (C:\Program Files/Apache Ant/bin) to the Path system variable.

Write HelloWorld script

<?xml version="1.0"?
Hello, World!

Create a subdirectory hello in the home directory (in Linux this is done with the mkdir command, in Windows with the md command) and save a file named build.xml containing the above script.

Basic principles of operation

The build script is a regular XML file. The text opens (and closes) with the project tag, where you can specify a project name and a default target. It then contains the definition of targets, dependencies and properties. The simplest script must have at least one target. In the target tag we describe the invocation of one or more tasks. The target can be named using the name attribute (name=”name_of_target”).

Minimum required list of tasks

The standard version of Ant contains more than 150 tasks. We will only need seven so far:

  • echo – output messages to the console;
  • mkdir – create directories;
  • delete – deleting files and directories;
  • javac – compile Java code;
  • java – run class and jar files;
  • jar – create jar file;
  • junit – run tests.

Script for building and testing a Java project

Ant provides full freedom in forming the directory structure.