View RSS Feed

My Java Tips

Ant exec task

Rate this Entry
by , 11-07-2011 at 06:19 PM (3428 Views)
To execute a system command from Ant, exec is used. Since different operating systems have different commands, therefore OS commands (if used), will restrict the build script to a particular operating system.

One cannot interact with the forked program and if any attempt to read input in the forked program is made, an EOF (-1) will be thrown.

The task delegates to Runtime.exec which in turn apparently calls ::CreateProcess. Generally, batch files (.bat) cannot by executed directly. You need to execute the command shell executable cmd using the /c switch.

<target name="help">
<exec executable="cmd">
<arg value="/c"></arg>
<arg value="ant.bat"></arg>
<arg value="-p"></arg>

Review the following example:

<exec dir="${src}" executable="cmd.exe" os="Windows 2000" output="dir.txt">
<arg line="/c dir"></arg>

Let me briefly explain the parameters of exec:

dir - specifies the directory in which the command should be executed.
executable - specifies the command to execute without any command line arguments.
os - specifies the Operating Systems on which the command may be executed.
output - specifies the file to which the output of the command should be redirected.

Hope this helps.

Submit "Ant exec task" to Facebook Submit "Ant exec task" to Digg Submit "Ant exec task" to Submit "Ant exec task" to StumbleUpon Submit "Ant exec task" to Google

Tags: None Add / Edit Tags
Ant , XML