• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Execute batch file or shell script using Ant

January 25, 2013 //  by Manisha Patil//  Leave a Comment

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.

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

We then define these targets as:

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

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

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

If isOSWindows property,defined as –

[java] <condition property="isOSWindows">
<os family="windows"/>
</condition>
[/java]

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

Else, ifOSNotWindows is tested using isOSUnix property:

[java] <condition property="isOSUnix">
<os family="unix" />
</condition>
[/java]

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 :

[java] <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>
[/java]

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

[java] <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>
[/java]

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:
[java] 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
[/java]

build.xml

[java] <?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>
[/java]

test.bat

[java] @echo **************************************
@echo Hello world!…Printed from batch file. %1%
@echo **************************************
[/java]

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.

Category: Apache AntTag: ANT

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.

Previous Post: «jquery jQuery Event Handling – Key Events
Next Post: jQuery Event Handling – Form Events jquery»

Reader Interactions

Leave a Reply Cancel reply

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

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact