Apache Ant Command Line Arguments Example

1. Introduction

In this example, we will explain Apache Ant Command Line Arguments.

In software development, the term building usually means the conversion of source code and other artifacts, like images or configuration files, into another artifact. For example source code might be compiled into a JAR file, or you may create a new standalone application. The build result can be shared with users of the software or used only internally.

A build tool is used to automate repetitive tasks during this process. This can be, for example, compiling source code, running software tests, and creating files and documentation for the software deployment.

Build tools typically run without a graphical user interface directly from the command line. As a user interface is not required for such builds, these builds are called headless.

Popular build tools in the Java space are Maven, Gradle and Apache Ant.

2. What is Apache Ant

Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test, and run Java applications. Ant can also be used effectively to build non-Java applications, for instance, C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.

A Java build process typically includes:

  • the compilation of the Java source code into Java bytecode
  • creation of the .jar file for the distribution of the code
  • creation of the Javadoc documentation

Ant uses an XML file for its configuration. The default file name is build.xml. Ant builds are based on three blocks: tasks, targets, and extension points.

A task is a unit of work which should be performed and constitutes of small atomic steps, for example, compile source code or create Javadoc. Tasks can be grouped into targets.

A target can be directly invoked via Ant. Targets can specify their dependencies. Ant will automatically execute all dependent targets.

For example, if target A depends on B and Ant is instructed to run A, it first runs B before running A.

In your build.xml file, you can specify the default target. Ant executes this target if no explicit target is specified.

3. Technology Stack

In this example we will be using the following technology stack:

  1. Eclipse 4.14
  2. Ant 1.9.15
  3. JDK 1.7

4. Installation

4.1 Prerequisites

Ant requires the installation of the Java Development Kit (JDK). 

4.2 Windows

Download Apache Ant from

Extract the zip file into a directory structure of your choice. Set the ANT_HOME environment variable to this location and include the ANT_HOME/bin directory in your path.

Make also sure that the JAVA_HOME environment variable is set to the JDK. This is required for running Ant.

Check your installation by opening a command line and typing ant -version into the command line. The system should find the command Ant and show the version number of your installed Ant version.

5. Apache Ant Command line option summary

When no arguments are specified, Ant looks for a build.xml file in the current directory and, if found, uses that file as the build file and runs the target specified in the default attribute of the <project> tag. To make Ant use a build file other than build.xml, use the command-line option –buildfile file, where the file is the name of the build file you want to use (or a directory containing a build.xml file).

If you use the -find[file] option, Ant will search for a build file first in the current directory, then in the parent directory, and so on, until either a build file is found or the root of the filesystem has been reached. By default, it will look for a build file called build.xml. To have it search for a build file other than build.xml, specify a file argument. Note: If you include any other flags or arguments on the command line after the -find flag, you must include the file argument for the -find flag, even if the name of the build file you want to find is build.xml.

You can also set properties on the command line. This can be done with the -Dproperty=value option, where the property is the name of the property, and value is the value for that property. If you specify a property that is also set in the build file (see the property task), the value specified on the command line will override the value specified in the build file. Defining properties on the command line can also be used to pass in the value of environment variables; just pass -DMYVAR=$MYVAR to Ant. You can then access environment variables using the property task’s environment attribute.

Options that affect the amount of logging output by Ant are: -quiet, which instructs Ant to print less information to the console; -verbose, which causes Ant to print additional information to the console; -debug, which causes Ant to print considerably more additional information; and -silent which makes Ant print nothing but task output and build failures (useful to capture Ant output by scripts).

It is also possible to specify one or more targets that should be executed. When omitted, the target that is specified in the default attribute of the project tag is used.

The –projecthelp option prints out a list of the build file’s targets. Targets that include a description attribute are listed as “Main targets”, those without a description are listed as “Other targets”, then the “Default” target is listed (“Other targets” are only displayed if there are no main targets, or if Ant is invoked in -verbose or -debug mode).

5.1 Options

-help, -h
print this message and exit

-projecthelp, -p
print project help information and exit

print the version information and exit

print information that might be helpful to diagnose or report problems and exit

-quiet, -q
be extra quiet

-silent, -S
print nothing but task outputs and build failures

-verbose, -v
be extra verbose

-debug, -d
print debugging information

-emacs, -e
produce logging information without adornments

specifies a path to search for jars and classes

-logfile , -l
use given file for log

the class which is to perform logging

add an instance of class as a project listener

do not allow interactive input

-buildfile , -file , -f
use is given buildfile

use-value for a ven property

-keep-going, -k
execute all targets that do not depend on failed target(s)

load all properties from file with -D properties taking precedence

the class which will handle input requests

-find , -f
Search for build file towards the root of the filesystem and use it

-nice number
A niceness value for the main thread: 1 (lowest) to 10 (highest); 5 is the default

Run ant without using the jar files from ${user.home}/.ant/lib

Run ant without using CLASSPATH

Java1.5+: use the OS proxy settings

override Ant’s normal entry point

5.2 Examples

a) runs Ant using the build.xml file in the current directory, on the default target.


b) runs Ant using the test.xml file in the current directory, on the default target.

ant -buildfile test.xml

c) runs Ant using the test.xml file in the current directory, on the target called dist.

ant -buildfile test.xml dist

d) runs Ant using the test.xml file in the current directory, on the target called dist, setting the build property to the value build/classes.

ant -buildfile test.xml -Dbuild=build/classes dist

e) runs Ant picking up the additional task and support jars from the /home/ant/extras location

ant -lib /home/ant/extras

6. Using Ant for Java development

The following describes how you compile Java classes, create an executable JAR file, and create Javadoc for your project with Apache Ant. The following example assumes that you are using the Eclipse IDE to manage your Java project and your Ant build.

6.1 Create Java project

Create a Java project called com.core.utils.DateUtils in Eclipse.

package com.core.utils;

import java.util.Date;

public class DateUtils {

	public static void main(String[] args) {


	private static Date getLocalCurrentDate() {
		return new Date();


6.2 Create build.xml

Create a new file through the File New File menu and create the build.xml file. Implement the following code to this file.


<project name="AntJavaProject" default="main" basedir=".">
		Create a Java Project (JAR) with Ant build script

	<property name="projectName" value="DateUtils" />

	<!-- Java sources -->
	<property name="src.dir" location="src" />

	<!-- Java classes -->
	<property name="build.dir" location="bin" />

	<!-- Output, Jar -->
	<property name="dist.dir" location="dist" />

	<target name="init">
		<!-- Create the time stamp -->
		<tstamp />
		<!-- Create the build directory structure used by compile -->
		<mkdir dir="${build.dir}" />

	<target name="compile" depends="init" description="compile the source ">
		<!-- Compile the java code from ${src.dir} into ${build.dir} -->
		<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" />

	<target name="dist" depends="compile" description="package, output to JAR">

		<!-- Create the distribution directory -->
		<mkdir dir="${dist.dir}" />

		<!-- Put everything in ${build} into the {$projectName}-${DSTAMP}.jar file -->
		<jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}">
				<attribute name="Main-Class" value="com.core.utils.DateUtils" />

	<target name="clean" description="clean up">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />

	<!-- Default, run this -->
	<target name="main" depends="clean, compile, dist" />


6.3 Run your Ant build from the command line

Open a command line and switch to your project directory. Type the following commands.

# run the build
ant -f build.xml

# run ant with defaults (build.xml file and the default target)

The build should finish successfully and generate the build artifacts.

7. Download the source code

That was an example of Apache Ant Command Line.

You can download the full source code of this example here: Apache Ant Command Line Arguments Example

Simranjit Singh

Simranjit Singh has graduated from Computer Science Department of the Guru Nanak Dev University of Amritsar, Punjab, India. He also holds a Master degree in Software Engineering from the Birla Institute of Technology & Science of Pilani, Rajasthan, India. He works as a Senior Consultant in the e-commerce sector where he is mainly involved with projects based on Java and Big Data technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button