84

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.

Community
  • 1
  • 1
Erik5388
  • 2,011
  • 2
  • 19
  • 28

5 Answers5

155

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.

Sayed Ibrahim Hashimi
  • 42,483
  • 14
  • 139
  • 172
  • 3
    I think the OP simply wants to "deploy" the web app to an arbitrary folder on his local machine. – Richard Szalay Dec 19 '12 at 11:18
  • This is perfect, do you know whether this update comes with the standalone msbuild.exe? – Erik5388 Dec 19 '12 at 18:29
  • Yeah but you need to install VS too. VS is the one who ships the .targets which are consumed by msbuild.exe. – Sayed Ibrahim Hashimi Dec 19 '12 at 19:48
  • 3
    so, on a deployment server a person would need to install the visual studios IDE to get a command like that to work? – Erik5388 Dec 20 '12 at 15:25
  • 2
    or could i just do this: http://www.microsoft.com/en-us/download/details.aspx?id=30670 – Erik5388 Dec 20 '12 at 15:40
  • For VS 2010, the publish profiles seem to be written into the project root directory (wherever your X.csproj file is found). Additionally, filenames appear to be [Name].Publish.xml, not [Name].pubxml – Nate Kennedy Jun 09 '14 at 14:11
  • 10
    Does this work in VS 2013? Running the same command line, produces no publish. No errors either. Testing with a simple file system deployment in a publishing profile. build works, jut no publishing result. Destination is empty. – Rex Whitten Nov 24 '14 at 17:13
  • 1
    I have exactly the same problem @starfighterxyz. Have you found a solution for this? – Tamas Ionut Jan 12 '15 at 15:08
  • 1
    You will have to add the version parameter for Visual Studio 2013 as well: .. /p:VisualStudioVersion=12.0 ... – Daniel Lemke Mar 27 '15 at 11:02
  • 1
    I'm trying to get this to work in Visual Studio 2013 with MSBuild with cruise control. I'd like to be able to configure this in Cruise Control and not the project. Is there a way to set the output directory different than the publish profile? The only way I can get the Web.config transform to work is to deploy. We were just using MSBuild with no special options, but we'd like the publish transformations to apply. – Julia McGuigan May 05 '15 at 17:40
  • 2
    When I use this it publishes my Debug build even though the .pubxml file has Release – reggaeguitar Jul 01 '15 at 16:09
  • 2
    @reggaeguitar Configuration must be set at the beginning of the build so it cannot be set in the .pubxml. You should pass it as a cmd line param. More details at http://sedodream.com/2012/10/27/msbuildhowtosettheconfigurationproperty.aspx. The value in the .pubxml is just for the VS UI. – Sayed Ibrahim Hashimi Jul 01 '15 at 19:57
  • @SayedIbrahimHashimi Thanks for this - I just ended up on this page after being burned by this exact issue. Was assuming the configuration was coming from the pubxml – Steve Vermeulen Jul 02 '15 at 21:46
  • 1
    VS2015, BuildTools, no IDE, file system profile: "C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe" MyProject.csproj /p:DeployOnBuild=true /p:PublishProfile=MyProfile /p:VisualStudioVersion=14.0 /p:Configuration=Release – Jesper Mygind May 25 '16 at 07:37
  • I have the same issue, build is running but no files in publish folder, did any one found a solution ? "C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe" TestApp.csproj /p:DeployOnBuild=true /p:PublishProfile=Properties\PublishProfiles\test1.pubxml /p:VisualStudioVersion=14.0 /p:Configuration=Release – SirMoreno Sep 22 '16 at 19:22
11

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>
nils
  • 448
  • 4
  • 12
  • 7
    This answer was very helpful to me, but there's one thing I would add to improve it. If you execute it as written, robocopy will return exit code 1 to indicate a successful copy...causing msbuild to think the build has failed. To workaround this, simply append "if %errorlevel% leq 1 exit 0 else exit %errorlevel%" after the /e in the robocopy command. – Alex Jun 14 '13 at 19:55
  • Both the above answer and Alex's comments were helpful to me. I would also add before to keep the project folder clean. – JackArbiter Dec 18 '13 at 20:14
8

The command below works perfect:

msbuild Myproject.sln  /t:Rebuild /p:outdir="c:\outproject\\" /p:Configuration=Release /p:Platform="Any CPU"
Roman Marusyk
  • 19,402
  • 24
  • 55
  • 90
jamilir
  • 113
  • 1
  • 1
  • 3
    when you just want to publish the content files, this works. But web.config transformations are not executed – andreas Jun 03 '15 at 09:56
  • 1
    This method doesn't work for publishing an MVC website (views are not copied) – eka808 Jul 02 '16 at 20:32
  • For a web application such as Web API two critical files are not produced in the bin folder: App_global.asax.dll and App_global.asax.compiled. I recommend using the example of Sayed above. – Fred Peters Mar 31 '17 at 02:04
2

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

Mick
  • 5,754
  • 4
  • 39
  • 60
0

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!
James Joyce
  • 1,414
  • 16
  • 16