MSBuild Script and VS2010 publish apply Web.config Transform

I was looking for similar information and didn’t quite find it, so I did some digging around in the .targets files that come with Visual Studio 2010 and MSBuild 4.0. I figured that was the best place to look for the MSBuild task that would perform the transformation.

As far as I have been able to tell, the following MSBuild task is used:

<Project ToolsVersion="4.0"
         DefaultTargets="Deploy"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask TaskName="TransformXml"
               AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

    <PropertyGroup>
        <ProjectPath>C:\Path to Project\Here</ProjectPath>
        <DeployPath>C:\Path to Deploy\There</DeployPath>
        <TransformInputFile>$(ProjectPath)\Web.config</TransformInputFile>
        <TransformFile>$(ProjectPath)\Web.$(Configuration).config</TransformFile>
        <TransformOutputFile>$(DeployPath)\Web.config</TransformOutputFile>
        <StackTraceEnabled>False</StackTraceEnabled>
    </PropertyGroup>


    <Target Name="Transform">
        <TransformXml Source="$(TransformInputFile)"
                      Transform="$(TransformFile)"
                      Destination="$(TransformOutputFile)"
                      Condition="some condition here"
                      StackTrace="$(StackTraceEnabled)" />
    </Target>
</Project>

I have tested the above and can confirm that it works. You might need to tweak the structure a bit to fit with your build script better.

Leave a Comment