I'm trying to get TeamCity to build my C# solution and execute StyleCop and fail the build if there are any violations. I want it to execute the same as when you right click the solution in Solution Explorer in Visual Studio and then click Run StyleCop
except that I would like the warnings to be treated as errors.
I've tried the suggestions in this answer, however I've had a few problems implementing it.
The problems were:
<CreateItem Include="$(SourceFolder)\**\*.cs">
was including all .cs
files in the folder structure and not respecting the individual StyleCop settings for each project (e.g. files to exclude).
Another option would be to have a build.xml
file so that it can be referenced by MSBuild. However this would mean maintaining the XML file as well as the .sln
file. I did find this answer which meant you could include the solution reference in the XML like so
<include name="MyTopSecretApplication.sln"/>
but for some reason it wasn't passing the compiled files when I had my CreateItem
set as
<CreateItem Include="@(Compile)">
which should in theory pass the compiled files in and respect include/exclude settings if condition
was set (as per the below) (I'm guessing because the XML file including the .sln
meant the compiled files weren't passed).
Condition="('%(Compile.ExcludeFromStyleCop)' != 'true') and ('%(Compile.ExcludeFromSourceAnalysis)' != 'true')"
Is there a good way I can get MSBuild to build my solution file in TeamCity, validate that StyleCop rules are followed, and respect the StyleCop rules per project?
So here's how I get this to work in my environment
StyleCopTreatErrorsAsWarnings
element with a value of false
in the relevant build configuration section, for example I only do this for Release builds. See this blog for more detail on this.