Best way to integrate StyleCop with TFS CI

11,204

Solution 1

Some points for using StyleCop in general:

  • You don't need to have StyleCop installed on your (or the build) machine - simply add StyleCop's binaries to your source control (about 1.5 MB of stuff)
  • Then simply add the relative reference to your project files, like this (I left out < and >):

    Import Project="..\..\lib\Microsoft StyleCop\Microsoft.StyleCop.Targets"

  • We treat all StyleCop warnings as errors using the StyleCopTreatErrorsAsWarnings setting in the project file.

  • For legacy projects, you can use the tool provided by Microsoft to automatically set all existing source files to ignore.
  • You can put the StyleCop settings file (Settings.StyleCop) in the root directory where your VS solution is (and of course place it under the source control). StyleCop knows how to find this file automatically (by loooking up the directory tree). This way you can have custom settings for each solution.
  • We turned off certain StyleCop rules which we think aren't very practical to follow.

This way StyleCop will be integrated in your compile step and as such it will become a part of the CI build.

Solution 2

We use the approach in the following articles successfully on a couple of projects...

http://blogs.msdn.com/sourceanalysis/pages/source-analysis-msbuild-integration.aspx http://blogs.msdn.com/sourceanalysis/archive/2008/11/11/introducing-stylecop-on-legacy-projects.aspx

basically you set a couple of environment variables up, reference them in all the .csproj files so that you can cater for different working directories for each individual developer, install the StyleCop MSBuild components on the build machine and you're off. No changes to the team build required.

Solution 3

Go to Visual Studio, right click Solution > Manage Nuget Packages > Install StyleCop.MSBuild.

That's it =)

Share:
11,204
Slavo
Author by

Slavo

An internet addict interested in everything web and mobile. #SOreadytohelp Passionate about REST, APIs, MVC frameworks (client and server), user experience. A guitarist. A snowboarder. A tennis newbie. A traveler. An all-around nice guy. I am currently working for Zopa as a senior developer. Blogging at http://www.slavoingilizov.com

Updated on June 03, 2022

Comments

  • Slavo
    Slavo almost 2 years

    I've been doing research on how to enable source analysis for the project I'm working on and plan to use StyleCop. The setup I have is a TFS Server for source control, using TFS Continuous Integration. I want to enable source analysis for CI builds and daily builds run on the build machine, and not only for those run on developers' machines.

    Here's an article from the documentation of StyleCop that I read on the subject: http://blog.newagesolution.net/2008/07/how-to-use-stylecop-and-msbuild-and.html. It basically modifies the csproj file for the purpose.

    I've also read other opinions about how StyleCop should be integrated with build automation, which advise doing the same thing using build tasks:

    http://blog.newagesolution.net/2008/07/how-to-use-stylecop-and-msbuild-and.html http://freetodev.spaces.live.com/blog/cns!EC3C8F2028D842D5!400.entry.

    What are you opinions? Have you had similar projects and done something like this?

  • Jeff Wilcox
    Jeff Wilcox over 14 years
    Exactly how my team does this. We don't mind the .csproj changes, it helps us add other functionality and custom tasks along the way.
  • Iain Galloway
    Iain Galloway over 11 years
    There appears to be a nuget package for this now! :D
  • Maurício
    Maurício over 10 years
    For reference, the NuGet package is StyleCop.MSBuild