Matthew Dresser
Matthew Dresser

Reputation: 11442

MSBuild Iterate over properties from a labeled PropertyGroup and retrieve name and value

Is there a way to iterate over the properties of an MSBuild file accessing the name and value of each property within a specific <PropertyGroup>? Perhaps something like this:

<Project ToolsVersion="4.0" DefaultTargets="test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup Label="testPropertyGroup">
    <Property1>Value1</Property1>
    <Property2>Value2</Property2>
  </PropertyGroup>

  <Target Name="prop">
   <Message Text="Name=%(testPropertyGroup.Name)"/>
   <Message Text="Value=%(testPropertyGroup.Value)"/>
  </Target>

</Project>

Upvotes: 3

Views: 2145

Answers (2)

Michael Kauflin
Michael Kauflin

Reputation: 26

If you use an ItemGroup with several subnodes, it works:

<ItemGroup>
  <ApplicationLanguages Include="zh-chs">
    <Name>Chinese.Simplified</Name>
    <IsAppLanguage>true</IsAppLanguage>
  </ApplicationLanguages>
  <ApplicationLanguages Include="zh-cht">
    <Name>Chinese.Traditional</Name>
    <IsAppLanguage>false</IsAppLanguage>
  </ApplicationLanguages>
  <ApplicationLanguages Include="fr">
    <Name>French</Name>
    <IsAppLanguage>true</IsAppLanguage>
  </ApplicationLanguages>
  <ApplicationLanguages Include="de">
    <Name>German</Name>
    <IsAppLanguage>true</IsAppLanguage>
  </ApplicationLanguages>
  <ApplicationLanguages Include="es">
    <Name>Spanish</Name>
    <IsAppLanguage>true</IsAppLanguage>
  </ApplicationLanguages>
</ItemGroup>
<Target Name="PrintValues" Outputs="%(ApplicationLanguages.Identity)">
  <Message Text="Identity: %(ApplicationLanguages.Identity)" Importance="high" />
  <Message Text="Name:     %(ApplicationLanguages.Name)" Importance="high" />
  <Message Text="IsValid:  %(ApplicationLanguages.IsAppLanguage)" Importance="high" />
</Target>

Upvotes: 1

James Woolfenden
James Woolfenden

Reputation: 6681

If i understand you correctly you're after something like the ant equivalent of echoproperties? The only way to get this is to run your msbuild as /verbosity:detailed or /verbosity:diagnostic, however i find that finding info in that output is a little difficult. The best way ive found is to create a target and write message tasks for each property (a long slog i know) and call that in the project as one of the InitialTargets.

<Project ToolsVersion="4.0" 
         DefaultTargets="prop" 
         InitialTargets="CheckProperties"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup Label="testPropertyGroup">
    <Property1>Value1</Property1>
    <Property2>Value2</Property2>
  </PropertyGroup>


  <Target Name="CheckProperties">
    <Message Text="Property1: $(Property1)"/>
    <Message Text="Property2: $(Property2)"/>
  </Target>

  <Target Name="prop">
   <Message Text="Name=%(Names.Identity)"/>
   <!--<Message Text="Value=%(testPropertyGroup.Value)"/>-->
  </Target>

</Project>

Upvotes: 0

Related Questions