Useful Ant build tags


How to do following tasks in ant file?

  1. Make zip file.
  2. Run command.
  3. Copy files to remote machine.
  4. Run commands on Remote Linux machine.
  5. Open an input box and respond to input value.
  6. Make an ant call.


1. Make zip file:

Following is the xml for making zip file in ant:

<zip destfile="${destination.folder}/">
     <fileset dir= "${Source.folder}" />

In here “destfile” is the name and location of the created zip file. Inside fileset tag the dir attricute is used to specify source folder form where all files will be zipped.

2. Run commands:

Here I will show you how to start tomcat in ant file to demonstrate how to run commands in ant. Following is the xml for this:

<exec dir="${tomcat.home}/bin" executable="cmd" os="Windows XP">
     <arg line="/c startup.bat"/>

Here “${tomcat.home}” is the path of the tomcat folder. The command is given in “<arg>” tag in “line” attribute.


Note: To run following commands you will need JSCH jar.

3. Copy files to remote machine:

If remote machine is in your LAN and you can access it directly as \\remote_machine\path then you can use simple copy command as follows:

<copy file="${source.folder.file} " todir="\\remote\path">

To copy files on remote machine that supports SCP use following tag:

<scp file="${source.folder.file} " todir="${remote.user}@${}:${}"
       password="${remote.password}" trust="true">

In above both commands “file” is the source file which you want to copy with its path. And “todir” is the remote machine folder path where you want to copy the file.


4. Run commands on remote machine:

You can use following tag to execute commands on remote Linux machine.

<sshexec host="${}" username="${remote.username}" password="${remote.password}"
     command="${}" trust="true" />

For example to start tomcat use following tag:

<sshexec host="${}" username="${remote.user}" password="${remote.password}"
         command="sh ${tomcat.home}/" trust="true" />

5. Open an input box and respond to input value:

To open an input dialog use following tag:

<input message="Enter id: " addproperty="">

Here “” in “addproperty” is the variable name which holds input value. Now to check if user has denied to enter value in input:

<condition property="do.abort">
     <equals arg1="n" arg2="${}"/>
<fail if="do.abort">Build aborted by user.</fail>

And if user enters value and press OK then after you can refer to entered value as “${}”.

6. Make an ant call:

<antcall target="targetName">

Here “target” is the name of the target that will be executed.

Note: In above examples all values starting with “${”and ending with “}’’ are variables and you may have to put appropriate values in them to successfully run them.


About harryjoy
A passionate self-taught software developer. Currently working as Lead Software Engineer in an IT company.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: