Posted By

jstrassburg on 10/07/10


Tagged

webconfig transformation MSBuild


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Web.config transformation in MSBuild script


 / Published in: XML
 

This snippet transforms web.config files after the source Get. We are doing automatic deployment from our build server (not from a desktop build) and I wanted to make use of the web.config transformations in VS2010.

It uses the MSBuild extension pack to change file attributes. If anyone knows an easier way to do this please let me know.

  1. <!-- NOTE: You must set the build action to None (instead of Content) on the web.configs in your project
  2. to prevent the build from blowing up when it cannot copy the deleted original configs. -->
  3.  
  4. <PropertyGroup>
  5. <!-- Register the WebConfigTransformation target as a dependency of the get operation -->
  6. <GetDependsOn>
  7. $(GetDependsOn);
  8. WebConfigTransformation;
  9. </GetDependsOn>
  10. </PropertyGroup>
  11.  
  12. <Target Name="WebConfigTransformation">
  13. <!-- Transform web.config files -->
  14. <BuildStep
  15. Name="TransformConfigurationFiles"
  16. Message="Transforming configuration files for: %(ConfigurationToBuild.FlavorToBuild)"
  17. TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildURI)"
  18. Condition="'$(IsDesktopBuild)'!='true'">
  19.  
  20. <Output TaskParameter="Id" PropertyName="StepId" />
  21. </BuildStep>
  22.  
  23. <!-- Set all the files we will be using as writable. -->
  24. <ItemGroup>
  25. <_WebConfigFiles Include="$(SolutionRoot)\MyFirstProject\*.config">
  26. <Attributes>Normal</Attributes>
  27. </_WebConfigFiles>
  28. <_WebConfigFiles Include="$(SolutionRoot)\MySecondProject\*.config">
  29. <Attributes>Normal</Attributes>
  30. </_WebConfigFiles>
  31. <!-- etc for more config files in your solution -->
  32. </ItemGroup>
  33. <MSBuild.ExtensionPack.FileSystem.File
  34. TaskAction="SetAttributes"
  35. Files="@(_WebConfigFiles)" />
  36.  
  37. <!-- Execute msbuild with the command line switches to transform the configuration files -->
  38. <Exec Command="$(FrameworkVersion40Dir)\msbuild.exe &quot;$(SolutionRoot)\MyFirstProject\MyFirstProject.csproj&quot; /T:TransformWebConfig /P:Configuration=%(ConfigurationToBuild.FlavorToBuild);RunCodeAnalysis=Never"/>
  39. <Exec Command="$(FrameworkVersion40Dir)\msbuild.exe &quot;$(SolutionRoot)\MySecondProject\MySecondProject.csproj&quot; /T:TransformWebConfig /P:Configuration=%(ConfigurationToBuild.FlavorToBuild);RunCodeAnalysis=Never"/>
  40. <!-- etc for more projects -->
  41.  
  42. <!-- Delete the other original configs -->
  43. <Delete Files="@(_WebConfigFiles)" />
  44.  
  45. <!-- Copy the transformed config to the root -->
  46. <Copy
  47. SourceFiles="$(SolutionRoot)\MyFirstProject\obj\%(ConfigurationToBuild.FlavorToBuild)\TransformWebConfig\Transformed\Web.config"
  48. DestinationFiles="$(SolutionRoot)\MyFirstProject\Web.config" />
  49. <Copy
  50. SourceFiles="$(SolutionRoot)\MySecondProject\obj\%(ConfigurationToBuild.FlavorToBuild)\TransformWebConfig\Transformed\Web.config"
  51. DestinationFiles="$(SolutionRoot)\MySecondProject\Web.config" />
  52. <!-- etc for more projects -->
  53.  
  54. <BuildStep Id="$(StepId)" Name="TransformConfigurationFiles" Status="Succeeded"
  55. TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildURI)"
  56. Condition="'$(IsDesktopBuild)'!='true'" />
  57. <OnError ExecuteTargets="WebConfigTransformationError"/>
  58. </Target>
  59. <Target Name="WebConfigTransformationError">
  60. <BuildStep Id="$(StepId)" Name="WebConfigTransformation" Status="Failed"
  61. TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildURI)"
  62. Condition="'$(IsDesktopBuild)'!='true'" />
  63. </Target>

Report this snippet  

You need to login to post a comment.