Recently, I was tasked with building an installer for the project I'm currently on. The solution we had built consisted of a Windows application that hosted a web browser control, a web application that actually did all the work, several class libraries and two Windows services that both the Windows application and web application communicated with.
Making an installer/setup project for each component wasn't an option. We needed one installer. One that would not only install the windows application, but also check to see if certain components like MSMQ and IIS are installed and configured properly, deploy the website to the machine, install the two Windows services and some other things.

Unfortunately, one msi cannot call another msi file (only one is allowed to run at any given time), so my solution was to create an msi installer project in VS for the Windows application and add a Custom Action to launch a console application that would take care of the rest. So I created a Solution folder called Setup which contained a Setup project and the Console application and also a Web Deployment project.

The Web Deployment project is an add-on that MS created that among other things (like being able to use 1 dll for the website instead of the dynamically generated "nameless" dlls a normal build creates) packages up a website project and allows you to "deploy" it. But this wasn't going to work for us, because I wanted it to build/package it up but not deploy it. It would be "deployed" by my console app. So how was I going to do this? If only there was a way I could zip up the files in the website, then I could include them as an embedded resource of the console app and have it extract it out to the machine upon installation. But running the Web Deployment project and then zipping up the files it creates and putting it into the console app's file structure/project manually was too cumbersome and wouldn't have passed muster. What to do, what to do?

This brings us to the point of this article. Well, knowing that VS uses MSBuild as its build engine, and having delved ever so slightly into MSBuild itself previously, I was convinced there must be some way of doing this. I looked at all the options available to MSBuild and there wasn't a zip facility like there is with NAnt. Well now what?

A quick search on the net for "Zip" and "MSBuild" brought me to a site that hosts an open-source project called MSBuild Community Tasks. One of the tasks is a Zip task that will Zip up files for you. Perfect! So how to do what I did? Well download the tasks and install them as normal. (These tasks will work on any project that contains an MSBuild proj file (any type except Website project)).

So, first I right clicked on my deployment project and chose to "Open Project File" (for other project types you have to "Unload Project" first, then choose to "Edit" it) . I changed my Web Deployment project to Output to a single folder, regardless of the build configuration:

So first, I've added a Target called AfterBuild. MSBuild recognizes this as a default and knows it's going to do this after it's finished building the website. I create an ItemGroup and create a group called ZipFiles that sets up some parameters, namely to include every file in the output folder excluding any zip files.
Then the Zip task, takes that item and uses it as it's Files property, sets the Working Directory (so paths will be relative to that directory) names the ZipFile and how many levels down it should go to zip.

Next we have to get that zip file into our console app project. If this is the first time we've built this, it doesn't exist, but assuming we've built the solution before, and that we're under source control, we need to delete the existing zip file in the destination(console app) directory. The Attrib task lets us set the file to Normal (not Read-Only). Then we delete the file (as the Delete task suggests). Then we move the newly built zip file and reset it's attribute to Read-Only.

Finally, since we no longer need it, we remove our OutputPath directory (the one created by the web deployment build). Now, if this is the first time we've done this, the only thing left to do is to pull the zip file into the console app's project (via Add...Existing Item...), set it's Build Action to Embedded Resource and check in the project file.