Search code examples
.netmsbuildcsprojtargetsproj

Msbuild ItemGroup exclude doesn't work with wildcards


This Itemgroup ItemsFromAnotherTarget contains:

..\..\References\AnotherFolder\ReferencedAssembly.dll
bin\GeneratedAssembly1.dll
bin\GeneratedAssembly2.dll
somefolder\somefile.txt
somefolder\somefile.exe
bin\anexe.exe

The idea is to generate another item group BinaryFiles containing

bin\GeneratedAssembly1.dll
bin\GeneratedAssembly2.dll
somefolder\somefile.exe
bin\anexe.exe

So I have the following:

<ItemGroup>
    <BinaryFiles Include="@(ItemsFromAnotherTarget)" Condition="'%(Extension)'=='.dll' or '%(Extension)'=='.exe'" Exclude="..\..\References\AnotherFolder\ReferencedAssembly.dll" />
</ItemGroup>

So this generates the required item group. But if we replace the Exclude with a wild card, it doesn't work.

Exclude="..\..\**\References\**"
Exclude="..\..\References\**\*.dll"
Exclude="..\..\References\**\*"
None of these work.

The issue is the References folder might have multiple folders and dlls, we need to exclude the whole References folder. Any idea how to do the filtering using a wild card?


Solution

  • The only way I could get to exclude References folder is by Regex. It seems sort of hacky and any other suggestion is welcome.

    <ItemGroup>
        <BinaryFiles Include="@(ItemsFromAnotherTarget)" Condition="(!$([System.Text.RegularExpressions.Regex]::IsMatch('%(Identity)', `.\\References\\.`))) and ('%(Extension)'=='.dll' or '%(Extension)'=='.exe')" />
    </ItemGroup>