10

I think the question title pretty much said it all, but for clarity, I am trying to:

  1. Build a VS2010 ASP.NET MVC4 solution from the command line (MSBuild), specifying a solution configuration (e.g. Release)
  2. Apply any Web.config transformations for that configuration during the process
  3. Output the results into a folder on the local machine (no IIS, agents, zip files, FTP, packages etc, just a folder containing all the files required to run that web site)

I've been trying to figure this out for almost a week now, through the Microsoft docs (which are spectacularly unhelpful), other answers on SO and general Googling. I don't know if I'm just not getting it (which is entirely possible), or if MSBuild/MSDeploy really is the cryptic mess it currently appears to be.

Is there a reasonably simple way to achieve this? Help, please!

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
  • Microsoft docs are an overkill most of the time. I have been reading for msbuild, msdeploy, iis. My experience so far is like a dog chasing its tail. – OK999 Sep 26 '16 at 21:59

1 Answers1

43

If you are running VS2010: ensure you have the Azure 1.7+ SDK installed (even if you're not publishing to Azure, it adds the VS2012 publishing features to VS2010)

VS2012+ have these featured built in

msbuild ProjectFile.csproj /p:Configuration=Release ^
                           /p:Platform=AnyCPU ^
                           /t:WebPublish ^
                           /p:WebPublishMethod=FileSystem ^
                           /p:DeleteExistingFiles=True ^
                           /p:publishUrl=c:\output

Or if you are building the solution file:

msbuild Solution.sln /p:Configuration=Release ^ 
                     /p:DeployOnBuild=True ^
                     /p:DeployDefaultTarget=WebPublish ^
                     /p:WebPublishMethod=FileSystem ^
                     /p:DeleteExistingFiles=True ^
                     /p:publishUrl=c:\output

Either way, you can also set /p:PrecompileBeforePublish=true and the output will be a precompiled version of the site.

Once you have the Azure SDK installed you would usually create a publish profile and simply use /p:PublishProfile=DeployToFolder but I've given you the manual command line in case you're not interested in going all-in with the new publishing stuff.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • 4
    That is _fantastic_ Richard, thanks so much! I've been struggling with this for far too long now. I've edited your answer slightly to include the Configuration option, which was part of my question but not included in the title due to lack of space; I've tested both methods post-edit and everything works perfectly. – Mark Bell Mar 10 '13 at 07:29
  • 2
    Incidentally, I _did_ have the Azure SDK installed, but in order to correctly integrate it with VS2010, I first had to install the `Windows Azure SDK for .NET (VS2010 SP1) 1.8.1 - February 2013` package using Web Platform Installer. – Mark Bell Mar 10 '13 at 07:35
  • Glad to have helped, and good point about the 2010 version of the SDK – Richard Szalay Mar 10 '13 at 09:09
  • Hi @RichardSzalay, our build server has VS2013 on it, but nothing else. In order for the above MSBuild command to work, do we also need to install something else e.g. "Web Deployment Projects" or "Web Deployment Tool"? Thanks :) – Richard Feb 29 '16 at 09:26
  • You shouldn't have to install anything. But the more important question is: what happened when you tried it? – Richard Szalay Feb 29 '16 at 09:56
  • I was getting an error saying something like "invalid switch: WebPublish" (I included the /t:) so I thought I maybe needed to install some extensions to MSBuild. I got it working (using Bamboo to call MSBuild) by adding a publishing profile to my project in VS2013 and then using these options to my MSBuild task: /p:Configuration=UAT;VisualStudioVersion=12.0;DeployOnBuild=true;PublishProfile=PublishUAT. PublishUAT corresponds to the PublishUAT.pubxml file in my web project. Bit different to yours but it seems to do everything I need including config transforms. Cheers for replying so quickly. – Richard Feb 29 '16 at 12:38