views:

706

answers:

3

I am trying my hardest to define a list of CodeAnalysisRules that should be omitted from the Code Analysis tools when MSBuild executes my TFSBuild.proj file.

But each time I test it, my list of Code Analysis Rules to exclude are ignored and Team Build just simply honors the Code Analysis Rules settings for each project.

Anyone have an example of a TFSBuild.proj file that shares one list of Code Analysis Rules exceptions for all projects that are build in Team? I am using Team System 2008.

Thanks for any assistance?

A: 

Well, it took me a while to find this but here is the best answer I could find

http://bloggingabout.net/blogs/rick/archive/2007/09/04/howto-disable-specific-code-analysis-rules-for-a-team-build.aspx

It works, and it does let me enforce a consistent set of rules across all of my solutions without having to visit each project's file and that is huge for me as we tweak our rulesets and don't wish to manually update each project each time

Anyone else know of anything that works? - this was a VS2005 solution/workaround so I was hoping that it would be easier in VS2008 Team System but so far haven't found anything else

Brad Osterloo
+1  A: 

We do this via the Code Analysis Check-in Policy. You can configure this via Team System. To implement, simply choose your rules and then Right Click Solution -> Replace Code Analysis Settings with Check-in Policy.

Adam Fyles
+1  A: 

I don't see much point in trying to override the csproj settings with a different set - surely you want to use the same CA settings (or disable CA entirely) whenever and wherever you build the code?

As foosnazzy says, there's (usually) no need to do this in MSBuild. You can use the CA check in policy to set up the rules you wish to apply. Then, right click your solution in the solution explorer and about half way down the context menu there is a submenu of options for applying the TFSProjects' CA policy to all Projects in the solution. You can overwrite the project settings with the server's, or merge them.

It may only copy the settings for the current configuration so you may need to do it twice if you want to apply the same CA settings to Debug and Release. (I vaguely remember this happening but we don't run CA on our release build so it's not something I've tried recently)

(This was available in VSTS2005, but it didn't work - the values were "merged" with those in the projects so that any existing CA rules would be increased in severity (to warning or error) but you couldn't disable CA rules (demote error -> warning -> disabled). In addition, every time you opened a solution the CA settings would "drift" so that you had to reapply them every few days to keep it working)

An alternative is to set up the CA rules you want in a single project, find the XML element that contains the list, and use a text editor (or a few lines of C# code) to do a global search & replace for the CA element in all csproj files in your project. That's how I did it until VSTS2008 came along - once you've sussed the technique it only takes a few seconds to migrate your settings through all the csprojs. The advantage of this is you can be more selective about which projects the CA rules are applied to.

Jason Williams
What if you don't use TFS?
Jeffrey Cameron
@Jeffrey: It depends entirely on which product you're using for CA, but the principle in the last paragraph of my answer probably applies to most/all CA tools (it should usually be possible to duplicate the settings manually, if you can find where they're stored).
Jason Williams