31

I can compile a .cs file referenced by PropertyGroup:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <AssemblyName>MSBuildSample</AssemblyName>
        <OutputPath>Bin\</OutputPath>
        <Compile>helloConfig.cs</Compile>
    </PropertyGroup>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <Csc Sources="$(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"/>
    </Target>        
</Project>

or do the same thing using ItemGroup:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    
    <ItemGroup>
        <Compile Include="helloConfig.cs" />
    </ItemGroup>

    <PropertyGroup>
        <AssemblyName>MSBuildSample</AssemblyName>
        <OutputPath>Bin\</OutputPath>
    </PropertyGroup>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"/>
    </Target>  
</Project>

I know that using ItemGroup should be the preferred method, but what's when I should use each one of these properties?

Yar
  • 7,020
  • 11
  • 49
  • 69

1 Answers1

32

Think of property group as a collection of individual variables, properties can only hold one value.

Whereas itemgroup is similar to an array or collection which can hold zero, one or many values. You can also iterate item groups which is often useful when you want to carry out the same task against multiple items. A common example of this is compiling many files.

David Martin
  • 11,764
  • 1
  • 61
  • 74
  • 6
    ItemGroup values can also have multiple attributes, including having a defined "schema". Properties are strictly single string values. – Richard Szalay Feb 17 '16 at 10:09
  • @David good explanation on the difference. I felt that I could use ItemGroup for my usecase "Copy files/dirs from different source paths to deployment path with same subdirs" ie. copy xyz\*.dll to deploy\xyz\*.dll; copy abc\images\*.jpg to deploy\abc\images\*.jpg (Here, I want to use a single task that could be utilized to perform my 100s of such copies) Can you show me how that works? (Detailed reqd is explained at https://stackoverflow.com/questions/53544528/build-script-to-copy-files-from-various-source-folder-to-various-destination-fol/53547258#53547258) – OmGanesh Nov 30 '18 at 12:58