Apache Ant is a Java library and command-line tool to automate the repetitive tasks and helps to build software. Ant is flexible and does not impose coding conventions. It supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications.

Other than Java applications it can also be used to build non-Java applications like C or C++ applications.

The Apache Ant project is part of the Apache Software Foundation.

Ant Features

  • Command-line tool to automate the repetitive tasks
  • A flexible tool that provides built-in tasks allowing to compile, assemble, test and run Java applications
  • Can develop custom tasks
  • Declaring properties using property element
  • Copying files or directories from one location to other
  • Delete files or directories
  • Edit files and replace/delete some content
  • Conditional tasks

Ant Setup

  • To work with apache ant JDK (Java Development Kit) must have installed on your system.
  • Set JAVA_HOME environment variable.
  • Download Apache Ant from the link.
  • Unzip the downloaded Apache zip and set the ANT_HOME environment variable.
  • To verify the installation use following command
C:\>ant -version
Apache Ant(TM) version 1.9.7 compiled on April 9 2016

Creating simple build file

Apache Ant build file is written in XML. To create a simple build file you need to create a project and at least one target using ‘project’ and ‘target’ element.

Project

<project basedir="." default="build" name="project_name">

name is the name of the project.

default attribute specifies the default target that will be executed when no target is specified with ant command.

basedir attribute defines the path of the directory from where another path will be resolved.

Target

<target depends="init" name="build-project">
   <echo message="any message here to print"/>
</target>

name is the name of target that can be used to refer/run a target.

depends attribute defines the target on which current target depends on. Dependent target will be executed first.

Example

 <project basedir="." default="init" name="project_name">
      <target name="init">
          <echo message="Init target"/>
       </target>
 </project>

Save above snippet as build.xmlTo run this use ant command.

D:\>ant
Buildfile: D:\build.xml
init:
     [echo] Init target
BUILD SUCCESSFUL
Total time: 1 second

When you simply run ant command it searches for the build.xml file and run the default target that is defined in project element.

If your build file is saved with some other name then you can use buildfile option.

 ant -buildfile file_name.xml

 If you want to run specific target then you can use

 ant -buildfiile file_name.xml target_name

Next Tutorial >> Apache Ant Basic Tasks

Categories: Apache AntJava

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Apache Ant

Difference between Maven and Apache Ant

Build tools are programs to manage and automate build process of your application. By build process means compiling source code, linking the packages, bundle them into an executable binary. Maven and Apache Ant are popular build Read more…

Apache Ant

Apache Ant basic tasks

In the previous post, we have learned what is Apache Ant, its features and an example to create simple build file. This post will guide you other basic ant tasks that are frequently used to create Read more…

Java

Advantages of spring interceptor over servlet filter

Even filters can do the same things that interceptors can but interceptors can do more easily and more elegantly. Interceptors are more coupled to the request/response cycle than filters. Can inject other beans in the Read more…