Originally published on 2010-03-21; fully revised and updated.
Apache Ant is a build automation tool used heavily in Java ecosystems to compile source code, run tests, and package deployable artifacts. This guide helps engineers who need a practical Ant refresher: you will learn core concepts, execution flow, working commands, CI usage patterns, and when Ant is a better fit than Maven or Gradle.

Original slide deck and attribution
The original post primarily embedded a SlideShare deck: Introduction To Ant by Rajesh Kumar.
Apache Ant fundamentals
- Project: The root object in
build.xml. - Target: A named build stage such as
clean,compile, orpackage. - Task: A concrete operation inside a target (for example
mkdir,javac,jar). - Dependencies: Targets can depend on other targets, creating ordered execution.
- Properties: Reusable key-value settings for directories, options, and environment values.
Ant is an imperative build system: you describe exactly what should run and in what order. That explicit control is useful for custom workflows and legacy projects.
How an Ant build runs
flowchart TD
A[Run ant package] --> B[Load build.xml]
B --> C[Resolve properties]
C --> D[Evaluate target dependencies]
D --> E[Execute tasks in target order]
E --> F[Compile test package steps]
F --> G[Create build artifact]
Minimal build.xml you can run
This example compiles Java source code and packages a JAR file.
<project name='demo-app' default='package' basedir='.'>
<property name='src.dir' value='src'/>
<property name='build.dir' value='build'/>
<property name='dist.dir' value='dist'/>
<target name='clean'>
<delete dir='${build.dir}'/>
<delete dir='${dist.dir}'/>
</target>
<target name='init'>
<mkdir dir='${build.dir}'/>
<mkdir dir='${dist.dir}'/>
</target>
<target name='compile' depends='init'>
<javac srcdir='${src.dir}' destdir='${build.dir}' includeantruntime='false'/>
</target>
<target name='package' depends='compile'>
<jar destfile='${dist.dir}/demo-app.jar' basedir='${build.dir}'/>
</target>
</project>Code language: HTML, XML (xml)
Common Ant CLI usage:
ant clean
ant compile
ant package
ant -f build.xml package
ant -Dbuild.dir=out package
ant -p
Running Ant in CI jobs
In CI, Ant is usually called from a shell step. Keep the command explicit and fail the job on any non-zero exit code.
#!/usr/bin/env bash
set -euo pipefail
ant clean package
ant -Dbuild.number=${BUILD_NUMBER} packageCode language: JavaScript (javascript)
Ant vs Maven vs Gradle
| Tool | Build definition style | Dependency management | Best for | Trade-offs |
|---|---|---|---|---|
| Ant | Imperative XML targets and tasks | Not built in by default (commonly paired with Apache Ivy) | Custom workflows and legacy Java builds | More manual structure and conventions |
| Maven | Declarative lifecycle with conventions | Built-in repository model and transitive dependencies | Standardized Java application and library builds | Less flexible for unusual build logic |
| Gradle | Declarative plus programmable DSL | Built-in dependency management | Complex multi-module and modern build automation | Can become complex without strong standards |
Useful Ant command patterns
| Command | What it does | Typical use |
|---|---|---|
| ant | Runs the default target from build.xml | Standard local build |
| ant test | Runs one target explicitly | Debug or isolate a single stage |
| ant -p | Prints available targets | Discover project build entry points |
| ant -f custom-build.xml package | Uses a non-default build file | Alternate workflows or monorepo setups |
| ant -Denv=prod deploy | Injects runtime property values | Environment-specific execution without editing XML |
Best practices and common pitfalls
- Keep targets small and single-purpose; use
dependsto control order. - Externalize environment values into properties files; avoid hard-coded absolute paths.
- Name targets clearly and verify discoverability with
ant -p. - Keep a reliable
cleantarget to ensure reproducible builds. - If dependency resolution is needed, standardize your Ivy integration across all projects.
Ant remains a strong choice when you want explicit, scriptable control over build steps. For heavily standardized dependency-driven projects, Maven or Gradle may reduce manual effort.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals
Apache Ant Comments and echo Task: Write Clear build.xml Files and Control Log Output
Learn the correct way to add comments in Apache Ant and when to use the echo task instead. This guide covers echo attributes, log-level behavior, and practical build.xml examples.