Tag Archives: stylecop

Enabling a project to have multiple StyleCop targets.

As the RC is now out for StyleCop 4.5 I wanted to give it a go mainly to see if the ReSharper integration included works with Resharper 6 (unfortunately it doesn’t :(). Unfortunately it includes a breaking-change if you import the StyleCop.targets file (so that violations are shown as either errors or warnings when building the project) as the path has changed.

To enable me to continue to use the 4.5RC version and my workmates to carry on using 4.4 I had to make some changes to the csproj file to cater for both:

<PropertyGroup>
    <StyleCop4_4Path>$(MSBuildExtensionsPath32)\Microsoft\StyleCop\v4.4\Microsoft.StyleCop.targets</StyleCop4_4Path>
    <StyleCop4_5Path>$(MSBuildExtensionsPath32)\StyleCop\v4.5\StyleCop.targets</StyleCop4_5Path>
</PropertyGroup>
<Import Condition="!exists($(StyleCop4_5Path))" Project="$(StyleCop4_4Path)" />
<Import Condition="exists($(StyleCop4_5Path))" Project="$(StyleCop4_5Path)" />

This disables the 4.4 target if 4.5 is installed, however if neither are installed it will fail the build trying to find 4.4. If you need to disable both if they are not installed you can replace the first import with the following:

  <Import Condition="exists($(StyleCop4_4Path))" Project="$(StyleCop4_4Path)" />