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

Leave Comment

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

en_USEnglish
en_USEnglish