Sometimes it’s convenient to store values of some build properties in an external XML file so that instead of modifying the script itself, you’d rather want to modify only this external XML. In this XML file you’d like to store connection strings, build paths and other values which are “local” in a sense that they apply only to specific build environment (your machine, coninuous integration server etc.)
NAnt folks use a technique involving “include” directive which normally is used to cross-reference other scripts. However, it’s possible to “include” a script containing only a list of properties.
So, how do you achieve such functionality in MsBuild? Let’s start with the local.properties.xml file:
Note the XML namespace – this is to preserve the compatibility with NAnt scripts.
Then comes the MsBuild script, which uses the XmlRead task from community tasks:
The output of this script would be:
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.225]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 2011-04-21 11:43:00.
Project "C:\tmp\msbuild_test\testbuild.xml" on node 1 (default targets).
Reading Xml Document "local.properties.xml".
1 node(s) selected for read.
XmlRead Result: "mymachine\sql2008"
Done Building Project "C:\tmp\msbuild_test\testbuild.xml" (default targets)