Calling FxCop from MsBuild
It seems that everyone has been talking about TDD and setting up a Continuous Integration server. The goal is very simple. Build software in less time that is more robust and easier to maintain. One or the tools that helps in this process is FxCop. FxCop will inspect your compiled dll’s to ensure that they follow design rules and naming conventions that you specify. FxCop interestingly enough was created by Microsoft internally to ensure that the .Net framework met their own guid lines. FxCop has a GUI but it will also create an XML file that can be used by CruiseControl.net or most likely any other build server.
Less talk and more code… the following is an example of how to call fxcop from the command line.
1: FxCopCmd.exe /searchgac /rule:NamingRules.dll /file:My.Framework.dll /out:fxCop.xml
Parameters that are passed in
/searchgac – Tells FxCop to search the gac for any referenced dll's not found in say the bin folder. If you forget this FxCop will error out.
/rule:Rull.dll – You can specify as many rules as you like to check against your compiled dll just repeat this command with each of the Rules you like. Unless you have a specific FxCop project you must specify at least one rule.
/file:YourCompiled.dll – Much like the rule parameter you can specify as many .dll’s as you would like to check but need at least one.
/out:someXml.xml – You are going to want to specify an XML file to output the results to so that you can integrate the results in your build log. CruiseControl.net uses XSL to transform the generated XML into HTML for viewing in its Web Dashboard and auto generated Emails.
Putting it all together and error 9009
Now that you know what its doing lets put it together and show how to call FxCop from MsBuild’s “Exec” Task.
1: <Target Name="FxCop" DependsOnTargets="BuildProject>
2: <Exec Command=""C:\Program Files\Microsoft FxCop 1.36\FxCopCmd.exe"
/searchgac /rule:"C:\Program Files\Microsoft FxCop 1.36\Rules\NamingRules.dll"
/file:C:\Project\bin\My.Framework.dll /out:fxCop.xml" ContinueOnError="true">
3: </Exec>
4: </Target>
Error 9009- I saw a lot of entries about this error when googling to figure it out myself. The error is caused by calling C:\Program Files\.. without putting quotes around it (“C:\Program Files..”). As we are calling it from an MsBuild file which is XML we have to encode the quotes as “"”
ContinueOnError – Unless you have written your own FxCop rules you are most likely going to want to set this value to true so that your build doesn’t fail each time you break one of the many rules that FxCop specifies.