Archive for August, 2009

Ant tip – Handling space in “exec” task.

August 28, 2009 1 comment
<exec executable=”notepad”>
<arg value=”c:\Documents and settings\msthoughts\doc.txt”/>

Ant is a powerful build and script tool provided by Apache Foundation. In a recent project I used the exec task and needed to allow usage of spaces in command line argument of the called executable. A legitimate request, but if you don’t pay attention on the different ‘exec’  parameters syntax, you may waste a lot of time…

Handling space in “exec” task’s arguments.

The exec task of ant allow to execute system command. The arguments of the command are passed as arg sub tags. The sub tag is the arg tag, followed by an attribute named value, line or path. If your argument contains space (for example a file path) do not use the line attribute, instead use the value or path ones. The line attribute will consider the spaces as separator of command line argument for the program executed.

For example you need to pass a text file as command line to a document editing application, and that the file is located under C:\Documents and settings\msthoughts\doc.txt

Using the line attribute:

<exec executable="myapp">
  <arg line="c:\Documents and settings\msthoughts\doc.txt"/>

Will produce an error since the command line is interpreted as 3 args each one denoted with brakets: [c:\Documents] [and] [settings\msthoughts\doc.txt]

To resolve this in command line window you usually use the quotes to wraps the command line arg having space. But using quote from ant will lead to other problem very quickly. The better solution is to use the value attribute instead of the line attribute

<exec executable="myapp">
  <arg value="c:\Documents and settings\msthoughts\doc.txt"/>

Categories: Ant Tags: , ,