Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild Filtering ItemGroup of files with condition

Tags:

msbuild

This feels like it's so simple, but I cannot get it to work.

All I'm trying to achieve is a filtered list of the embedded resources. I've tried various approaches but I can't seem to get it right.

Here's what I thought was the right solution:

<ItemGroup>
  <AllEmbeddedResources Include="@(EmbeddedResource)" Condition="$(FullPath.Contains('Change')"/>
</ItemGroup>

Edit... To clarify, the results are without the condition, the list is all embedded resources, with the condition, the group is empty.

I've tried this inside and outside of target's, and I've tried getting the full list in one group, and then filtering in a separate group. I know I'm just misunderstanding some fundamental part of msbuild syntax, I just can't seem to work it out. Looking forward to being shown my stupid mistake!

like image 678
Adam Avatar asked Sep 12 '25 02:09

Adam


1 Answers

Inside a target, this can be done using the batching syntax for items and using the System.String.Copy method to be able to call instance functions on the string:

<Target Name="ListAllEmbeddedResources">
  <ItemGroup>
    <AllEmbeddedResources Include="@(EmbeddedResource)" Condition="$([System.String]::Copy(%(FullPath)).Contains('Change'))" />
  </ItemGroup>
  <Message Importance="high" Text="AllEmbeddedResources: %(AllEmbeddedResources.Identity)" />
</Target>

Note that this syntax only works inside a target and not during static evaluation (item group directly under the <Project> node).

like image 178
Martin Ullrich Avatar answered Sep 14 '25 21:09

Martin Ullrich