tags:

views:

71

answers:

1

I have an AfterCompile target defined in my csproj which involves minifying and combining JS and CSS. I then add them to ItemGroup Content and remove the unnecessary files, however the Remove paramter does not seem to work.

<Target Name="AfterCompile">
  <ItemGroup>
    <JS_Combine Include="js\c??.*.min.js" />
    <CSS_Combine Include="css\c??.*.min.css" />
  </ItemGroup>
  <!-- Combine JS -->
  <ReadLinesFromFile File="%(JS_Combine.Identity)">
    <Output TaskParameter="Lines" ItemName="JSLines" />
  </ReadLinesFromFile>
  <WriteLinesToFile File="js\combined.min.js" Lines="@(JSLines)" Overwrite="true" />
  <!-- Combine CSS -->
  <ReadLinesFromFile File="%(CSS_Combine.Identity)">
    <Output TaskParameter="Lines" ItemName="CSSLines" />
  </ReadLinesFromFile>
  <WriteLinesToFile File="css\combined.min.css" Lines="@(CSSLines)" Overwrite="true" />
  <!-- Tidy up -->
  <ItemGroup>
    <Content Include="js\combined.min.js" />
    <Content Include="css\combined.min.css" />
    <Content Remove="@(JS_Combine)" />
    <Content Remove="@(CSS_Combine)" />
  </ItemGroup>
      <!-- DEBUG message -->
  <Message Text="DEBUG: @(Content)" Importance="high" />
</Target>

The debug message still shows @(Content) as having the unnecessary js files. Can anyone tell me what's happening?

A: 

In order to recreate you situation I created this sample script

<Project DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;

  <Target Name="Demo">
    <ItemGroup>
      <JS_Combine Include="js\c01.min.js;js\c02.min.js;js\c03.min.js;" />
      <CSS_Combine Include="css\c01.min.css;css\c02.min.css;css\c03.min.css;" />
    </ItemGroup>
    <ItemGroup>
      <Content Include="@(JS_Combine);@(CSS_Combine)"/>
    </ItemGroup>

    <Message Text="Content Before: @(Content)" Importance="high" />
    <!-- Tidy up -->
    <ItemGroup>
      <Content Include="js\combined.min.js" />
      <Content Include="css\combined.min.css" />
      <Content Remove="@(JS_Combine)" />
      <Content Remove="@(CSS_Combine)" />
    </ItemGroup>

    <Message Text="-------------------------"/>
    <Message Text="Content After: @(Content)" Importance="high" />
  </Target>

</Project>

It works for me here is the results:

Project "C:\Data\Development\My Code\Community\MSBuild\RemoveTest\Remove01.proj" on node
1 (default targets).
Demo:
  Content Before: js\c01.min.js;js\c02.min.js;js\c03.min.js;css\c01.min.css;css\c02.min.c
  ss;css\c03.min.css
  -------------------------
  Content After: js\combined.min.js;css\combined.min.css
Done Building Project "C:\Data\Development\My Code\Community\MSBuild\RemoveTest\Remove01.
proj" (default targets).

Are you still having issues with this?

Sayed Ibrahim Hashimi