Execute batch file or shell script using Ant

This article discusses on how to execute a batch file or shell script using Ant.The article details about using exec command with an example code.We will write an Ant build script, which identifies the current operating system(OS) and based on the OS detected either a batch file or a shell script is executed.We assume that the reader of this article has basic knowledge about Apache Ant tool.

Example Code

Let’s assume we have two scripts, one (.bat file) which executes on windows operating system family, and other (.sh) that runs on all non-windows platforms like UNIX, Linux, etc. Now we need an ant task in build file, to call the appropriate scripts depending on the operating system.

Let’s write a target with named checkOS. This target depends on targets ifOSWindows and ifOSNotWindows.

<target name="checkOS" depends="ifOSWindows, ifOSNotWindows"/>

We then define these targets as:

<target name="ifOSWindows" if="isOSWindows">
	<antcall target="runscript.windows"/>
</target>

<target name="ifOSNotWindows" if="isOSUnix">
	<antcall target="runscript.unix"/>
</target>

We can see that, ifOSWindows and ifOSNotWindows are based on a condition, which if true calls the specific target.

If isOSWindows property,defined as -

<condition property="isOSWindows">
    <os family="windows"/>
</condition>

returns true, then ant target runscript.windows is executed.

Else, ifOSNotWindows is tested using isOSUnix property:

<condition property="isOSUnix">
   <os family="unix" />
</condition>

If this condition returns true, then runscript.unix will be called.

Following is how you can define an ant task to execute a batch script on windows:
To execute a script , exec command is used wrapped inside your target definition. <exec> executes a system command. When the os attribute is specified, then the command is only executed when Apache Ant is run on one of the specified operating systems :

<target name="runscript.windows">
   <echo>This is a Windows machine.</echo>
       <exec dir="." executable="cmd" >
	      <arg line="/c test.bat 'm4 foo.m4 > foo'"/>
   	</exec>
</target>

Following is to run a shell script on UNIX using Ant task:

<target name="runscript.unix">
   <echo>This is an Unix machine.</echo>
	   <exec dir="." executable="/bin/sh">
	     <arg path="-c test.bat 'm4 foo.m4 > foo'"/>
	</exec>
</target>

To try out this,

  1. Copy the build.xml and test.bat (attached below), to your C: directory.Now open the command prompt,cursor pointing to C:\>
  2. Execute the ant command and the output should be:
C:\>ant
Buildfile: build.xml

ifOSWindows:
     [echo] is windows........

runscript.windows:
     [echo] This is a Windows machine.
     [exec] **************************************
     [exec] Hello world!...Printed from batch file. "m4 foo.m4 > foo"
     [exec] **************************************

ifOSNotWindows:

checkOS:

BUILD SUCCESSFUL
Total time: 0 seconds

build.xml

<?xml version="1.0"?>
<project name="checkOperatingSystem" default="checkOS">

	<!--checking if for windows and non windows OS-->
	<target depends="ifOSWindows, ifOSNotWindows" name="checkOS"/>

		<condition property="isOSUnix">
			<os family="unix" />
		</condition>

		<condition property="isOSWindows">
			<os family="windows" />
		</condition>

	<!-- if the OS is windows call the target run.script.windows -->
	<target name="ifOSWindows" if="isOSWindows">
		<echo>is windows........</echo>
		<antcall target="runscript.windows"/>
	</target>

	<!-- if the OS is not windows call the target run.script.unix-->
	<target name="ifOSNotWindows" if="isOSUnix">
		<echo>is unix........</echo>
		<antcall target="runscript.unix"/>
	</target>

	<target name="runscript.windows">
		<echo>This is a Windows machine.</echo>
		<exec dir="." executable="cmd" >
			<arg line="/c /test.bat 'm4 foo.m4 > foo'"/>
		</exec>

	</target>

	<target name="runscript.unix">
		<echo>This is an Unix machine.</echo>
	   	<exec dir="." executable="/bin/sh">
	     		<arg line="-c /test.bat 'm4 foo.m4 > foo'"/>
		</exec>
	</target>
</project>

test.bat

@echo **************************************
@echo Hello world!...Printed from batch file. %1%
@echo **************************************

To summarize,on native UNIX systems, you should be able to run shell scripts directly. On systems running a Unix-type shell (for example, Cygwin on Windows) execute the (command) shell

  1. sh for shell scripts. Pass the shell script (plus any arguments to the script) as a single command, using -c switch.
  2. cmd for batch files. Pass the batch files (plus any arguments to the script) as a single command, using /c switch.

Comments

comments

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Comments

  1. I am my bat file its running fine but my bat file during runtime asking for some run time input like source directory path and like do u want to migrate flat file then i need to pass arguments either in yes or no …so simply i want to know how i can achieve this…
    Please help me

Speak Your Mind

*