MSBuild 配置转换问题 - 目标文件夹和将文件恢复到原始状态

MSBuild config transform issues - desination folder and reverting file back to original state

问题:

DataFolder.config 文件没有恢复到以前的状态(patch 元素的值)。此外,none 的自定义配置文件转换被推送到目标文件夹。

有趣的是 ConnectionStrings.config 文件在转换后正在转换回原始状态,而不是 DataFolder.config

<Target Name="BeforeBuild">
    <!-- Web.config, ConnectionStrings.config, DataFolder.config
         Duplicates original config files into a temp file -->
    <Copy SourceFiles="Web.config" DestinationFiles="Web.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(ProjectDir)\App_Config\Include\zzz\DataFolder.config" DestinationFiles="$(ProjectDir)\App_Config\Include\zzz\DataFolder.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(ProjectDir)\App_Config\ConnectionStrings.config" DestinationFiles="$(ProjectDir)\App_Config\ConnectionStrings.temp.config" OverwriteReadOnlyFiles="True" />
    <!-- Tranforms config files -->
    <TransformXml Source="Web.temp.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    <TransformXml Source="$(ProjectDir)\App_Config\Include\zzz\DataFolder.temp.config" Transform="$(ProjectDir)\App_Config\Include\zzz\DataFolder.$(Configuration).config" Destination="$(ProjectDir)\App_Config\Include\zzz\DataFolder.config" />
    <TransformXml Source="$(ProjectDir)\App_Config\ConnectionStrings.temp.config" Transform="$(ProjectDir)\App_Config\ConnectionStrings.$(Configuration).config" Destination="$(ProjectDir)\App_Config\ConnectionStrings.config" />
  </Target>
  <Target Name="AfterPublish123" AfterTargets="MSDeployPublish">
    <!-- Overwrite transformed config files with the temps. 
         Delete temp files for Web.config, ConnectionStrings.config, DataFolder.config -->
    <Copy SourceFiles="Web.temp.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="Web.temp.config" />
    <Copy SourceFiles="$(ProjectDir)\App_Config\Include\zzz\DataFolder.temp.config" DestinationFiles="$(ProjectDir)\App_Config\Include\zzz\DataFolder.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="$(ProjectDir)\App_Config\Include\zzz\DataFolder.temp.config" />
    <Copy SourceFiles="$(ProjectDir)\App_Config\ConnectionStrings.temp.config" DestinationFiles="$(ProjectDir)\App_Config\ConnectionStrings.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="$(ProjectDir)\App_Config\ConnectionStrings.temp.config" />
  </Target>

DataFolder.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sc.variable name="dataFolder">
      <patch:attribute name="value">#{sitecore.DataFolder}</patch:attribute>
    </sc.variable>
  </sitecore>
</configuration>

DataFolder.Release.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sc.variable name="dataFolder" xdt:Locator="Match(name)" xdt:Transform="Replace">
      <patch:attribute name="value">c:\inetpub\wwwroot\fb822\Data</patch:attribute>
    </sc.variable>
  </sitecore>
</configuration>

发布完成后,patchDataFolder.config 元素现在具有 DataFolder.Release.config 元素值的值,这不应该。

问题是 AfterPublish 不是有效任务。 运行 "after publishing" 的正确任务是:

<Target Name="CustomPostPublishActions" AfterTargets="GatherAllFilesToPublish">

请注意,Name 属性无关紧要,它可以是任何东西。这是完成的输出:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="BeforeBuild">
    <!--   Web.config, ConnectionStrings.config, DataFolder.config
         Duplicates original config files into a temp file -->
    <Copy SourceFiles="Web.config" DestinationFiles="Web.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" OverwriteReadOnlyFiles="True" />
    <!--  Tranforms config files -->
    <TransformXml Source="Web.temp.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    <TransformXml Source="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" Transform="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.$(Configuration).config" Destination="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.config" />
    <TransformXml Source="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" Transform="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.$(Configuration).config" Destination="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.config" />
  </Target>
  <Target Name="CustomPostPublishActions" AfterTargets="GatherAllFilesToPublish">
    <!-- Overwrite transformed config files with the temps. 
         Delete temp files for Web.config, ConnectionStrings.config, DataFolder.config -->
    <Copy SourceFiles="Web.temp.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="Web.temp.config" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" />
  </Target>

编辑:

添加到我的问题上,当其他开发人员 "built" 项目并且没有发布它时,转换文件将更改原始配置,这不是所需的方法。因此,由于我们使用 File Deploy 作为 One-Click Publish,所以有一个名为 CopyAllFilesToSingleFolderForPackageTarget,它在文件被推送到目录之前被调用。

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="CustomPrePublishActions" BeforeTargets="CopyAllFilesToSingleFolderForPackage">
    <!-- Duplicates original config files into a temp file -->
    <Copy SourceFiles="Web.config" DestinationFiles="Web.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.temp.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.temp.config" OverwriteReadOnlyFiles="True" />
    <!--  Tranforms config files -->
    <TransformXml Source="Web.temp.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    <TransformXml Source="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" Transform="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.$(Configuration).config" Destination="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.config" />
    <TransformXml Source="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" Transform="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.$(Configuration).config" Destination="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.config" />
    <TransformXml Source="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.temp.config" Transform="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.$(Configuration).config" Destination="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.config" />
    <TransformXml Source="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.temp.config" Transform="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.$(Configuration).config" Destination="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.config" />
  </Target>
  <Target Name="CustomPostPublishActions" AfterTargets="GatherAllFilesToPublish">
    <!-- Overwrite transformed config files with the temps. -->
    <Copy SourceFiles="Web.temp.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.temp.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.config" OverwriteReadOnlyFiles="True" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.temp.config" DestinationFiles="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.config" OverwriteReadOnlyFiles="True" />
    <!-- Delete temp files -->
    <Delete Files="Web.temp.config" />
    <Delete Files="$(MSBuildProjectDirectory)\App_Config\Include\zzz\DataFolder.temp.config" />
    <Delete Files="$(MSBuildProjectDirectory)\App_Config\ConnectionStrings.temp.config" />
    <Delete Files="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.CDN.temp.config" />
    <Delete Files="$(MSBuildProjectDirectory)\App_Config\Include\zzzSites\Cinnabon\Cinnabon.SiteDefintion.temp.config" />
  </Target>