52

I'm trying to replicate the Visual Studio 2010 "Publish..." command (applicable to Web Application projects) where I would in the UI choose Publish Method: "File System".

My attempt at this is...

%msbuild% /t:MsDeployPublish /property:MsDeployServiceUrl="file:///d:\MyDeploymentFolder";MsDeployPublishMethod="File System" "d:\MySourceFolder\Project.csproj"

... and having tried a method of "FileSystem", "File System", "Local", and a few others.

The error I get implies that MsDeploy is still trying to push to an IIS server:

"D:\MySourceFolder\Project.csproj" (MsDeployPub
lish target) (1) ->
(MSDeployPublish target) ->
  C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web
.Publishing.targets(3847,5): error : Web deployment task failed.(The metabase k
ey '/lm/w3svc' could not be found.) [D:\MySourceFolder\Project.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.P
ublishing.targets(3847,5): error : \r [D:\MySourceFolder\Project.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.P
ublishing.targets(3847,5): error : The metabase key '/lm/w3svc' could not be fo
und.\r [D:\MySourceFolder\Project.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.P
ublishing.targets(3847,5): error : Unable to access the IIS configuration syste
m. Please make sure you have IIS 7 (or later) installed.\r [D:\MySourceFolder\Project.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.P
ublishing.targets(3847,5): error : Retrieving the COM class factory for compone
nt with CLSID {2B72133B-3F5B-4602-8952-803546CE3344} failed due to the followin
g error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REG
DB_E_CLASSNOTREG)). [D:\MySourceFolder\Project.csproj]

How can I target the file system for deployment, as Visual Studio normally lets me in the GUI?

DuckMaestro
  • 15,232
  • 11
  • 67
  • 85

3 Answers3

183

As per my answer from Using MSBuild, how do I build an MVC4 solution from the command line (applying Web.config transformations in the process) and output to a folder?

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

You can also target the project via the solution using the /t:SolutionFolder/Project:Target syntax:

msbuild Solution.sln /t:SolutionFolder/ProjectFile:WebPublish ^
                     /p:Configuration=Release ^ 
                     /p:WebPublishMethod=FileSystem ^
                     /p:DeleteExistingFiles=True ^
                     /p:publishUrl=c:\output
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • 18
    This is the holy grail of building web csproj files. – Matthew Feb 04 '15 at 21:49
  • 1
    If you're building with Visual Studio 2013, try adding `/p:VisualStudioVersion=12.0` if you get an error saying the `WebPublish` target does not exist. – Matthew Aug 20 '15 at 03:17
  • In case you are searching this for a build server (Teamcity), specify in the `Targets:` field: Rebuild WebPublish, and check out this answer to have WebPublishing working on a agent server without installing visual studio: http://stackoverflow.com/a/24245360/2901207 – CularBytes Mar 05 '17 at 14:58
  • 2
    It took so much searching and trying different combinations on msbuild to finally reach this answer. – Jason Cheng Feb 28 '19 at 20:17
  • I ran into an issue while publishing an WebApi using the first example, with `.csproj` file. My web api referenced many class libraries within my solution. Those class libraries have NuGet packages on their own. After publishing and deploying to AppService I got many "could not load file or assembly". Then I looked at the publish folder and realized none of the NuGet packages from the libs were there. Is as if none of the projects got built. I fall back using sln and a publish profile instead. – jpgrassi Sep 18 '19 at 10:19
  • 5
    Using this my project compiles, however there is no output generated. My build server does not generate anything on the publishUrl directory – luisgepeto Jan 17 '20 at 22:59
3

I gave up trying to get MSBuild to copy deployable web files (and not do anything else but that), so I scripted it in PowerShell and am really happy with the result. Much faster than anything I tried through MSBuild. Here's the gist (literally):

function copy-deployable-web-files($proj_path, $deploy_dir) {
  # copy files where Build Action = "Content" 
  $proj_dir = split-path -parent $proj_path
  [xml]$xml = get-content $proj_path
  $xml.Project.ItemGroup | % { $_.Content } | % { $_.Include } | ? { $_ } | % {
    $from = "$proj_dir\$_"
    $to = split-path -parent "$deploy_dir\$_"
    if (!(test-path $to)) { md $to }
    cp $from $to
  }

  # copy everything in bin
  cp "$proj_dir\bin" $deploy_dir -recurse
}
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
0

I don't think you are being specific enough with what you are telling msbuild.

Pulled this out of one of my bookmarks on the subject, hopefully it will help: http://www.digitallycreated.net/Blog/59/locally-publishing-a-vs2010-asp.net-web-application-using-msbuild

Jesse
  • 8,223
  • 6
  • 49
  • 81