MSBuild Task Reference and Ajax Minification


I’m not one for dumping all scripts into a single folder and calling it a day.

The template project for ASP.NET MVC 3 does that unfortunately. So, after a bit of cleanup and adding a few more JavaScript libraries that I needed, I’m left with a reasonably organized folder structure.

I’ve added a folder for JavaScript files associated with Views, in this case, it’s located here: Scripts\Views\ViewName\…

I want to create minified versions of scripts, yet not minify scripts that have already been minified by other processes. The Microsoft Ajax Minifier (download here) includes a msbuild task, making the effort of creating minified scripts painless.

I followed these basic instructions for adding the build task:

I did not however, as I mentioned, want to compress all JavaScript files.

So, based on this documentation (MSBuild Items), I changed the basic Include settings to reflect my folder structure.

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
  <Target Name="AfterBuild">
      <JS Include="Scripts\Views\\**\*.js" Exclude="Scripts\Views\\**\*.min.js;" />
      <CSS Include="**\*.css" Exclude="**\*.min.css" />
    <AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />

What I wanted specifically, is to only process JavaScript files found in the Scripts\Views subfolders. So, I changed the Include attribute to:


Roughly translated to English, the above:

  1. Recursively, find all JavaScript files under the folder Scripts\Views
  2. Skip anything that is already named *.min.js
  3. Minify.