This post originated from an RSS feed registered with .NET Buzz
by Peter G Provost.
Original Post: MSBuild Custom Metadata
Feed Title: Peter Provost's Geek Noise
Feed URL: /error.aspx?aspxerrorpath=/Rss.aspx
Feed Description: Technology news, development articles, Microsoft .NET, and other stuff...
Before coming to MS, I wrote a bunch of general purpose NAnt scripts to help people do things like automate their builds, set up CC.NET and other fun things.
Of course, you have to imagine that I couldn’t just sit here and not explore MSBuild a bit here and there.
So while putting together a bootstrap MSBuild file for our build server, I discovered a cool feature I wish NAnt had had “back in the day”: Custom Metadata.
Suppose I want to create a target called UnitTest that called NUnit-Console.exe on a number of assemblies. Also assume that for various reasons, I can’t use wildcards to figure out which assemblies to use. I have to specify which specific assemblies I want it to run against.
One more thing, I want the tests to run with the current working directory set to the bin\Debug directory for that test assembly.
In NAnt, I would have had to use <foreach> and a number of other nasty property constructs, but in MSBuild it is a simple as this. (Please note the line numbers… I have manually wrapped some lines to make them fit on the page better.)
The “trick” is in the UnitTest target. The @(TestAssembly) tells MSBuild to do that task once for each item named “TestAssembly” in the above <ItemGroup>. The %(WorkingDirectory) says to pull the WorkingDirectory metadata element from the specific TestAssembly item that is being used for that task.
That is very cool. I think I may be able to find all kinds of interesting uses for this kind of custom metadata. I certainly don’t think I could have written the same funtionality in NAnt with a script this short.
Also, there is a bunch of built-in, so called “Well Known Metadata” documented over in MSDN. Check it out.