Sunday, December 09, 2007

MSBuild Exec Task fail if we have any space in the command

To execute an external application using MSBuild, I use the built in Exec task. A problem we all face is spaces in command line we execute. and this makes MSBuild unhappy , the task will fail.

To correct that, we should surround the command path and any paramters with quotation, a trick from old DOS days, but this also will not work as MSBuild is an XML file and " is reserved for attributes.

The trick here is to use the " in MSBuild scripts whenever you need a " , this makes your script really ugly, here I found another guy who rasie the issue to an upper level.

 

My prefered way to do that is to have the external application and each parameter in a separate property; just to make the command line itself more readable.

 

<Project>
 <PropertyGroup>
    <ToolPath>&quot;my-external-application.exe&quot;</ToolPath>
    <Param1>&quot;my-param-value&quot;</Param1>
    <Param2>&quot;my-param-value&quot;</Param2>
 </PropertyGroup>

<Target Name="Run-External-App">
 <Exec Command="$(ToolPath) $(Param1) $(Param1)"/>
</Target>

</Project>