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

≡放荡痞女 提交于 2019-11-30 07:15:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!