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

Visual StudioMsbuildasp.net Mvc-4Msdeploy

Visual Studio Problem Overview


I'm looking for a command to run against the MSBuild.exe that just takes a MVC 4 project and publishes it to a given directory.

For example,

MSBuild <solution>/<project>.csproj -publish -output=c:/folder

This is obviously incorrect syntax. I'm trying to simplify my question.

This question talks of a build XML, but I'm not trying to do anything with that much detail.

I'm simply trying to do a deploy.

Further down in that question, someone speaks of "MSDeploy". I can look into that, but is it the only option? I do not have the ability to install web deploy on the server. In which case, all I really need to do is "Publish" and send the contents of the published project to a given directory on the server/file-system.

Does anyone have a one liner I can use?

Do I have to use MSDeploy?

Does MSDeploy require web deploy to be installed on the server?

Doesn't setting up web deploy on the server require setting up some ports, permissions, and installing some IIS add-ons?

I'd love to just execute something simple.

Visual Studio Solutions


Solution 1 - Visual Studio

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.

Solution 2 - Visual Studio

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>

Solution 3 - Visual Studio

The command below works perfect:

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

Solution 4 - Visual Studio

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

Solution 5 - Visual Studio

With web projects you need to build, as per above, but then you also need to package/copy. We use a file copy, rather than the "publish"...

Also; we use DEBUG/RELEASE to build the website; but then actual environments, ie "QA" or "PROD" to handle the web.config transforms.

So we build it initially with RELEASE, and then package it with QA - in the example below.

  <PropertyGroup>   
    <SolutionName>XXX.Website</SolutionName>
    <ProjectName>XXX.Website</ProjectName>
    <IisFolderName>XXX</IisFolderName>
   
    <SolutionConfiguration>QA</SolutionConfiguration> <!--Configuration will be set based on user selection-->   

    <SolutionDir>$(MSBuildThisFileDirectory)..</SolutionDir>
    <OutputLocation>$(SolutionDir)\bin\</OutputLocation>
     <WebServer>mywebserver.com</WebServer>
  </PropertyGroup>

  <Target Name="BuildPackage">
    <MSBuild Projects="$(SolutionDir)\$(SolutionName).sln" ContinueOnError="false" Targets="Clean;Rebuild" Properties="Configuration=Release" />
    <MSBuild Projects="$(SolutionDir)\$(ProjectName)\$(ProjectName).csproj" ContinueOnError="false" Targets="Package" Properties="Configuration=$(SolutionConfiguration);AutoParameterizationWebConfigConnectionStrings=False" />
  </Target>

  <Target Name="CopyOutput">
    <ItemGroup>
      <PackagedFiles Include="$(SolutionDir)\$(ProjectName)\obj\$(SolutionConfiguration)\Package\PackageTmp\**\*.*"/>
    </ItemGroup>
    <Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\\$(WebServer)\$(IisFolderName)\$(SolutionConfiguration)\%(RecursiveDir)%(Filename)%(Extension)')"/>
  </Target>

So;

  1. Setup your properties
  2. Call the BuildPackage target
  3. Call the CopyOutput target And voila!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErik5388View Question on Stackoverflow
Solution 1 - Visual StudioSayed Ibrahim HashimiView Answer on Stackoverflow
Solution 2 - Visual StudionilsView Answer on Stackoverflow
Solution 3 - Visual StudiojamilirView Answer on Stackoverflow
Solution 4 - Visual StudioMickView Answer on Stackoverflow
Solution 5 - Visual StudioJames JoyceView Answer on Stackoverflow