MSBuild config transform issues - desination folde

2019-09-16 16:23发布

问题:

Problem:

DataFolder.config file is not reverting back to previous state (the patch element's value). Also, none of the custom config file transforms are being pushed out to the destination folder.

What's interesting is the ConnectionStrings.config file is being converted back to the original state after the transformation, but not the 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>

After the publish is complete, the DataFolder.config element for patch now has the value of the DataFolder.Release.config element's value, which it shouldn't.

回答1:

Issue was AfterPublish is not a valid task. The correct task for running "after publishing" is:

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

Note that the Name attribute doesn't matter, it can be anything. Here's the completed output:

<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>

EDIT:

Adding onto my issue, when other developers "built" the project, and didn't publish it, the transform files would change the original configs, which is not the desired approach. Therefore, since we're using File Deploy as the One-Click Publish, there's a Target named CopyAllFilesToSingleFolderForPackage, which is called right before files are pushed to the directory.

<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>