使 MSDeploy (Visual Studio) 不删除 App_Data 文件夹而是删除其他所有内容 [英] Make MSDeploy (Visual Studio) not delete App_Data folder but delete everything else

查看:28
本文介绍了使 MSDeploy (Visual Studio) 不删除 App_Data 文件夹而是删除其他所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 的 Publish 按钮来部署我的网站,并希望在服务器上有一个不同的 App_Data 文件夹.有一个在目标上留下额外文件(不要删除)的复选框,它可以防止我的 App_Data 文件夹被删除,但随着网站的变化,它最终会积累大量残留文件.

I'm using Visual Studio's Publish button to deploy my website, and want a different App_Data folder on the server. There's a checkbox for Leave extra files on destination (do not delete) which prevents my App_Data folder from getting deleted, but then it'll eventually accumulate a lot of vestigial files as the website changes.

有什么办法可以让它在删除所有内容时只排除 App_Data?

Is there any way to make it exclude just App_Data when it deletes everything?

推荐答案

手动调用 msdeploy 即可完成 - 只需添加以下参数:

It can be done when you invoke msdeploy manually - just add the following parameter:

-skip:Directory=\App_Data

请参阅Web 部署操作设置.路径是正则表达式,非常灵活.

See Web Deploy Operation Settings. The path is a regular expression, so it is quite flexible.

如果您使用 VS 生成的 ProjectName.deploy.cmd 脚本进行部署,您还可以在 _MsDeployAdditionalFlags 环境变量中传递此参数(运行该脚本时).

If you deploy using the VS-generated ProjectName.deploy.cmd script, you can also pass this parameter in the _MsDeployAdditionalFlags environment variable (when running that script).

这是我为满足我们的需求而提出的最佳方案(我们的情况与您相似).我没有尝试将它与 VS 的发布按钮集成,因为我们是从命令行部署的.

This is the best I've come up with for our needs (we have a similar situation as you). I haven't tried integrating it with VS's Publish button, since we deploy from command line.

自从我发布这个答案以来,我已经了解了一些关于 MSDeploy 的知识,所以我想我现在应该更新它.

I have learned a few things about MSDeploy since I posted this answer, so I thought I'd update it now.

首先,上面的跳过规则会跳过匹配路径(App_Data)上的任何操作.如果需要更精细的控制,则可以使用更详细的语法.例如,仅跳过删除(在目标服务器上保留任何额外文件,但添加任何新文件并更新现有文件):

First of all, the above skip rule skips any operations on the matching path (App_Data). If more granular control is needed, a more verbose syntax is available. For example, to skip only deletes (to keep any extra files on target server, but add any new ones and update existing ones):

-skip:skipaction='Delete',objectname='filePath',absolutepath='\App_Data\.*' -skip:skipaction='Delete',objectname='dirPath',absolutepath='\App_Data\.*'

这会跳过 App_Data 中所有文件和所有子文件夹(及其所有内容)的删除,但不会阻止添加和更新.

This skips deletes of all files and all subfolders (with all their content) in App_Data, but doesn't prevent adds and updates.

另一个有用的是,可以在项目文件 (.csproj) 中定义跳过规则,以便它们自动包含在随同生成的 .deploy.cmd 脚本中与包.这使得没有必要通过 _MsDeployAdditionalFlags 将它们传递给脚本.

Another useful thing is that skip rules can be defined in the project file (.csproj) so that they are automatically included in the .deploy.cmd script generated along with the package. This makes it unnecessary to pass them to the script through _MsDeployAdditionalFlags.

如果csproj文件中包含以下内容,将添加上述跳过规则:

The above skip rule will be added if the following is included in csproj file:

<PropertyGroup>
  <OnBeforePackageUsingManifest>AddCustomSkipRules</OnBeforePackageUsingManifest>
</PropertyGroup>
<Target Name="AddCustomSkipRules">
  <ItemGroup>
    <MsDeploySkipRules Include="SkipDeleteAppData">
      <SkipAction>Delete</SkipAction>
      <ObjectName>filePath</ObjectName>
      <AbsolutePath>$(_Escaped_PackageTempDir)\App_Data\.*</AbsolutePath>
      <XPath>
      </XPath>
    </MsDeploySkipRules>
    <MsDeploySkipRules Include="SkipDeleteAppData">
      <SkipAction>Delete</SkipAction>
      <ObjectName>dirPath</ObjectName>
      <AbsolutePath>$(_Escaped_PackageTempDir)\App_Data\.*</AbsolutePath>
      <XPath>
      </XPath>
    </MsDeploySkipRules>
  </ItemGroup>
</Target>

(名称AddCustomSkipRulesSkipDeleteAppData 是完全任意的;$(_Escaped_PackageTempDir) 应该是可能需要,但在实践中我总是看到它评估为空字符串)

(the names AddCustomSkipRules and SkipDeleteAppData are completely arbitrary; $(_Escaped_PackageTempDir) is supposed to be possibly needed, but in practice I've always seen it evaluate to an empty string)

请参阅Web 部署:自定义部署包如何在 .csproj 文件中设置 MSDeploy 设置了解更多信息.

See Web Deploy: Customizing a deployment package and How to set MSDeploy settings in .csproj file for more info.

一个警告:这只会将这些规则添加到 .deploy.cmd 脚本中,因此如果您想使用图形 IIS 管理器进行包部署,它是没有用的,因为它不使用那个脚本(从 VS 部署可能也是如此,但我还没有检查过).

One caveat: this only adds those rules to the .deploy.cmd script, so it is useless if you want to use the graphical IIS Manager for package deployment, as it doesn't use that script (the same probably goes for deployment from VS, but I haven't checked).

这篇关于使 MSDeploy (Visual Studio) 不删除 App_Data 文件夹而是删除其他所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆