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]
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 :
<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,
- Copy the build.xml and test.bat (attached below), to your C: directory.Now open the command prompt,cursor pointing to C:\>
- Execute the ant command and the output should be:
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
- sh for shell scripts. Pass the shell script (plus any arguments to the script) as a single command, using -c switch.
- cmd for batch files. Pass the batch files (plus any arguments to the script) as a single command, using /c switch.