Craig
Craig

Reputation: 1940

How can I assign a value to a custom MSBuild task's string[] property, using the command line?

I have a custom MSBuild task (that extends Microsoft.Build.Utilities.Task). This task has a property of type string[]. The task itself (let's call it 'BobTask') is in a MSBuild file that sets its value as follows:

<BobTask MyStringArrayProperty="@(MyStringArrayProperty)" />

My intention is that when a user runs the MSBuild file from the command line (using MSBuild.exe), they can set the property like so:

msbuild.exe file.proj /p:MyStringArrayProperty="value1"

or

msbuild.exe file.proj /p:MyStringArrayProperty="value1;value2"

(and yes, I know the command line samples above are incomplete - it's just to indicate my intent.)

However, when I try this for real, the MyStringArrayProperty in my custom task is always null. I see that other MSBuild tasks use string[] properties so that seems like a valid property type for custom tasks. This leads me to believe the syntax I'm using for the string list on the command line is incorrect. Thus, is there a way to assign a value to a custom MSBuild task's string[] property, using the command line? Is there some special syntax I need to use?

Thanks,

-Craig

Upvotes: 1

Views: 1635

Answers (1)

Brian Kretzler
Brian Kretzler

Reputation: 9938

You are supplying the value of a property on the command line, but supplying the contents of an item array to your task; the two have the same name but are a different datatype.

To convert the property to an item, do this:

<ItemGroup>
    <MyStringArrayProperty Include="$(MyStringArrayProperty)" />
</ItemGroup>
<BobTask MyStringArrayProperty="@(MyStringArrayProperty)" /> 

Excerpted from the book "MSBuild Trickery" tip #30: "How to convert properties into items"

Upvotes: 2

Related Questions