将自定义元数据从另一个ItemGroup添加到已定义的ItemGroup [英] Add Custom Metadata to Already-defined ItemGroup from Another ItemGroup

查看:68
本文介绍了将自定义元数据从另一个ItemGroup添加到已定义的ItemGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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

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

其中每个file.version文件仅包含一行,即-您猜对了-格式为Major.Minor.Build.Revision的版本.

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

我希望能够通过将Files项组中的每个项添加为元数据来将其与_Version相关联,这样我可以执行以下操作:

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)" />

并让MSBuild打印出不错的文件版本关联列表.

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

这可能吗?

推荐答案

这可以通过使用目标批处理将Version成员添加到元数据中来实现.这涉及使用@(Files) ItemGroup作为输入,将您的ReadLinesFromFile操作移至其自己的目标.

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.

这将导致对ItemGroup中的每个项目执行目标,从而允许您从每个文件中读取内容(即版本号),然后更新该项目以添加Version元数据:

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>

这篇关于将自定义元数据从另一个ItemGroup添加到已定义的ItemGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆