How do I get an msbuild task to do config transforms on a collection of files?

前端 未结 1 478
礼貌的吻别
礼貌的吻别 2020-12-30 12:00

I am trying to transform all of the web.config files in a project I have, here\'s a my tree structure:

  • Transform.bat
  • Transforms
    • ConfigTransfo
相关标签:
1条回答
  • 2020-12-30 12:48

    I think you were pretty close. I have pasted a sample below which shows how to do this.

    In my sample I discover the transform sitting next to the web.config file itself. For your scenario you can just use an MSBuild property pointing to a specific file.

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="TransformAll" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
      <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
    
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
        <OutputFolder Condition=" '$(OutputFolder)'=='' ">C:\temp\transformed-files\</OutputFolder>
      </PropertyGroup>
    
      <!--
      This target shows how to transform web.config with a specific transform file associated to that specific web.config file.
      -->
      <Target Name="TransformAll">
    
        <!-- discover the files to transform -->
        <ItemGroup>
          <FilesToTransofm Include="$(MSBuildProjectDirectory)\**\web.config"/>
        </ItemGroup>
    
        <!-- Ensure all target directories exist -->
        <MakeDir Directories="@(FilesToTransofm->'$(OutputFolder)%(RecursiveDir)')"/>
    
        <!-- TransformXml only supports single values for source/transform/destination so use %(FilesToTransofm.Identity)
             to sned only 1 value to it -->
        <TransformXml Source="%(FilesToTransofm.Identity)"
                      Transform="@(FilesToTransofm->'%(RecursiveDir)web.$(Configuration).config')"
                      Destination="@(FilesToTransofm->'$(OutputFolder)%(RecursiveDir)web.config')" />    
      </Target>
    
    </Project>
    

    FYI you can download a full sample at https://github.com/sayedihashimi/sayed-samples/tree/master/TransformMultipleWebConfigs.

    0 讨论(0)
提交回复
热议问题