I have a custom MSBuild task to minify some JavaScript files. I created an ItemGroup to define which files I want to be minified, and which should not be. I have the following in my .csproj file:
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js;obj\**\*.*;**\_references.js;[snip]" />
</ItemGroup>
I want to split that property into several lines for better readability (the snipped part is long and could get longer in the future), so I tried this:
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js" />
<JS Exclude="obj\**\*.*" />
<JS Exclude="**\_references.js" />
[snip]
</ItemGroup>
But that gave me this build error:
error MSB4066: The attribute "Exclude" in element <JS> is unrecognized.
The same occurred when I added an empty include (Include="") in those subsequent elements. (Putting something inside the quotes removed the error, but included extra "files" in the JS var.)
I then learned from the docs for MSBuild Items that the Exclude attribute only affects items added by the Include attribute in the same element.
I also tried using only one Exclude string, but splitting the string itself into multiple lines, like this:
<ItemGroup>
<JS Include="**\*.js"
Exclude="**\*.min.js;
obj\**\*.*;
**\_references.js;
[snip]" />
</ItemGroup>
That looks okay, but when I subsequently saved the project from Visual Studio, the line endings were mangled, so it turned into this:
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js;
 obj\**\*.*;
 **\_references.js;
 [snip]" />
</ItemGroup>
(This didn't break anything in the build, but defeats the purpose of splitting the string into multiple lines.)
How can I split these excludes into multiple lines?
I found this answer about excluding files from Content, which is also part of an ItemGroup. So I tried that:
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js" />
<JS Remove="obj\**\*.*" />
<JS Remove="**\_references.js" />
[snip]
</ItemGroup>
That worked. JS now contains only the files I want, and the csproj file is a bit more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With