How to publish a web site with MSBuild

How to publish a web site with MSBuild

In part two of my MSBuild tutorial I needed to find a way to call Visual Studio’s Publish Web Site feature from MSBuild. Much trawling of the interweb failed to find anything of use, so in the end I had to produce my own target which copied the relevant files into the output folder.

The problem with this approach is that it only works for files of type dll, aspx or config. It is a simple task to add an extension, for example png, however on larger projects this becomes impractical. Developers would have to check the build script each time they added or removed a file just in case the Publish target needed to be updated. These are just the sort of jobs that get forgotten, which can lead to invalid builds later on.

Fortunately I came across a post on Mirosław Jedkynak’s blog showing how to use MSBuild to publish a web site. As some of you may be aware, a project file (vbproj or, in the case of my demo, csproj) contains targets of its own, as well as importing targets from other files that come as part of a Visual Studio installation. One such file is Microsoft.WebApplication.targets. This file provides the _CopyWebApplication target which will effectively replace my home-brewed numberswiki.com

Publish target.

In order to make use of this target we need to pass it two properties, WebProjectOutputDir and OutDir, which will ensure that the files get published into the correct folder. Here is an example

<Target Name="Publish">
  <RemoveDir Directories="$(OutputFolder)"
             ContinueOnError="true" />
  <MSBuild Projects="BuildDemoSite.csproj"
           Targets="ResolveReferences;_CopyWebApplication"
           Properties="WebProjectOutputDir=$(OutputFolder);
           OutDir=$(WebProjectOutputDir)\" />
</Target>

As you can see the ResolveReferences target is also called, this ensures that any third party dependencies are copied over as well.

Integrating this into my demo build script was simple, however I noticed that some files were being copied over that I didn’t want. These were the build script itself, and the environment-specific config files. This is because their build action was set to Content. Once I had switched it to None and ran the script again, everything was fine. The build action can be set by right-clicking on a file in the Solution Explorer and selecting Properties. Build Action is the first item in the list.

I have posted a new version of the build script to CodePlex for those interested in taking a look.

Technorati tags:

Tags: ,

31 Responses to “How to publish a web site with MSBuild”

  1. We use the same procedure; however, I have found that if you specify /p:Configuration:Release on the MSBuild command line a slightly different build results which will not pass our web tests. Do you know anything about this? Setting it to Debug or completely removing the switch creates useable code.

  2. Mike, can you provide a little more info with regards to the build being “slightly different”? Also which web tests fail? I must admit I have not seen this problem before.

  3. [...] i started to look at MSBuild and see if it could handle my Publish task and i found this great article. It explains how to make a build file that build your project and then also simulate the publish [...]

  4. Paru says:

    Hi,
    I’m an tester and very new to MSBuild + for web based project(dotnet)
    I need to do publish, the initial examples provided in Part one and part two executed smoothly with out any error on implementing to my project.
    But on trying the new code for publish the following error is displayed,
    D:\src\LITE.sln : error MSB4057: The target “ResolveReferences” does not exist in the project.
    Can u pls provide me the solution?? and also let me know Is it not possible to publish using .sln ? does it only work for .csproj?

  5. Hi Paru,

    You need to be using a web application project file for that to work, not an sln file.

    Ross

  6. daw says:

    can i publish a website in msbuild using .sln file…

    Following cmd doesn’t seem to work

    msbuild xyz.sln /p:Configuration=Release

    can u assist ?

    Thanks.

  7. daw says:

    To add to the above

    I would like to replicate the following in msbuild

    rightclick on VS2008 in project and select ‘publish website’

  8. If you want replicate the VS publish website feature in MSBuild, this article shows you how :-) But, in response to your previous comment, you can’t pass an sln file to MSBuild – it must be a web project file in order to publish a website.

  9. Nicolay says:

    I succedded to run Build.xml, but had some problems:
    1. Directory C:\Program Files\MSBuild\Microsoft\VisualStudio\v9.0\WebApplicationswas not exists on my development machine, I copied it from another machine manually.

    2. MSBuild.Community.Tasks should be downloaded and installed.

    3. This script works only for Visual Studio 2008. How ca I create similar script for Visual Studio 2005?

  10. Nicolay says:

    One more comment, the name of the article is not correct, should be: How to publish a web application with MSBuild, because this approuch works only for web applications, not sites.

  11. Nicolay says:

    I changed the Build.xml to publish web services using MSBuild 2005.

    Output

    C:\Inetpub\wwwroot\WebService1Test
    WebService1Test

    C:\Inetpub\wwwroot\WebService1Live
    WebService1Live

  12. Nicolay says:

    I cannot upload an XML file, so I can send it via email.

  13. Ben says:

    If you’re having problems with referenced dll’s not being included, you might need to add the property BuildingProject=true to the MsBuild command.

    This is a known bug, see http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=383199

  14. Luke Correa says:

    I get the following error…

    C:\Program Files\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets(45,3): error MSB3191: Unable to create directory “\\TestShare\Web\DEV\bin”. Could not find file ‘\\TestShare\Web\DEV\bin’.

    Is this a rights issue?

  15. Hi Luke,

    Difficult to say, but most likely it is security and permissions related. Make sure that your account has write access to that share.

    Ross

  16. Ajeeth says:

    I have used the script for my application and I could see that the referenced dlls are getting duplicated into D:\ drive which is annoying.

    Can you please let me know where I am making the mistake

    Please find the xml file

    Output

  17. Ajeeth says:

    I am not able to post the XML script. I can mail you if needed.

  18. Ajeeth says:

    Output

  19. Dyadenka says:

    Hi, nice article, however you may want to update it with Ben’s comment about bug in _CopyWebApplication target. I was fighting that for a few hours until I looked under the article and stumbled upon Ben’s comment.

    If you reference 3-rd party assembly (e.g. NHibernate.dll) which in turn depends on some other assembly (e.g. Castle.Core.dll, Iesi.Collections.dll) those third level dependency assemblies are not copied without workaround suggested in that Connect issue that Ben is referring to.

  20. hi Dyadenka, yes you’re right the article is a bit out of date. One of these days I will get time to rewrite it using the latest tools, and all the useful comments people have provided over the last two years.

  21. Dyadenka says:

    Sounds quite familiar, usually there is never enough time to catch up with all things :) . Just a small correction – seems like it’s not _CopyWebApplication target that is buggy but rather ResolveReferences one.

  22. The Earl says:

    You can emulate the publish web project menu command by calling the built in ‘PipelinePreDeployCopyAllFilesToOneFolder’ target.

    MSBuild /T:Clean;Build;PipelinePreDeployCopyAllFilesToOneFolder
    /p:_PackageTempDir=;Configuration=Release

    I lost days of my life looking for that one :(

  23. @The Earl: I’ve just tried that command and I don’t know if it was because I didn’t set “_PackageTempDir” to something but after a while the build started deleting files all over the place. So be very careful using that command!!!

  24. Peter de Witte says:

    Another approach can be: Ad a Web Deployment Project to your solution. Building it will do a pretty good job publishing your webapp.

  25. I was struggling with the same error (MSB4057: The target “_CopyWebApplication” does not exist in the project.

    Yes. I was using a Web Application Project (not a web site).

    I was baffled because I had one project that worked, and one that did not. So I sat down with ExamDiffPro and went to work comparing the project files. What I found was the section of build target includes at the bottom of the project files were different.

    One project (that was working) was created using a newer version of Visual Studio. The other one (that was NOT working) was created years ago and has been upgraded over the years, to the current version I am working with. The upgraded project, evidently didn’t get upgraded with the new build targets as newer versions of Visual Studio became available.

    At the bottom of the project file which was working, I found the following:

    The second import line was NOT in the project which was causing problems.

    I simply copied the import from the working file, and pasted it in the relative location in the non-working file, and viola!

    While this is not a direct solution to the initial problem defined in this thread, I hope this helps some of the other people that stumbled across this thread, looking for similar issues, only with actual Web Application Projects.

  26. [...] How to publish a web site with MSBuild – Coding Cockerel In part two of my MSBuild tutorial I needed to find a way to call Visual Studio’s Publish Web Site feature from MSBuild. Much trawling of the interweb failed to find anything of use, so in the end I had to produce my own target which copied the relevant files into the output folder. The problem with this approach is that it only works for files of type dll, aspx or config. [...]

  27. Soumya Roy says:

    Thanks a ton Coding Cockerel… I was banging my head on wall for last 4 days to do publish using MSBuild… Finally I succeeded in doing it with ur help… Need one more favor from you… Can you please give me FTP upload code where I can upload the whole folder except the web.config file???

  28. […] This article seems to be relevant to ASP.NET Web Sites, but I find that I’m getting an error when trying to build using those suggestions: […]

Leave a Reply