TFS 2012 and web.config transforms

Nathan Greenway picture Nathan Greenway · May 1, 2013 · Viewed 12.8k times · Source

I am trying to have my TFS Build create the web.config transform associated with the configuration I selected for my build. When I run the build I check the web.config file no transforms have been applied. When I a publish from VS2012 the transform works correctly.

I have set up TFS 2012 (Update 2), and have a separate server for builds. I don't have VS2012 installed on the build server, but I copied the C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\ Web and WebApplications folders and files from my dev machine to the build server.

I have created some configurations in my project, and have added some tranforms to the associated web.{configname}.config.

I have created a build and set the Items to Build - Configurations to Build to one of the configurations in my project. I noticed that it only has Debug and Release, it didn't have any of the configurations I created. (Side question: Is that correct, or should it show all the configurations I created?)

So I run a build and check the output folder and the web.config has not applied the transforms. Is there anything else I need to do?

Answer

David De Sloovere picture David De Sloovere · Aug 8, 2013

Just throwing /p:TransformConfigFiles=true on there won't actually do anything.

You also need to add this target to the project file:

<Target Name="TransformConfigFiles" AfterTargets="AfterBuild" Condition="'$(TransformConfigFiles)'=='true'">
    <ItemGroup>
        <DeleteAfterBuild Include="$(WebProjectOutputDir)\Web.*.config" />
    </ItemGroup>
    <TransformXml Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="$(WebProjectOutputDir)\Web.config" />
    <Delete Files="@(DeleteAfterBuild)" />
</Target>

This is my source: http://blog.degree.no/2012/03/automatic-config-transformations/