Ant
no notes

Web Application Project Publish (part 1)

I would like to have a publish option which would ...
Build
Compress JavaScript and CSS files
Overwrite my dev web.config with one for live
Publish to FTP, network drive, etc

Also, its important that I don't have to do any extra work every time I publish and it should not effect build and debug.

So I started to investigate the way the current one works, in the hopes that I could add a small bit of code to do the compression and web.config selection.

A project (*.proj) file is an MSBuild file containing the locations of contents and code files for an application along with instructions on how to build it. There are extension points in a project file where you can add your own steps - bingo!

<Target Name="AfterBuild">
 ... (add code here)
</Target>

There are a number of standard Targets which you can use, which will be triggered and run your msbuild instructions, there is even BeforePublish and AfterPublish. So I downloaded the YUICompressor and wrote an Exec command to call it.

<Target Name="BeforePublish">
 <ItemGroup>
  <Compress Include="**\*.js"/>
 </ItemGroup>
 <Exec Command="java -jar w:\yuicompressor.jar --charset utf-8 %(Compress.FullPath) -o %(Compress.RootDir)%(Compress.Directory)%(Compress.FileName).min%(Compress.Extension)" />
</Target>

(note: charset was important, illegal char exception without it)

This will create min.js files for all of my JavaScript files in the project.

But... the BeforePublish and AfterPublish targets are never hit, and if I put the code into, say, AfterBuild it happens every time so thats no good.

Moving on then... the next step was to investigate how the publish part happens. It must be an MSBuild task... right?

There is a special set of Targets for web applications stored in Microsoft.WebApplication.targets which is in Program Files\MSBuild\.. this is imported in the .proj file and contains a target for copying a web application, perhaps this is used by the publish option and adding to it would give me the result I want? To test that it is used, I removed the import in my .proj file and did a publish - no change, it seems the publish option does not use MSBuild for the actual publish - scuppered.

There are other options; You can use MSBuild or NAnt from the command line or you can compress using HTTP Handlers and Modules in you application as you go.

I'm off to check out MSBuild now...

Some references
MSBuild Blog
MSBuild Reference
MBCompression - Handlers & Modules
YUICompressor MSBuild Task

Post a Note

(required)

(required never shown)

On Twitter Follow MrAntix on Twitter

11 hours ago
verge
Microsoft teases Windows 8 'Consumer Preview' with Bing betta fish site http://t.co/lcJICazH