Visual Studio 2010: How to publish an ASP.NET web app to a target folder with MSBUILD

msbuildpublishvisual studio 2010

In Visual Studio 2010, you know how you can change your configuration (debug, release, etc), right-click a project in the solution explorer, click publish, and have all the important web app project files for the selected configuration copied to a target folder along with an xdt-transformed web.config? Well, I am looking for the MSBUILD equivalent of exactly that.

My challenge to you: Provide the ONE LINE that I need to execute at my command prompt in order to accomplish this. No third party programs. No tutorial videos. Just a single, straight-up command that I can literally copy from your response, paste into a command window, modify as necessary to support my directory structure, and then hit enter.

If not, then perhaps someone could provide a link to a complete MSBUILD reference showing every command, switch, and value I can use at the command line.

Best Answer

Put the below to ProjectPublish.MSBuild.xml file (change PropertyGroup as needed):

<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Publish"> 
    <PropertyGroup>
        <ProjectFile>Path\To\Web.csproj</ProjectFile>
        <PublishDir>Path\For\Publish\Output</PublishDir>
        <TempDir>Path\To\Temp\Folder</TempDir>
        <BuildConfig>Release|Debug</BuildConfig>
    </PropertyGroup>

    <Target Name="Publish">
        <MSBuild Projects="$(ProjectFile)"
                 Properties="Configuration=$(BuildConfig);WebProjectOutputDir=$(PublishDir);OutDir=$(TempDir)\;BuildingProject=true"
                 Targets="ResolveReferences;_CopyWebApplication" />
    </Target>
</Project>

Calling this from command line (or .bat file) should do the trick:

%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe ProjectPublish.MSBuild.xml