(For more resources related to this topic, see here.)
In this article we will see how to build and display StyleCop errors in Jenkins/Hudson jobs. To do so, we will need to see how to configure the Jenkins job with a full analysis of the C# files in order to display the technical debt of the project. As we want it to diminish, we will also set in the job an automatic recording of the last number of violations. Finally, we will return an error if we add any violations when compared to the previous build.
For this article you will need to have:
<?xml version="1.0" encoding="utf-8" ?>
<Project DefaultTargets="StyleCop" >
<UsingTask TaskName="StyleCopTask" AssemblyFile="$(MSBuildExtens
ionsPath)..StyleCop 4.7StyleCop.dll" />
<PropertyGroup>
<!-- Set a default value of 1000000 as maximum Stylecop
violations found -->
<StyleCopMaxViolationCount>1000000</StyleCopMaxViolationCount>
</PropertyGroup>
<Target Name="StyleCop">
<!-- Get last violation count from file if exists -->
<ReadLinesFromFile Condition="Exists('violationCount.txt')"
File="violationCount.txt">
<Output TaskParameter="Lines" PropertyName="StyleCopMaxViola
tionCount" />
</ReadLinesFromFile>
<!-- Create a collection of files to scan -->
<CreateItem Include=".***.cs">
<Output TaskParameter="Include" ItemName="StyleCopFiles" />
</CreateItem>
<!-- Launch Stylecop task itself -->
<StyleCopTask
ProjectFullPath="$(MSBuildProjectFile)"
SourceFiles="@(StyleCopFiles)"
ForceFullAnalysis="true"
TreatErrorsAsWarnings="true"
OutputFile="StyleCopReport.xml"
CacheResults="true"
OverrideSettingsFile= "StylecopCustomRuleSettings.Stylecop"
MaxViolationCount="$(StyleCopMaxViolationCount)">
<!-- Set the returned number of violation -->
<Output TaskParameter="ViolationCount" PropertyName="StyleCo
pViolationCount" />
</StyleCopTask>
<!-- Write number of violation founds in last build -->
<WriteLinesToFile File="violationCount.txt" Lines="$(StyleCopV
iolationCount)" Overwrite="true" />
</Target>
</Project>
We also set it to check for changes on the subversion repository every 15 minutes.
Then, we have to launch our MSBuild script using the MSBuild task. The task is quite simple to configure and lets you fill in three fields:
As you can see in the preceding screenshot, Jenkins is again quite simple to configure. After providing the XML filename for StyleCop, you have to fix thresholds to build health and the maximum number of violations you want to display in the detail screen of each file in violation.
In the first part of the How to do it…section, we presented a build script. Let's explain what it does:
First, as we don't use the premade MSBuild integration, we have to declare in which assembly the StyleCop task is defined and how we will call it. This is achieved through the use of the UsingTask element.
Then we try to retrieve the previous count of violations and set the maximum number of violations that are acceptable at this stage of our project. This is the role of the ReadLinesFromFile element, which reads the content of a file. As we added a condition to ascertain the existence of the violationsCount.txt file, it will only be executed if the file exists. We redirect the output to the property StyleCopMaxViolationCount.
After that we have configured the Jenkins job to follow our project with StyleCop. We have configured some strict rules to ensure nobody will add new violations over time, and with the violation plugin and the way we addressed StyleCop, we are able to follow the technical debt of the project regarding StyleCop violations in the Violations page.
A summary of each file is also present and if we click on one of them, we will be able to follow the violations of the file.
How to address multiple projects with their own StyleCop settings
As far as I know, this is the limit of the MSBuild StyleCop task. When I need to address multiple projects with their own settings, I generally switch to StyleCopCmd using NAnt or a simple batch script and process the stylecop-report.violations.xml file with an XSLT to get the number of violations.
This article talked about integrating StyleCop analysis in Jensons/Hudkins. This article helped in building a job analysis for our project.