Thursday, April 28, 2011

Managing Exec Commands with Arguments in MSBuild

I recently had to write an MSBuild Exec task to run the Visual Studio 2013 Code Metrics tool from the Command Line: http://www.microsoft.com/en-us/download/details.aspx?id=41647

Unfortunately, the tool is solely managed via the Command Line and there does not appear to be any specialized  MSBuild Task for it.  Once I started digging into the syntax, I encountered a problem with passing multiple files to the command since each file needs to be passed as a separate /file: parameter as outlined in this article: http://blogs.msdn.com/b/camerons/archive/2011/01/28/code-metrics-from-the-command-line.aspx

When working with NAnt, this operation would have been relatively easy since the exec task in NAnt provides an arg parameter: http://nant.sourceforge.net/release/latest/help/tasks/exec.html

However, the MSBuild Exec task has no such arg parameter to accomplish this in just the same manner.

Fortunately, I found this blog posting which saved me a lot of grief and helped me to come with a suitable solution to run my Code Metrics via the command line so I thought I would share it with anyone else who might need it as well: http://weblogs.asp.net/lorenh/archive/2008/12/11/msbuild-trick-for-making-lt-exec-gt-calls-more-maintainable.aspx

The MSBuild trick that it utilizes leverages an ItemGroup and delimits the elements in the ItemGroup with a single blank space character ' '.  I found it extremely handy and hopefully you will too!

No comments:

Post a Comment