I thought I would do a short but sweet blog post about getting web.config transforms (and any other XDT transform) working when automating your project builds with Team Build 2010.
I’ll start off talking about the MSBuild side of this, and then how we go about integrating it into our automated build in Team Build 2010.
This seems to be a bit of a contentious issue, and there seems to be a LOT of people doing a lot of swinging and missing with various MSBuild targets. It seems a lot of people leverage MSBuild, but a lot don’t take the time to understand it. The best place to start digging into the guts of MSBuild’s various functionalities are the targets files your build configuration uses. These detail the various targets you can utilize which expose different functionalities used during the build process.
If you open up your web application project’s csproj file and do a ctrl + F for “Import”, you should see two import statements here – one for the standard CSharp targets file, and one for the WebApplication targets. The CSharp one is not the one we really want to dig in to for this post (It just defines the targets for ‘standard’ C# projects); we need to open up Microsoft.WebApplications.Targets, and see if there is anything in there we can utilize to ensure the transformations are run.
You’ll notice that the second target specified in the file, named _CopyWebApplication, has a short blurb commented above it, which states “It allow to leverage the web.config transformation”. Engrish aside, the targets files are actually commented very well, and are fairly clear about each targets intended use, dependencies and so forth.
So to ensure our web.config transformations are performed, we will specify /p:UseWPP_CopyWebApplication=true. Now if you went into Microsoft.Web.Publish.Targets and had a look to see what that property is used for, you might have noticed there is a check performed in a Property group on PipelineDependsOnBuild just below where UseWPP_CopyWebApplication is defined. It states “We use the following property to detect BuildCircularDependency that we might introduce in the msbuild due to $(UseWPP_CopyWebApplication) and $(PipelineDependsOnBuild) both set to true instead of letting msbuild failed error early with no message, we record message tweak it to make the build work then Log the error messsage later to allow user to fix the issue.” Basically it is saying we need to set PipelineDependsOnBuild to false otherwise there will be a circular dependency introduced. If you didn’t happen to catch this it actually outputs a fairly informative error the first time you build with only UseWPP_CopyWebApplication specified anyway.
If you really want to get into the nitty gritty of the build process and see where and how the transforms are performed, just keep on reading the Microsoft.Web.Publishing.targets file. It might be a bit tough to keep track of things without a pen and paper handy though!
So how do we combine this with our Build definition in Team Build 2010? I am not going to go over how to set up the build def, there are plenty of blogs for that already. All you need to do is right click your build definition and select “Edit Build Definition…”, then select the “Process” item. Expand the “Advanced” area, and under MSBuild Arguments, append /p:UseWPP_CopyWebApplication=True /p:PipelineDependsOnBuild=False. Now when the build is dropped, the site under _PublishedWebsites will have had the transformations applied to its configuration files.
I am using this in conjunction with Vishal Joshi’s solution for applying XDTs to other xml and configuration files within your solution to perform transformations in other .config files at the same time. Vishal’s blog has been more useful than any other single resource for learning about MSBuild and Team Build 2010 I have come across.
Print | posted on Friday, 17 June 2011 10:40 AM