Friday, 27 March 2009

XmlMultiMassUpdate - Extending MSBuild Community Task's XmlMassUpdate

The MSBuild Community Tasks library provides lots of additional and very useful functionality to MSBuild through new MSBuild tasks. One task that we are using on our build server to merge, at build time, certain configuration settings based on the environment we are building for is the XmlMassUpdate task.

XmlMassUpdate takes a source XML document and updates it from another XML document from a particular document root. Our merging routine operates as described in Doran Yaacoby's excellent blog post.

The Problem


The issue we were having is that XmlMassUpdate can operate only on a single XML file. For our build process, we wanted to merge the contents of all substitution.config files into all appSettings.config in the entire solution tree. This is because we use have multiple executing assemblies (ie. web applications, class libraries for unit tests, console applications, etc.) that are being built in a single solution. Also, because we use a templated CruiseControl.NET configuration (and the maintenance overhead), we could not manually hard-code individual XmlMassUpdate tasks in each solution to update the config files. The ability to operate only on a single file at a time was causing problems.

The Solution


To overcome this we created our own XmlMultiMassUpdate task which is based on the XmlMassUpdate task. The task now can take an item group with multiple files and will update all the files in the set. For example:


<ItemGroup>
<ContentFiles Include="C:\example\**\config\appSettings.config" />
<Substitution Files Include="C:\example\**\config\substitutions.config" />
</ItemGroup>

<XmlMultiMassUpdate
ContentFile="@(ContentFiles)"
SubsitutionsFile="@(SubstitutionFiles)"
ContentRoot="/"
SubstitutionsRoot="/" />



Constraints


There are a some constraints when using a file set (rather than an individual file) in the XmlMultiMassUpdate plugin:

  1. The number of files in the content item group and the substitutions item group must be the same.

  2. The original XmlMassUpdate allows the specification of a 'MergeFile' where the output will be written. When using multiple files a merge file cannot be specified. Instead the substitutions file is merged into the content file and the content file gets overwritten with new content.



The Full Source


I have not made the full source code for the XmlMultiMassUpdate available in this post. Mainly because it has been done as part of this project at the NHS Information Centre and all such things should follow the appropriate channels. If you really think it would be useful to you though, please email me at talk@peppermint-it.com and I'll see what I can do.

No comments:

Post a Comment