Apache Ant
Apache Ant is a software tool for automating software build processes which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java language and requires the Java platform. Unlike Make, which uses the Makefile format, Ant uses XML to describe the code build process and its dependencies.
Released under an Apache License by the Apache Software Foundation, Ant is an open-source project.
History
Ant was conceived by James Duncan Davidson while preparing Sun Microsystems's reference JSP and Servlet engine, later Apache Tomcat, for release as open-source. A proprietary version of Make was used to build it on the Solaris platform, but in the open-source world, there was no way of controlling which platform was used to build Tomcat; so Ant was created as a simple platform-independent tool to build Tomcat from directives in an XML "build file". Ant was officially released as a stand-alone product on July 19, 2000.Several proposals for an Ant version 2 have been made, such as AntEater by James Duncan Davidson, Myrmidon by Peter Donald and Mutant by Conor MacNeill, none of which were able to find large acceptance with the developer community.
At one time, Ant was the build tool used by most Java development projects. For example, most open source Java developers included
build.xml
files with their distribution. Because Ant made it trivial to integrate JUnit tests with the build process, Ant made it easy for willing developers to adopt test-driven development, and even extreme programming.Extensions
WOProject-Ant is just one of many examples of a task extension written for Ant. These extensions are installed by copying their.jar
files into ant's lib
directory. Once this is done, these task extensions can be invoked directly in the typical build.xml
file. The WOProject extensions allow WebObjects developers to use ant in building their frameworks and apps, instead of using Apple's Xcode suite.Antcontrib
provides a collection of tasks such as conditional statements and operations on properties as well as other useful tasks.Ant-contrib.unkrig.de
implements tasks and types for networking, Swing user interfaces, JSON processing and other.Other task extensions exist for Perforce,.NET Framework, EJB, and filesystem manipulations.
Example
Below is listed a samplebuild.xml
file for a simple Java "Hello, world" application. It defines four targets - clean
, clobber
, compile
and jar
, each of which has an associated description. The jar
target lists the compile
target as a dependency. This tells Ant that before it can start the jar
target it must first complete the compile
target.Within each target are the actions that Ant must take to build that target; these are performed using built-in tasks. For example, to build the
compile
target Ant must first create a directory called classes
and then invoke the Java compiler. Therefore, the tasks used are mkdir
and javac
. These perform a similar task to the command-line utilities of the same name.Another task used in this example is named
jar
:This Ant task has the same name as the common Java command-line utility, JAR, but is really a call to the Ant program's built-in JAR/ZIP file support. This detail is not relevant to most end users, who just get the JAR they wanted, with the files they asked for.
Many Ant tasks delegate their work to external programs, either native or Java. They use Ant's own and tasks to set up the command lines, and handle all the details of mapping from information in the build file to the program's arguments and interpreting the return value. Users can see which tasks do this, by trying to execute the task on a system without the underlying program on the path, or without a full Java Development Kit installed.
Portability
One of the primary aims of Ant was to be more portable than Make. In Make, the actions required to create a target are specified as platform-specific shell commands, whereas Ant provides a large amount of built-in functionality that is designed to behave the same on all platforms. For example, in the samplebuild.xml
file above, the clean target deletes the classes
directory and everything in it. In a Makefile this would typically be done with the command:rm -rf classes/
rm
is a Unix-specific command unavailable in some other environments. Microsoft Windows, for example, would use:rmdir /S /Q classes
In an Ant build file the same goal would be accomplished using a built-in command:
Additionally, Ant does not differentiate between forward slash or backslash for directories and semicolon or colon for path separators. It converts each to the symbol appropriate to the platform on which it executes.
Limitations
- Ant build files, which are written in XML, can be complex and verbose, as they are hierarchical, partly ordered, and pervasively cross-linked. This complexity can be a barrier to learning. The build files of large or complex projects can become unmanageably large. Good design and modularization of build files can improve readability but not necessarily reduce size. Other build tools, such as Gradle or Maven, use more concise scripts at the expense of generality and flexibility.
- Many of the older tasks—the core ones that are used every day, such as, and —use default values for options that are not consistent with more recent versions of the tasks. Changing those defaults would break existing Ant scripts.
- When expanding properties in a string or text element, undefined properties are not raised as an error, but left as an unexpanded reference.
- Ant has limited fault handling rules.
- Lazy property evaluation is not supported. For instance, when working within an Antcontrib loop, a property cannot be re-evaluated for a sub-value which may be part of the iteration.
- In makefiles, any rule to create one file type from another can be written inline within the makefile. For example, one may transform a document into some other format by using rules to execute another tool. Creating a similar task in Ant is more complex: a separate task must be written in Java and included with the Ant build file in order to handle the same type of functionality. However, this separation can enhance the readability of the Ant script by hiding some of the details of how a task is executed on different platforms.