Ant tutorial

Jakob Jenkov
Last update: 2015-08-18

Ant was the first build tool that was created for building Java applications. Ant is implemented in Java, and its build scripts are written in XML. The XML build scripts were easier to understand than the "make" files at that time, so Ant quickly became popular as a build tool among Java developers. Additionally, Ant is an open source project which made it available for free to all developers.

Just to recap, a "Java build tool" is a tool which can compile Java code, run the unit tests for it, package the compiled classes into a JAR file and many, many other things. You specify what to build (and sometimes how) in a build script. This build script can then be executed again and again by the build tool. This is much easier, faster, and less error prone than performing all these tasks manually.

Other Java Build Tools

Ant is not the only build tool for Java. After Ant came Maven which has standardized how Java projects are built and how dependencies (external JAR files used by your Java project) are managed (specified and downloaded). Today Maven has taken a lot of the popularity that Ant used to have. You can learn more about Maven in my Maven tutorial.

Later came another build tool called Gradle. Gradle was originally intended as a build tool for Groovy based projects. Groovy is a programming language that also runs on top of the Java Virtual Machine. Gradle build scripts are actually Groovy scripts.

Gradle lived its own life in the Groovy community in the beginning, but then slowly started to make its way into the Java community. Gradle has especially gained traction after Google decided to use Gradle as the standard build tool in Android Studio for Android projects. You can learn more about Gradle in my Gradle tutorial.

At some point in the future I will write an article comparing these different build tools, so check back from time to time for a link to that article!

The Ant Website

You find the Ant website at:

http://ant.apache.org

Ant Version

This Ant tutorial is based on Ant version 1.9.6 which is the newest version of Ant at the time of writing (august 2015).

Ant Core Concepts

The Ant core concepts are:

  • Projects
  • Properties
  • Targets
  • Tasks
  • Build scripts

An Ant project contains all the information necessary to build some software project using Ant. An Ant project is contained within an Ant build script (a file).

An Ant property is a key, value pair which you can specify. Properties typically contain information used in the build process, like directory names, file names, server names or whatever else you may need in your project.

An Ant target typically specifies one step of the build process for the project. An Ant project contains at least one target, but more commonly an Ant project contains multiple targets. You could specify the whole build process within a single target, but typically it is easier to split the build process up into multiple targets.

Ant targets can be executed individually, so if you have multiple different build actions you need to be able to execute independently, splitting them up into multiple Ant targets is the way to go. For instance, you might want to be able to compile your code without generating JavaDoc every time, or without running the unit tests etc. Splitting JavaDoc and unit test execution into their own targets makes this easier to achieve.

Ant tasks are build actions like copying files, compiling Java code, generating JAR files, generating JavaDoc etc. Ant comes with a large set of built-in tasks you can use, so you don't have to code your own tasks (but you can if you need to). An Ant target will typically contain one or more tasks. The Ant tasks perform the actual build operations of the Ant target they are listed inside.

An Ant build script contain a single Ant project, and that project may again contain properties and targets. The targets may contain tasks.

Getting Started With Ant

There are two ways to get started with Ant. Either you use a Java IDE which supports Ant scripts (like Intellij IDEA) or you download and install Ant and run Ant from the command line. This Ant tutorial will focus on how you run Ant from the command line (running Ant build scripts from within an IDE is pretty easy once you understand how Ant works, and how the Ant command tool line works).

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next