3

有没有办法遍历 MSBuild 文件的属性,以访问特定的每个属性的名称和值<PropertyGroup>?也许是这样的:

<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>
4

2 回答 2

1

如果您使用具有多个子节点的 ItemGroup,它可以工作:

<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>
于 2013-05-10T12:07:13.877 回答
0

如果我理解正确,您是否在追求类似于 ant 的 echoproperties 之类的东西?实现这一点的唯一方法是将您的 msbuild 作为 /verbosity:detailed 或 /verbosity:diagnostic 运行,但是我发现在该输出中查找信息有点困难。我发现的最好方法是为每个属性创建一个目标并编写消息任务(我知道这是一个漫长的过程),并在项目中将其称为 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>
于 2013-05-10T09:33:16.897 回答