Return to Snippet

Revision: 33219
at October 7, 2010 03:38 by jstrassburg


Initial Code
<!-- NOTE: You must set the build action to None (instead of Content) on the web.configs in your project
     to prevent the build from blowing up when it cannot copy the deleted original configs. -->

<PropertyGroup>
  <!-- Register the WebConfigTransformation target as a dependency of the get operation -->
  <GetDependsOn>
    $(GetDependsOn);
    WebConfigTransformation;
  </GetDependsOn>
</PropertyGroup>

<Target Name="WebConfigTransformation">
  <!-- Transform web.config files -->
  <BuildStep 
    Name="TransformConfigurationFiles" 
    Message="Transforming configuration files for: %(ConfigurationToBuild.FlavorToBuild)"
    TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildURI)"
    Condition="'$(IsDesktopBuild)'!='true'">
    
    <Output TaskParameter="Id" PropertyName="StepId" />
  </BuildStep>

  <!-- Set all the files we will be using as writable. -->
  <ItemGroup>
    <_WebConfigFiles Include="$(SolutionRoot)\MyFirstProject\*.config">
      <Attributes>Normal</Attributes>
    </_WebConfigFiles>
    <_WebConfigFiles Include="$(SolutionRoot)\MySecondProject\*.config">
      <Attributes>Normal</Attributes>
    </_WebConfigFiles>
    <!-- etc for more config files in your solution -->
  </ItemGroup>
  <MSBuild.ExtensionPack.FileSystem.File
          TaskAction="SetAttributes"
          Files="@(_WebConfigFiles)" />

  <!-- Execute msbuild with the command line switches to transform the configuration files -->
  <Exec Command="$(FrameworkVersion40Dir)\msbuild.exe &quot;$(SolutionRoot)\MyFirstProject\MyFirstProject.csproj&quot; /T:TransformWebConfig /P:Configuration=%(ConfigurationToBuild.FlavorToBuild);RunCodeAnalysis=Never"/>
  <Exec Command="$(FrameworkVersion40Dir)\msbuild.exe &quot;$(SolutionRoot)\MySecondProject\MySecondProject.csproj&quot; /T:TransformWebConfig /P:Configuration=%(ConfigurationToBuild.FlavorToBuild);RunCodeAnalysis=Never"/>
  <!-- etc for more projects -->
  
  <!-- Delete the other original configs -->
  <Delete Files="@(_WebConfigFiles)" />
  
  <!-- Copy the transformed config to the root -->
  <Copy 
    SourceFiles="$(SolutionRoot)\MyFirstProject\obj\%(ConfigurationToBuild.FlavorToBuild)\TransformWebConfig\Transformed\Web.config" 
    DestinationFiles="$(SolutionRoot)\MyFirstProject\Web.config" />
  <Copy 
    SourceFiles="$(SolutionRoot)\MySecondProject\obj\%(ConfigurationToBuild.FlavorToBuild)\TransformWebConfig\Transformed\Web.config" 
    DestinationFiles="$(SolutionRoot)\MySecondProject\Web.config" />
  <!-- etc for more projects -->

  <BuildStep Id="$(StepId)" Name="TransformConfigurationFiles" Status="Succeeded"
    TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildURI)"
    Condition="'$(IsDesktopBuild)'!='true'" />
  <OnError ExecuteTargets="WebConfigTransformationError"/>
</Target>
<Target Name="WebConfigTransformationError">
  <BuildStep Id="$(StepId)" Name="WebConfigTransformation" Status="Failed"
    TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildURI)"
    Condition="'$(IsDesktopBuild)'!='true'" />
</Target>

Initial URL

                                

Initial Description
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.

Initial Title
Web.config transformation in MSBuild script

Initial Tags

                                

Initial Language
XML