How to get the msbuild task to perform configuration conversions in a file collection?

I am trying to convert all web.config files to a project that I have, here is my tree structure:

  • Transform.bat
  • Transformations
    • ConfigTransform.proj
    • Web.Transform.config
  • Web site
    • web.config
    • Views
      • web.config

There are more web.config files, but the idea is that they will find all of them and apply the same configuration conversion to them.

I took some tips from the blog post I found , but I was stuck in the last step, the actual conversion. Also there is a slightly rough part in the middle that I don’t really like (I don’t quite understand what I am doing, and I obviously am doing it wrong). Here where I still have:

<Project ToolsVersion="4.0" DefaultTargets="Transform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask TaskName="TransformXml" AssemblyFile="Tools\Microsoft.Web.Publishing.Tasks.dll"/> <PropertyGroup> <SitePath>..\..\Website</SitePath> <WebConfigTransformInputFile>$(SitePath)\Web.config</WebConfigTransformInputFile> <WebConfigTransformFile>Web.Transform.config</WebConfigTransformFile> <OutDir>..\N\N\</OutDir> </PropertyGroup> <ItemGroup> <_FilesToTransform Include="$(SitePath)\**\web.config"/> </ItemGroup> <Target Name="Transform"> <MakeDir Directories="@(_FilesToTransform->'$(OutDir)%(RelativeDir)')" /> <TransformXml Source="@(_FilesToTransform->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')" Transform="$(WebConfigTransformFile)" Destination="@(_FilesToTransform->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')" /> </Target> </Project> 

My Transform.bat looks like this:

 %systemroot%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe %CD%\Transforms\ConfigTransform.proj 

Therefore, when I call the package, the corresponding directories are created. However, as you can see, I had to be a little creative with OutDir, making it .. \ N \ N. For some reason, if I do not, the OutDir path will be exactly the same as the input directory. So I clearly need to change something in MakeDir, but I'm not sure what.

The real problem arises when it begins to make conversions. I tried to save the TransformXml Source parameter this way or this:

 @(_FilesToTransformNotAppConfig->'%(FullPath)') 

The latter gives me the error "Could not open source file: this path format is not supported." and the first gives me this result:

 Build started 30-4-2012 14:02:48. Project "D:\Dev\transform\DoTransforms\Transforms\ConfigTransform.proj" on node 1 (default targets). Transform: Creating directory "..\N\N\..\..\Website\Views\". Transforming Source File: ..\N\N\..\..\Website\Views\Web.config;..\N\N\..\..\Website\Web.config D:\Dev\transform\DoTransforms\Transforms\ConfigTransform.proj(32,2): error : Could not open Source file: Could not find a part of the path 'D:\Dev\transform\DoTransforms\Website\Views\Web.config;\Website\Web.config'. Transformation failed Done Building Project "D:\Dev\transform\DoTransforms\Transforms\ConfigTransform.proj" (default targets) -- FAILED. Build FAILED. 

Summarizing my questions:

  • How to avoid path problems for OutDir? I have searched for several ways, but I cannot figure it out.
  • How to make the TransformXml task accept multiple files in the Source attribute?
+8
msbuild slowcheetah web-config-transform msbuild-task
source share
1 answer

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

In my example, I find a conversion sitting next to the web.config file itself. For your scenario, you can simply use the MSBuild property, which points 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 the full sample at https://github.com/sayedihashimi/sayed-samples/tree/master/TransformMultipleWebConfigs .

+10
source share

All Articles