Add custom metadata to an already defined item group from another product group

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 only one line, which, you guessed it, is a version of Major.Minor.Build.Revision .

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

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

and MSBuild prints a nice list of file version associations.

Is it possible?

+4
source share
1 answer

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

This causes the goal to be completed for each item in your item group, allowing you to read the contents (i.e. version number) from each individual file and subsequently update that item to add 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> 
+4
source

All Articles