my6solutions

asp .net, the social web & other distractions

 

Running Apps


PayPal - The safer, easier way to pay online!

Disclaimer

I am in no way affiliated with Microsoft or Google. I am just another developer trying to make a difference. All opinions and observations are usually my own.

Minifying javascript and css files via a custom msbuild task

Recently, I have been looking into automatically minifying the javascript files being served by the gadget server in pesta. Before this, there were manually copied over from java shindig.

For those needing a good introduction with creating custom tasks using msbuild, this link gives a pretty good introduction and also explains how to set up msbuild as an external tool so that you can edit .proj files in Visual Studio happily. 

In short, the .csproj (C# projects) and .vbproj files that defines a Visual Studio (or developer) project is actually an XML file that is used by MSBuild to build your project or solution.

The final solution is based on this article on "How to create a YUI Compressor MSBuild Task" with some changes. The main change is to have the custom build task in an assembly of its own. This is due to an access problem when running the custom build. Visual Studio 2008 (devenv.exe) will have a lock on the DLL containing the custom Msbuild task. This is irritating as future cleans and builds will fail as Msbuild will complain that another process is using the file (which turns out to be Visual Studio). And the only way to fix this is to restart Visual Studio.

The directory structure of affected projects on disk are as follows

Project Relative location on disk Description
pestaServer ../pestaServer/pestaServer ASP .NET MVC project
pesta.NET.dll ../pesta/pesta Common shared DLL containing javascript files to be minified*
CustomBuildTasks ../pestaServer/CustomBuildTasks DLL containing custom Msbuild task

* the javascript files are set as "EmbeddedResource" here specifically for my own requirements. They don't have to be in your project.

 

What we want to achieve

Before pesta.NET.dll is built, minify the javascript files in pesta.NET.dll and then include these created files into the DLL.

 

pesta.csproj

The following line is added to tell msbuild where to find the custom msbuild task

<UsingTask TaskName="YuiCompress" AssemblyFile="..\..\pestaServer\CustomBuildTasks\bin\$(Configuration)

                      \CustomBuildTasks.dll" />

 

The BeforeBuild target is then updated to the following

  <Target Name="BeforeBuild">

    <Message Text="BeforeBuild Process..." Importance="high" />

    <YuiCompress Files="@(EmbeddedResource)" Type="JS" />

    <CreateItem Include="features\*.*\*.*\*.opt.js;features\*.*\*.opt.js">

      <Output TaskParameter="Include" ItemName="EmbeddedResource" />

    </CreateItem>

  </Target>

 

The YuiCompress task will minify any embedded javascript files and the CreateItem task will include any embedded javascript files in the project path specified by Include into the built DLL. The current custom YuiCompress task is also written to not minify any files if its minified version exist and the main file has not been modified.

The following extends the Clean Build Target so that all minified javascript files (identified by the extension ".opt.js") will be deleted on a Clean. For us, this is simply done by getting the Exec task to executed a delete command.

 

  <Target Name="AfterClean">

    <Message Text="Cleaning up minified javascript files..." Importance="high" />

    <Exec WorkingDirectory="$(ProjectDir)" Command="del *.opt.js /s"   />

  </Target>

 

CustomBuildTasks.dll

This assembly contains the custom msbuild task and the Yui Compressor jar file. The project for CustomBuildTasks can be downloaded via Subversion from https://pesta.googlecode.com/svn/trunk/pesta/CustomBuildTasks.

 

And that's it.

 

Bookmark and Share

Categories: ASP .NET | Pesta
Permalink | Comments (0) | Post RSSRSS comment feed