Take the 2-minute tour ×

Visual Studio 2010 has a Publish command that allows you to publish your Web Application Project to a file system location. I'd like to do this on my TeamCity build server, so I need to do it with the solution runner or msbuild. I tried using the Publish target, but I think that might be for ClickOnce:

msbuild Project.csproj /t:Publish /p:Configuration=Deploy

I basically want to do exactly what a web deployment project does, but without the add-in. I need it to compile the WAP, remove any files unnecessary for execution, perform any web.config transformations, and copy the output to a specified location.

My Solution, based on Jeff Siver's answer

<Target Name="Deploy">
    <MSBuild Projects="$(SolutionFile)" 
             ContinueOnError="false" />
    <Exec Command="&quot;$(ProjectPath)\obj\$(Configuration)\Package\$(ProjectName).deploy.cmd&quot; /y /m:$(DeployServer) -enableRule:DoNotDeleteRule" 
          ContinueOnError="false" />
share|improve this question
possible duplicate: stackoverflow.com/questions/1162253/… –  SnOrfus Jun 22 '10 at 22:06
@SnOrfus I'm currently using Web Deployment Projects in VS 2008 (as I mentioned in my answer to that question) but I'd like to try automating the Publish feature of VS 2010 instead. –  jrummell Jun 22 '10 at 23:05
This question looks helpful stackoverflow.com/questions/1983575/… –  jrummell Jun 23 '10 at 18:30
Just one little amendment to your script: you're using $(ProjectPath) for the deploy script but you really want is $(ProjectDir) otherwise you end up with .csproj\obj –  Troy Hunt Oct 28 '10 at 2:55
Starting with VS2012, this is much easier: stackoverflow.com/a/13947667/270348 –  RobSiklos Aug 12 at 15:04

6 Answers 6

up vote 79 down vote accepted

I got it mostly working without a custom msbuild script. Here are the relevant TeamCity build configuration settings:

Artifact paths: %system.teamcity.build.workingDir%\MyProject\obj\Debug\Package\PackageTmp 
Type of runner: MSBuild (Runner for MSBuild files) 
Build file path: MyProject\MyProject.csproj 
Working directory: same as checkout directory 
MSBuild version: Microsoft .NET Framework 4.0 
MSBuild ToolsVersion: 4.0 
Run platform: x86 
Targets: Package 
Command line parameters to MSBuild.exe: /p:Configuration=Debug

This will compile, package (with web.config transformation), and save the output as artifacts. The only thing missing is copying the output to a specified location, but that could be done either in another TeamCity build configuration with an artifact dependency or with an msbuild script.


Here is an msbuild script that will compile, package (with web.config transformation), and copy the output to my staging server


    <Target Name="Build" DependsOnTargets="BuildPackage;CopyOutput" />

    <Target Name="BuildPackage">
        <MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)" />

    <Target Name="CopyOutput">
            <PackagedFiles Include="$(ProjectName)\obj\$(Configuration)\Package\PackageTmp\**\*.*"/>
        <Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\\build02\wwwroot\$(ProjectName)\$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/>

You can also remove the SolutionName and ProjectName properties from the PropertyGroup tag and pass them to msbuild.

msbuild build.xml /p:Configuration=Deploy;SolutionName=MySolution;ProjectName=MyProject

Update 2

Since this question still gets a good deal of traffic, I thought it was worth updating my answer with my current script that uses Web Deploy (also known as MSDeploy).

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
    <ProjectFile Condition=" '$(ProjectFile)' == '' ">$(ProjectName)\$(ProjectName).csproj</ProjectFile>
    <DeployServiceUrl Condition=" '$(DeployServiceUrl)' == '' ">http://staging-server/MSDeployAgentService</DeployServiceUrl>

  <Target Name="VerifyProperties">
    <!-- Verify that we have values for all required properties -->
    <Error Condition=" '$(ProjectName)' == '' " Text="ProjectName is required." />

  <Target Name="Build" DependsOnTargets="VerifyProperties">
    <!-- Deploy using windows authentication -->
    <MSBuild Projects="$(ProjectFile)"
             ContinueOnError="false" />

In TeamCity, I have parameters named env.Configuration, env.ProjectName and env.DeployServiceUrl. The MSBuild runner has the build file path and the parameters are passed automagically (you don't have to specify them in Command line parameters).

You can also run it from the command line:

msbuild build.xml /p:Configuration=Staging;ProjectName=MyProject;DeployServiceUrl=http://staging-server/MSDeployAgentService
share|improve this answer
This is a great solution, I've just used this with an MSDeploy command as an exec task in the CopyOutput target. Thanks! –  Troy Hunt Oct 29 '10 at 3:11
thanks - this also works well directly from powershell (apologies for the formatting - no carriage returns in comments): &msbuild "$solution" /p:"Configuration=$configuration" ; &msbuild "$project" /t:Package /p:"Configuration=$configuration;_PackageTempDir=$outputfolder" –  zcrar70 Feb 2 '11 at 12:25
Fantastic! Thank you very much for posting. –  Jonathan Jul 11 '13 at 11:47
I tried the example from your first Update and it seems that the Package target depends on WebDeploy, too: error : Package/Publish task Microsoft.Web.Publishing.Tasks.IsCleanMSDeployPackageNeeded failed to load Web Deploy assemblies. Microsoft Web Deploy is not correctly installed on this machine. (Mentioning it since you write that your second update uses WebDeploy, which might imply the first would not use WebDeploy yet.) –  chiccodoro May 8 at 14:35

I came up with such solution, works great for me:

msbuild /t:ResolveReferences;_WPPCopyWebApplication /p:BuildingProject=true;OutDir=C:\Temp\build\ Test.csproj

The secret sauce is _WPPCopyWebApplication target.

share|improve this answer
Thanks - this is what I was looking for –  Mark Nov 13 '11 at 22:12
What is _WPPCopyWebApplication and how can I use it MSBbuild xml config file/ –  Johnny_D Sep 11 '13 at 12:20
OutDir was what worked for me –  Haroon Dec 5 '13 at 11:23
Using VS2012 .NET 3.5, I got the error error MSB4057: The target "_WPPCopyWebApplication" does not exist in the project. Taking out that part led to a deployment without deploying any views –  notfed Feb 11 at 21:19
You might need to call it with a different /p:VisualStudioVersion=12.0 because build uses the targets from c:\program files (x86)\msbuild\microsoft\visualstudio\VERSION\Webapplication\Microsoft.WebApplica‌​tion.targets so maybe its using an older version that doesnt have the correct target. –  FRoZeN Oct 9 at 7:00

Using the deployment profiles introduced in VS 2012, you can publish with the following command line:

msbuild MyProject.csproj /p:DeployOnBuild=true /p:PublishProfile=<profile-name> /p:Password=<insert-password> /p:VisualStudioVersion=11.0

For more information on the parameters see this.

share|improve this answer
Using VS2012 .NET 3.5, this did not work for deploying to the file system. It simply builds and does no deployment. –  notfed Feb 11 at 21:20
your /p:VisualStudioVersion=11.0 saved my life. I use /p:VisualStudioVersion=12.0 for vs2013 and its works fine. –  Seyed Morteza Mousavi Aug 11 at 19:22

I don't know TeamCity so I hope this can work for you.

The best way I've found to do this is with MSDeploy.exe. This is part of the WebDeploy project run by Microsoft. You can download the bits here.

With WebDeploy, you run the command line

msdeploy.exe -verb:sync -source:contentPath=c:\webApp -dest:contentPath=c:\DeployedWebApp

This does the same thing as the VS Publish command, copying only the necessary bits to the deployment folder.

share|improve this answer
That looks promising. However, it looks like the Management Service is only available on Server 2008. My staging server (where I want to automate deployment) is running Windows 7 Pro. –  jrummell Jun 23 '10 at 14:00
There are two pieces to the product. The pieces that integrate right into IIS require Server 2008. The command line component does not have that requirement; I have it running on a Server 2003 box I use for deployments. –  Jeff Siver Jun 23 '10 at 20:34
I've done some reading on MSDeploy. I got it installed and working on my staging server, thanks! Can I run MSDeploy from an MSBuild script? –  jrummell Jun 24 '10 at 19:15
It should run fine as an EXEC task in your build script. –  Jeff Siver Jun 25 '10 at 6:38
does the same thing as which configuration of the VS Publish command? Which publish method - file system or other? Does it use the MyProject.Publish.xml file to determine which files to copy? –  Anthony Sep 22 '11 at 10:23

With VisualStudio 2012 there is a way to handle subj without publish profiles. You can pass output folder using parameters. It works both with absolute and relative path in 'publishUrl' parameter. You can use VS100COMNTOOLS, however you need to override VisualStudioVersion to use target 'WebPublish' from %ProgramFiles%\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets. With VisualStudioVersion 10.0 this script will succeed with no outputs :)

Update: I've managed to use this method on a build server with just Windows SDK 7.1 installed (no Visual Studio 2010 and 2012 on a machine). But I had to follow these steps to make it work:

  1. Make Windows SDK 7.1 current on a machine using Simmo answer (http://stackoverflow.com/a/2907056/2164198)
  2. Setting Registry Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7\10.0 to "C:\Program Files\Microsoft Visual Studio 10.0\" (use your path as appropriate)
  3. Copying folder %ProgramFiles%\MSBuild\Microsoft\VisualStudio\v11.0 from my developer machine to build server


set WORK_DIR=%~dp0
pushd %WORK_DIR%
set OUTPUTS=%WORK_DIR%..\Outputs
set CONFIG=%~1
if "%CONFIG%"=="" set CONFIG=Release
if %VSTOOLS%=="" set "PATH=%PATH%;%WINDIR%\Microsoft.NET\Framework\v4.0.30319" && goto skipvsinit
call "%VSTOOLS:~1,-1%vsvars32.bat"
if errorlevel 1 goto end
msbuild.exe Project.csproj /t:WebPublish /p:Configuration=%CONFIG% /p:VisualStudioVersion=11.0 /p:WebPublishMethod=FileSystem /p:publishUrl=%OUTPUTS%\Project
if errorlevel 1 goto end
exit /b %ERRORLEVEL%
share|improve this answer

found two different solutions which worked in slightly different way:

1. This solution is inspired by the answer from alexanderb [link]. Unfortunately it did not work for us - some dll's were not copied to the OutDir. We found out that replacing ResolveReferences with Build target solves the problem - now all necessary files are copied into the OutDir location.

msbuild /target:Build;_WPPCopyWebApplication  /p:Configuration=Release;OutDir=C:\Tmp\myApp\ MyApp.csproj
Disadvantage of this solution was the fact that OutDir contained not only files for publish.

2. The first solution works well but not as we expected. We wanted to have the publish functionality as it is in Visual Studio IDE - i.e. only the files which should be published will be copied into the Output directory. As it has been already mentioned first solution copies much more files into the OutDir - the website for publish is then stored in _PublishedWebsites/{ProjectName} subfolder. The following command solves this - only the files for publish will be copied to desired folder. So now you have directory which can be directly published - in comparison with the first solution you will save some space on hard drive.

msbuild /target:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Release;_PackageTempDir=C:\Tmp\myApp\;AutoParameterizationWebConfigConnectionStrings=false MyApp.csproj
AutoParameterizationWebConfigConnectionStrings=false parameter will guarantee that connection strings will not be handled as special artifacts and will be correctly generated - for more information see link.

share|improve this answer

Your Answer


