【问题标题】:How do I set properties for items in an `ItemGroup` based on the name of the item being added?如何根据要添加的项目名称为“ItemGroup”中的项目设置属性?
【发布时间】:2020-01-09 17:18:34
【问题描述】:

假设我的 msbuild 文件中有以下 ItemGroup

<ItemGroup>
  <!-- build all the .proto files -->
  <MyGroup Include="**/*.txt" MyProperty="[something here to extract metadata for each item]" />
</ItemGroup>

在填充 itemgroup 时,我可以在括号中添加什么来设置属性?具体来说,我想获取文件的项目相对路径(没有文件名)。这样的事情可能吗?

【问题讨论】:

    标签: msbuild


    【解决方案1】:

    您可以在项目中使用well-known item metadata

    <Project>
      <ItemGroup>
         <TextFiles Include="**/*.txt"
             MyProperty="Included file %(Filename)(Extension: %(Extension)) in directory %(RelativeDir)" />
      </ItemGroup>
      <Target Name="ListTextFiles">
        <Message Importance="high" Text="@(TextFiles->'%(Identity): %(MyProperty)', '%0A')" />
      </Target>
    </Project>
    

    文件结构为

    fileA.txt
    SomeSubfolder\fileB.txt
    SomeSubfolder\fileC.txt
    

    打印:

    >dotnet msbuild -t:ListTextFiles -nologo
      fileA.txt: Included file fileA(Extension: .txt) in directory
      SomeSubfolder\fileB.txt: Included file fileB(Extension: .txt) in directory SomeSubfolder\
      SomeSubfolder\fileC.txt: Included file fileC(Extension: .txt) in directory SomeSubfolder\
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多