如何使用MsBuild MsDeployPublish来定位本地文件系统?

53
我正在尝试复制Visual Studio 2010中的“发布...”命令(适用于Web应用程序项目),在用户界面中选择“发布方法:文件系统”。我的尝试是...
``` %msbuild% /t:MsDeployPublish /property:MsDeployServiceUrl="file:///d:\MyDeploymentFolder";MsDeployPublishMethod="File System" "d:\MySourceFolder\Project.csproj" ```
我已经尝试了“FileSystem”、“File System”、“Local”等多种方法,但收到的错误信息表明MsDeploy仍然试图将内容推送到IIS服务器上。
"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]

如何定位文件系统进行部署,就像Visual Studio通常在GUI中让我做的那样?


3个回答

185

根据我在如何使用MSBuild从命令行构建MVC4解决方案(应用Web.config变换并输出到文件夹)的答案中所述:

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

如果您正在构建解决方案文件:

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

您还可以使用/t:SolutionFolder/Project:Target语法通过解决方案来定位项目:

点击此处了解更多信息。

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

18
这是构建网页csproj文件的至高无上之物。 - Matthew
1
如果您正在使用Visual Studio 2013进行构建,请尝试添加/p:VisualStudioVersion=12.0,如果出现“WebPublish”目标不存在的错误。 - Matthew
如果您正在为构建服务器(Teamcity)搜索此内容,请在“Targets:”字段中指定Rebuild WebPublish,并查看此答案,以使WebPublishing在代理服务器上工作而无需安装Visual Studio:https://dev59.com/questions/_mIk5IYBdhLWcg3wadgD#24245360 - CularBytes
2
我花了很多时间搜索和尝试不同的msbuild组合,最终才得出这个答案。 - Jason Cheng
我在使用.csproj文件发布WebApi的第一个示例时遇到了问题。我的WebApi引用了解决方案中的许多类库。这些类库本身有NuGet包。在发布和部署到AppService后,我得到了许多“无法加载文件或程序集”的错误。然后我查看了发布文件夹,并意识到没有任何来自库的NuGet包。就好像没有构建任何项目一样。我退回使用sln和发布配置文件。 - jpgrassi
5
使用这个方法编译我的项目后,没有输出结果。我的构建服务器在发布目录(publishUrl directory)中没有生成任何东西。 - luisgepeto

3

我放弃了尝试让MSBuild复制可部署的Web文件(而不做其他任何事情),所以我在PowerShell中编写了脚本,并对结果感到非常满意。比我通过MSBuild尝试过的任何东西都要快得多。以下是要点(确切地说):

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
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接