在Visual Studio 2012中使用MSBuild PublishProfile时,MSDeploy跳过规则

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Visual Studio 2012中使用MSBuild PublishProfile时,MSDeploy跳过规则相关的知识,希望对你有一定的参考价值。

我正在尝试使用WebDeploy使用自定义MSDeploy跳过规则和Visual Studio 2012中保存的发布配置文件来发布网站。

我从命令行使用发布配置文件,但跳过删除文件夹的跳过规则不起作用。

我在我的网络应用程序中有一个ErrorLog子文件夹,里面有一个web.config文件来设置正确的文件夹权限。没有任何跳过规则,ErrorLog文件夹和web.config文件将正常发布,但服务器上文件夹中的所有现有错误日志文件将在发布时删除。


<SkipAction>Delete</SkipAction>出错

当我向wpp.targets文件添加自定义跳过规则时,跳过规则不再接受<SkipAction>元素的值。如果我设置<SkipAction>Delete</SkipAction>,我会收到以下错误:

C:Program Files (x86)MSBuildMicrosoftVisualStudiov11.0WebMicrosoft.Web.Publishing.targets(4377,5): error : Web deployment task failed. (Unrecognized skip directive 'skipaction'. Must be one of the following: "objectName," "keyAttribute," "absolutePath," "xPath," "attributes.<name>.") [C:inetpubwwwrootMy.WebsiteMy.WebsiteMy.Website.csproj]

如果我只是省略<SkipAction>元素,则ErrorLog文件夹在正常发布时会被删除。

如果我再次设置<SkipAction></SkipAction>,则会在发布时删除ErrorLog文件夹。

如果我设置<KeyAttribute>Delete</KeyAttribute>,那么ErrorLogweb.config文件正常发布。

我的理解是,为了使用自定义跳过规则,您需要从命令行调用MSBuild而不是从VS 2012中发布。但是,我仍然想使用我保存的发布配置文件,我知道现在可以使用VS 2012。


我的MSBuild命令行:

C:WindowsMicrosoft.NETFrameworkv4.0.30319MSBuild.exe My.Website.sln /p:Configuration=Release;DeployOnBuild=true;PublishProfile="Test Server - Web Deploy"

My.Website.wpp.targets:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AfterAddIisSettingAndFileContentsToSourceManifest>AddCustomSkipRules</AfterAddIisSettingAndFileContentsToSourceManifest>
  </PropertyGroup>

  <Target Name="AddCustomSkipRules">
    <Message Text="Adding Custom Skip Rules" />
    <ItemGroup>
      <MsDeploySkipRules Include="SkipErrorLogFolder1">
        <SkipAction></SkipAction>
        <KeyAttribute>Delete</KeyAttribute>
        <ObjectName>dirPath</ObjectName>
        <AbsolutePath>$(_Escaped_WPPAllFilesInSingleFolder)\ErrorLog$</AbsolutePath>
        <XPath></XPath>
      </MsDeploySkipRules>
    </ItemGroup>
  </Target>
</Project>

我的MSBuild输出显示自定义跳过规则,但仍然删除文件:

GenerateMsdeployManifestFiles:
  Generate source manifest file for Web Deploy package/publish ...
AddCustomSkipRules:
  Adding Custom Skip Rules
MSDeployPublish:
  Start Web Deploy Publish the Application/package to http://testserver.domain.com/MSDEPLOYAGENTSERVICE ...
  Starting Web deployment task from source: manifest(C:inetpubwwwrootMy.WebsiteMy.WebsiteobjReleasePackageMy.Website.SourceManifest.xml) to Destination: auto().
  Deleting filePath (MyWeb/ErrorLog	est.txt).
  Updating setAcl (MyWeb/).
  Updating setAcl (MyWeb/).
  Updating filePath (MyWeb/ErrorLogWeb.config).
  Updating filePath (MyWeb/Web.config).
  Updating setAcl (MyWeb/).
  Updating setAcl (MyWeb/).
  Successfully executed Web deployment task.
  Publish is successfully deployed.
答案

编辑:事实证明你是对的:从Visual Studio执行时会忽略skip指令。

幸运的是,有一个解决方法。

你想要的是这个:

<!-- Skip the deletion of any file within the ErrorLog directory -->
<MsDeploySkipRules Include="SkipErrorLogFolder1">
  <SkipAction>Delete</SkipAction>
  <ObjectName>filePath</ObjectName>
  <AbsolutePath>ErrorLog</AbsolutePath>
</MsDeploySkipRules>

此外,您需要阻止VS使用UI任务(它似乎包含有关跳过规则的错误)。您可以通过在wpp.targets或pubxml中声明以下内容来执行此操作:

<PropertyGroup>
  <UseMsDeployExe>true</UseMsDeployExe>
</PropertyGroup>

我在本地测试了这个,我可以确认它是否按预期工作:附加文件已更新,但目录中没有文件被删除。

另一答案

作为参考,这是我的完整.wpp.targets文件,带有工作跳过规则,跳过删除ErrorLog文件夹和自定义ACL,使ErrorLog文件夹在服务器上可写。

从VS 2012 Update 3开始,这仅适用于从命令行使用MSBuild发布并且传递给MSBuild的DeployOnBuild=true;PublishProfile="Test Server - Web Deploy"选项。从VS内部发布时,这将不起作用。

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <UseMsdeployExe>true</UseMsdeployExe> <!-- Required for the MSDeploySkipRules to work -->
    <DeployManagedPipelineMode>Integrated</DeployManagedPipelineMode>
  </PropertyGroup>

  <PropertyGroup>
    <AfterAddIisSettingAndFileContentsToSourceManifest>
      $(AfterAddIisSettingAndFileContentsToSourceManifest);
      AddCustomSkipRules;
    </AfterAddIisSettingAndFileContentsToSourceManifest>
  </PropertyGroup>

  <Target Name="AddCustomSkipRules">
    <Message Text="Adding Custom Skip Rules" />
    <ItemGroup>
      <MsDeploySkipRules Include="SkipErrorLogFolder">
        <SkipAction>Delete</SkipAction>
        <ObjectName>filePath</ObjectName>
        <AbsolutePath>ErrorLog</AbsolutePath>
        <XPath></XPath>
      </MsDeploySkipRules>
    </ItemGroup>
  </Target>

  <PropertyGroup>
    <AfterAddIisSettingAndFileContentsToSourceManifest>
      $(AfterAddIisSettingAndFileContentsToSourceManifest);
      SetCustomACLs;
    </AfterAddIisSettingAndFileContentsToSourceManifest>
    <AfterAddDeclareParametersItemsForContentPath>
      $(AfterAddDeclareParametersItemsForContentPath);
      SetCustomAclParameters;
    </AfterAddDeclareParametersItemsForContentPath>
  </PropertyGroup>

  <Target Name="SetCustomACLs">
    <Message Text="Setting Custom ACLs" />
    <ItemGroup>
      <!--Make sure the application pool identity has write permission to the download folder--> 
      <MsDeploySourceManifest Include="setAcl"
        Condition="$(IncludeSetAclProviderOnDestination) And Exists('$(_MSDeployDirPath_FullPath)ErrorLog')">
        <Path>$(_MSDeployDirPath_FullPath)ErrorLog</Path>
        <setAclAccess>Write</setAclAccess>
        <setAclResourceType>Directory</setAclResourceType>
        <AdditionalProviderSettings>setAclResourceType;setAclAccess</AdditionalProviderSettings>
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>

  <Target Name="SetCustomAclParameters">
    <Message Text="Setting Custom ACL Parameters" />
    <EscapeTextForRegularExpressions Text="$(_MSDeployDirPath_FullPath)">
      <Output TaskParameter="Result" PropertyName="_EscapeRegEx_MSDeployDirPath" />
    </EscapeTextForRegularExpressions>
    <ItemGroup>
      <MsDeployDeclareParameters Include="Add write permission to ErrorLog folder"
        Condition="$(IncludeSetAclProviderOnDestination) and Exists('$(_MSDeployDirPath_FullPath)ErrorLog')">
        <Kind>ProviderPath</Kind>
        <Scope>setAcl</Scope>
        <Match>^$(_EscapeRegEx_MSDeployDirPath)\ErrorLog$</Match>
        <Description>Add write permission to ErrorLog folder</Description>
        <DefaultValue>Default Web Site/ErrorLog</DefaultValue>
        <Value>$(DeployIisAppPath)/ErrorLog</Value>
        <Tags>Hidden</Tags>
        <Priority>$(VsSetAclPriority)</Priority>
        <ExcludeFromSetParameter>True</ExcludeFromSetParameter>
      </MsDeployDeclareParameters>
    </ItemGroup>
  </Target>
</Project>
另一答案

另一种方法是避免使用SkipAction标签,我已成功直接从VS 2013使用此设置:

<Target Name="AddCustomSkipRules"
        AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
    <Message Text="Adding Custom Skip Rules" />
    <ItemGroup>
        <MsDeploySkipRules Include="SkipMedia">
            <objectName>dirPath</objectName>
            <absolutePath>media</absolutePath>
        </MsDeploySkipRules>
        <MsDeploySkipRules Include="SkipUpload">
            <objectName>dirPath</objectName>
            <absolutePath>upload</absolutePath>
        </MsDeploySkipRules>
    </ItemGroup>
</Target>

据我所知,唯一需要注意的是,它会忽略更新,删除和添加操作。

另一答案

经过几个小时看网。我在站点根文件夹下创建了这个文件{myprojectname} .wpp.targets。它在使用visual studio发布时有效。媒体文件夹被忽略。我正在使用VS 2010。

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <UseMsdeployExe>true</UseMsdeployExe>
        <!-- Required for the MSDeploySkipRules to work -->
        <DeployManagedPipelineMode>Integrated</DeployManagedPipelineMode>
    </PropertyGroup>

    <PropertyGroup>
        <AfterAddIisSettingAndFileContentsToSourceManifest>
            $(AfterAddIisSettingAndFileContentsToSourceManifest);
            AddCustomSkipRules;
        </AfterAddIisSettingAndFileContentsToSourceManifest>
    </PropertyGroup>

    <Target Name="AddCustomSkipRules">
        <Message Text="Adding Custom Skip Rules - WPP Targets 2" />
        <ItemGroup>
            <MsDeploySkipRules Include="SkipErrorLogFolder">
                <SkipAction>Delete</SkipAction>
                <ObjectName>dirPath</ObjectName>
                <AbsolutePath>media</AbsolutePath>
                <XPath></XPath>
                <Apply>Destination</Apply>
            </MsDeploySkipRules>
        </ItemGroup>
    </Target>
</Project>
另一答案

我认为问题在于不正确的AbsolutePath。它应该是匹配文件或文件夹的正则表达式。所以应该适当地逃脱。下面是为我工作的示例(我想跳过删除app_offline.htm以使交付成为更大部署的一部分)

<PropertyGroup>
    <PackageUsingManifestDependsOn>$(PackageUsingManifestDependsOn);AddCustomSkipRules</PackageUsingManifestDependsOn>
  </PropertyGroup>
  <Target Name="AddCustomSkipRules">
    <ItemGroup>
      <MsDeploySkipRules Include="SkipAppOfflineOnDeploy">
        <SkipAction></SkipAction>
        <ObjectName>filePath</ObjectName>
        <AbsolutePath>app_offline.htm</AbsolutePath>
        <Apply>Destination</Apply>
        <XPath></XPath>
      </MsDeploySkipRules>
    </ItemGroup>
  </Target>
另一答案

适用于我:我的Web解决方案中的App_Data / PublishProfiles文件夹中的My prepprod.pubxml文件。 Web部署不再从VS 2015中删除webdeploy上的cachefiles文件夹中的文件。第一个PropertyGroup是使用Visual Studio中的Web发布gui自动生成的。我添加了第二个PropertyGroup,以及之前评论中的Target部分。

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of y

以上是关于在Visual Studio 2012中使用MSBuild PublishProfile时,MSDeploy跳过规则的主要内容,如果未能解决你的问题,请参考以下文章

MSB3021 在 Visual Studio 中运行 Service Fabric 应用时无法复制文件找不到部分路径错误

Visual Studio:error MSB8020

如何在仍然使用 sysnative 虚拟文件夹的同时使用 Visual Studio 的 Exec 代码 -1 避免错误 MSB3073

错误 MSB3073:命令“grunt dist”在 Visual Studio 2017 中以代码 3 退出

visual studio 2019 error MSB3073 extied with code 0

Visual Studio 2019 编译.Net Core Console项目出现MSB4018 The "CreateAppHost" task failed unexpec(