msbuildmsbuild-taskmsbuild-propertygroup

how to pass variable number of items in PropertyGroup/ItemGroup to msbuild script


Please read till I state my question clearly. I'm trying to implement a custom msbuild task that will accept a variable number of input parameters from a msbuild script. I am aware of arrays of input parameters in customs tasks

public ITaskItem[] ArrayofItems { get; set; }

These can be declared as follows using either propertygroup/itemgroup

<PropertyGroup>
<Item1>1</Item>
<Item2>2</Item>
<Item3>3</Item>
<Item4>4</Item>
<Item5>5</Item></PropertyGroup>

 <ItemGroup>
<File Include="1"></File>
<File Include="2"></File>
<File Include="3"></File>
<File Include="4"></File>
<File Include="5"></File>  </ItemGroup>

Then from VS 2010 command line I can set/override the property as following

msbuild somefile.csproj /t:MyTarget /p:Item1=Name1;Item2=Name2...etc

My question is :- Is it possible to declare variable number of propertygroup/itemgroup in the build file so that I can pass in 'n' variable parameters from msbuild command line something like this using propertygroup/itemgroup?

msbuild somefile.csproj /t:MyTarget /p:Item1=Name1;Item2=Name2;ItemN=NameN ('N' Only for illustration purposes)

Is this even possible?

Thanks in advance, Any help will be greatly appreciated.


Solution

  • If you call your build project like this...

    > msbuild My.proj /p:ItemProperty="1;2;3;4;5"
    

    And the project does this...

    <ItemGroup>
        <FromProperty Include="$(ItemProperty)" />
    </ItemGroup>
    <Message Text="%(FromProperty.Identity)" />
    

    ...you have essentially converted a property into an array of items. If you were to convert to dymanically created properties, there would be no easy way to reference them in the rest of your script, since you wouldn't know their names ahead of time when the script is authored.

    Excerpted from MSBuild Trickery, trick #30 which has many pages of additional detail on this manipulation