Using MSBuild to deploy visual studio 2005 web applications

September 20, 2007 20:48 by Rob

Note: Please check out this update for an easier solution.

Today was cool. I managed to get my first continuous build server online at work using CruiseControl.net. I was cool, up to the point when I wanted to deploy the application nice and clean.

After a lot of googling, I found out that Visual Studio internally uses a .targets file to deploy web applications. It is (by default) referenced in your .csproj file of your web application so you don't have to add it to your solution file (which fortunately IS a msbuild .targets file with just another name). The target file is located here in C:\Program Files\MSBuild\Microsoft\VisualStudio\v8.0\WebApplications. You don't need to install Visual Studio on your build server, you just need to copy the MSBuild directory to the program files folder of your build server. You will however need to install the 2.0 SDK on the build server. it includes a hidden target named '_CopyWebApplication' which you can call.  Thanks to links here and here.

To deploy your web application, just enter the following commandline with your current directory set to the project directory:

msbuild /t:_CopyWebApplication

Unfortunately, this is not sufficient, because the build file detects if the output directory is the same as the current directory:

<Target Name="_CopyWebApplication" Condition="'$(OutDir)' != '$(OutputPath)'">

This problem is easily solved:

msbuild /t:_CopyWebApplication /property:OutDir=..\Drop\ /property:WebProjectOutputDir=..\Drop\

Which will effectually copy all the files in the directory ..\Drop\ and remove the added _PublishedWebsites path, 

but Alas! There is a 'problem' with this solution because references DLL's are not being copied over. I did a lot of googling again and i managed to solve it.

You need to change the line to:

<Target Name="_CopyWebApplication" Condition="'$(OutDir)' != '$(OutputPath)'" DependsOnTargets="ResolveAssemblyReferences">

By making the _CopyWebApplication dependant on the the ResolveAssemblyReferences target (which is a built-in target), the correct properties @(ReferenceCopyLocalPaths) has been magically filled and all your files will be nicely copied to your drop directory. I guess that this is a nifty bug in the web project build file.

There you have it. All that remains is to add another MSBuild task to your Cruisecontrol.net configuration file that publishes the result. I leave that as an exercise to the interested reader because my dinner is ready.


Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Rajeev

October 1, 2007 04:59

Hi,

I need to build and deploy my web service using MSBUILD can give me some sample of how to do it. I am confused.

Thanks

Rajeev us

rob planet

October 2, 2007 18:52

To build and deploy, you need to execute two statement on the commandline; one to build and one to deploy. Note that this only works with ASP.Net 2.0 Web Project applications.

First, start a command prompt and change directory to the working folder of your visual studio project. I assume this is a web application project. I also assume that you have the path to msbuild set up in your environment variables. Alternatively, start a visual studio 2005 command prompt, which does this automatically:

cd [path]
msbuild /t:Build

msbuild /t:ResolveAssemblyReferences,_CopyWebApplication /p:OutDir=Drop /p:WebOutputDir=Drop

This should give you a build in the 'Drop' folder that is easily copied to an ftp site for example.

rob planet

Rajeev

October 2, 2007 19:58

Hi,

I know this is a very stupid question but I am new to Msbuild world do I need to create a .proj,.target and .property file

How can I start from scratch to build a build script as I need to do some task like deleting files if they exist in build folder, getting the latest version from VSS,creating source files,building,deploying to iis

Thanks

Rajeev us

Rob

October 2, 2007 20:50

If you need info about msbuild, do some googling with 'msbuild syntax' or something like that. That should help you get started.

I usually use visual studio to set up the solution, and make enhancements to the build script (as visual studio project IS a msbuild script).

To be able to deploy IIS and Sourcesafe interaction, I suggest checking out the msbuild community tasks (http://msbuildtasks.tigris.org/) which can supply you with a lot of additional options to do stuff from within msbuild scripts.

Rob nl

pingback

April 28, 2010 15:38

Pingback from devezine.com

Using MSBuild to deploy visual studio 2005 web applications

devezine.com