Add Custom Metadata to Already-defined ItemGroup from Another ItemGroup

穿精又带淫゛_ 提交于 2019-12-19 19:55:10

问题


I have the following:

<ItemGroup>
  <Files Include="C:\Versioning\**\file.version" />
<ItemGroup>

<ReadLinesFromFile File="%(Files.Identity)">
  <Output TaskParameter="Lines" ItemName="_Version"/>
</ReadLinesFromFile>

where each file.version file contains simply one line which is - you guessed it - a version of the form Major.Minor.Build.Revision.

I want to be able to associate each item in the Files ItemGroup with its _Version by adding the latter as metadata, so that I can do something like:

<Message Text="%(Files.Identity): %(Files.Version)" />

and have MSBuild print out a nice list of file-version associations.

Is this possible?


回答1:


This can be achieved by using target batching to add your Version member to the metadata. This involves moving your ReadLinesFromFile operation to its own target, using the @(Files) ItemGroup as an input.

This causes the target to be executed for each item in your ItemGroup, allowing you to read the contents (i.e. version number) from each individual file and subsequently update that item to add the Version metadata:

<Project DefaultTargets="OutputFilesAndVersions" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
      <Files Include="C:\Versioning\**\file.version" />
    </ItemGroup>
    <Target Name="OutputFilesAndVersions" DependsOnTargets="RetrieveVersions">
        <Message Text="@(Files->'%(Identity): %(Version)')" />
    </Target>
    <Target Name="RetrieveVersions" Inputs="@(Files)" Outputs="%(Files.Identity)">
        <ReadLinesFromFile File="%(Files.Identity)">
          <Output TaskParameter="Lines" PropertyName="_Version"/>
        </ReadLinesFromFile>
        <PropertyGroup>
            <MyFileName>%(Files.Identity)</MyFileName>
        </PropertyGroup>
        <ItemGroup>
            <Files Condition="'%(Files.Identity)'=='$(MyFileName)'">
                <Version>$(_Version)</Version>
            </Files>
        </ItemGroup>  
    </Target>
</Project>


来源:https://stackoverflow.com/questions/7420812/add-custom-metadata-to-already-defined-itemgroup-from-another-itemgroup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!