Using MSBuild.exe to "Publish" a ASP.NET MVC 4 project with the cmd line

The command below works perfect:

msbuild Myproject.sln  /t:Rebuild /p:outdir="c:\outproject\\" /p:Configuration=Release /p:Platform="Any CPU"

In VS 2012 (as well as the publish updates available in the Azure SDK for VS 2010) we have simplified command line publishing for web projects. We have done that by using Publish Profiles.

In VS for a web project you can create a publish profile using the publish dialog. When you create that profile it is automatically stored in your project under Properties\PublishProfiles. You can use the created profile to publish from the command line with a command line the following.

msbuild mysln.sln /p:DeployOnBuild=true /p:PublishProfile=<profile-name>

If you want to store the publish profile (.pubxml file) in some other location you can pass in the path to the PublishProfile.

Publish profiles are MSBuild files. If you need to customize the publish process you can do so directly inside of the .pubxml file.

If your end goal is to pass in properties from the command line. I would recommend the following. Create a sample publish profile in VS. Inspect that publish profile to determine what MSBuild properties you need to pass in on the command line. FYI not all publish method support command line publishing (i.e. FTP/FPSE).

FYI if you are building the .csproj/.vbproj instead of the .sln and you are using VS 2012 you should also pass in /p:VisualStudioVersion=11.0. For more details as to why see http://sedodream.com/2012/08/19/VisualStudioProjectCompatabilityAndVisualStudioVersion.aspx.


Create a build.xml file thats look like below

Start Visual Studio command prompt

Run msbuild build.xml

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">

  <PropertyGroup>
    <Build>$(MSBuildProjectDirectory)\Build</Build>
    <ProjectFile>MyProject.csproj</ProjectFile> 
    <ProjectName>MyProjectNameInVisualStudio</ProjectName>
    <CopyTo>$(MSBuildProjectDirectory)\CopyTo</CopyTo>
  </PropertyGroup> 

  <Target Name="Build"> 
    <RemoveDir Directories="$(Build)"/>  
    <MSBuild Projects="$(ProjectFile)" Properties="Configuration=Release;OutputPath=$(Build);OutDir=$(Build)/"></MSBuild>  
    <Exec Command="robocopy.exe  $(Build)\_PublishedWebsites\$(ProjectName) $(CopyTo) /e /is
      if %errorlevel% leq 4 exit 0 else exit %errorlevel%"/>    
  </Target>

</Project>

I found Sayed's answer was deploying the default configuration i.e. Debug. The configuration selected in the Publishing Profile seems to get ignored by MSBuild. Accordingly I changed the command to specify the correct configuration for the deployment...

msbuild mysln.sln /p:Configuration=[config-name] /p:DeployOnBuild=true /p:PublishProfile=[profile-name]

where config-name = Release or some other build configuration you've created