How to use MsBuild MsDeployPublish to target local file system?

43,517

Solution 1

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

Solution 2

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
}
Share:
43,517

Related videos on Youtube

Mark Rolich
Author by

Mark Rolich

Self-taught programmer. B.Sc. Mathematics at UCLA (2006) and M.Sc. Computer Science at UCSD (2013). My other interests include economics, philosophy, and linguistics.

Updated on July 09, 2022

Comments

  • Mark Rolich
    Mark Rolich almost 2 years

    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?

  • Matthew
    Matthew over 9 years
    This is the holy grail of building web csproj files.
  • Matthew
    Matthew over 8 years
    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.
  • CularBytes
    CularBytes about 7 years
    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: stackoverflow.com/a/24245360/2901207
  • Jason Cheng
    Jason Cheng about 5 years
    It took so much searching and trying different combinations on msbuild to finally reach this answer.
  • jpgrassi
    jpgrassi over 4 years
    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.
  • luisgepeto
    luisgepeto over 4 years
    Using this my project compiles, however there is no output generated. My build server does not generate anything on the publishUrl directory