In this recipe, we will see how to automate our build process using MSBuild. We will describe which lines need to be added to the MSBuild project in order to enable StyleCop analysis and how to cap the number of violations encountered before the build breaks.
For this recipe, you will need to have:
<Import Project="$(MSBuildBinPath)Microsoft.CSharp.targets" />
<Import Project="$(ProgramFiles)MSBuildStyleCopv4.7StyleCop.targets" />
This enables StyleCop analysis on the project.
PropertyGroup
section within the project file, and then add a new XML element StyleCopMaxViolationCount
with a value of 100
. For example:<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' "> Debug </Configuration> <Platform Condition=" '$(Platform)' == '' "> AnyCPU </Platform> <ProductVersion>8.0.50727</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid> {F029E8D9-743F-4C6F-95F3-6FBDA6477165} </ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>VanillaProject</RootNamespace> <AssemblyName>VanillaProject</AssemblyName> <StyleCopMaxViolationCount> 100 </StyleCopMaxViolationCount> </PropertyGroup>
The first element we added imports the StyleCop task in the project. That's all that is really required to enable StyleCop analysis via MSBuild. The element is located just under the project root node. It can be placed anywhere as long as it's a direct child of the root node. As you can see, the path used to locate the StyleCop.Targets
file is dependant of the version you installed on your computer.
In the second part, I showed you how to modify the behavior of StyleCop by adding properties in the project.
There are 10 properties that can be modified that way; I will present the three most important to me:
StyleCopAdditionalAddinPaths
: This allows you to specify other paths for your custom rulesStyleCopTreatErrorsAsWarnings
: This allow you to turn your StyleCop violations into build errorsStyleCopMaxViolationCount
: This allows you to specify the maximum number of violations we accept in the project before breaking the buildHere is some other information that might come in handy in certain scenarios.
In this recipe, we have seen how to modify the StyleCop task behavior on a project basis. However, we can set the behavior properties as environment variables on the machine, or within the build environment command window. Setting the properties in this way will cause StyleCop to behave in the same way for all projects where StyleCop build integration is enabled.
Excluding files from analysis can be helpful in certain scenarios (such as in legacy projects, or when you add third-party Mono.Options
files, for example). To do such a thing, you need to open your project file and change the compile node of the file:
<Compile Include="File.cs"/>
Should become:
<Compile Include="File.cs"> <ExcludeFromStyleCop>true</ExcludeFromStyleCop> </Compile>