Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split an ItemGroup's Excludes into multiple lines?

Tags:

msbuild

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;&#xD;&#xA;                   obj\**\*.*;&#xD;&#xA;                   **\_references.js;&#xD;&#xA;                   [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?

like image 848
Scott Weldon Avatar asked Aug 22 '26 13:08

Scott Weldon


1 Answers

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.

like image 146
Scott Weldon Avatar answered Aug 25 '26 04:08

Scott Weldon