Skip to content
  • Services
    Our Approach
    Personalized, in-depth technical guidance on a global scale that helps organizations achieve their digital transformation goals.
    Learn more
    • Our Approach
    • Development
    • Design
    • Digital Experience Platform
    • Data & Analytics
    • Cloud & DevOps
    • Support
  • Work
    Our Work
    Through our expertise in strategy, design, and engineering, we help clients deliver digital transformation at scale.
    Learn more
    • Our Work
    • Healthcare
    • Finance
    • Manufacturing
    • Agriculture
    • Education
  • About
    About us
    For over 20 years, we’ve partnered with companies of all sizes and industries to solve their most complex business problems.
    Learn more
    • About us
    • Leadership
    • Locations
    • Events
    • News
  • Careers
    Join our Team
    Take your career to the next level. We offer exciting opportunities across every stage of the software development life cycle.
    Learn more
    • Join our Team
    • Open Positions
    • Application Process
    • Benefits
    • Learning & Development
  • Insights
    Our Insights
    Read our latest blogs, watch our recent videos, and browse our library of e-books — all full of insights from our experts.
    Learn more
    • Our Insights
    • Blog
    • Videos
    • Downloads
  • Contact
Menu

Jenkins CI in MVC.NET Part 3: Analysis and Report

Here we show how to setup Jenkins CI Job that runs code analyze tools like StyleCop and FxCop for .NET.

Annika Hey

Annika Hey

Design Principal

Atanas Atanasov

Atanas Atanasov

Software Development Manager – Agile Frameworks

Björn Stansvik

Founder & Chief Executive Officer

Daniela Nazim

Daniela Nazim

MentorMate Alumni

Dimitar Dobrev

Dimitar Dobrev

MentorMate Alumni

Craig Knighton

Chief Operating Officer

Eleonora Georgieva

Global VP, Delivery

Georgi Dormishev

George Dormishev

System Administration Manager

Ivaylo Kostadinov

Director, Software Engineering - .NET

Jamie Bolseth profile picture

Jamie Bolseth

MentorMate Alumni

Jay Miller

President

Jeni Kyuchukova

Director, Quality Engineering

Jessica Anderson

VP of Finance and Administration

Liz Spolyar

Global Director, Continuation Engineering

Nick Curran

Nick Curran

Technical Architect

Nikolay Lyubchev

Global Director, Talent Acquisition, MentorMate

Stefan Tsvyatkov

Director, Software Engineering - Mobile

Stefan Tzanev

Chief Financial Officer

Vesselin Dobrev

Vesselin Dobrev

General Manager (Bulgaria)

Sylvia Vassileva

Sylvia Vassileva

Software Development Manager - Spok

Filip Gajtanovski

Software Development Manager - Storyworks

Krasimir K. Nikolov

VP of Technology

Katherine Kelly

Director of Operations (USA)

Carrie Siewert

Strategic Account Manager

Brady Swanson

Global Director, Marketing

Eve Poeschl

MentorMate Alumni

Ryan Peña

MentorMate Alumni

Vassil Vassilev

Software Development Manager - .NET

Pavel Petrov

Director, Software Engineering - LAMP&FE

Ivan Peev

Senior Technology Manager

Bob Reuss

MentorMate Alumni

Vera Kasapova

QA Manager

Greta Yamacheva

QA Manager

Robert Samuelsson

General Manager (Sweden)

Kyle Simmons

Solutions Architect

Robin Thomas

Solutions Architect

Nataliya Naydenova

MentorMate Alumni

Adam Malone

Alexander Dimitrov

Enterprise Architect

Andrea Kates

CEO, LaunchPad Central

Andrew Eklund

CEO, Ciceron

Andrew Marinov

Angel Nikolov

MentorMate Alumni

Anurag Shukla

Aron Wolde

MentorMate Alumni

Ashley Goodridge

Office Assistant

Benjamin Gramlich

MentorMate Alumni

Chris Black

MentorMate Alumni

Christa Haeg

MentorMate Alumni

Colin Lee

MentorMate Alumni

Deyan Stoynov

MentorMate Alumni

Dimitar Danailov

MentorMate Alumni

Dobrinka Tabakova

Doug Leatherman

Emily Genco

MentorMate Alumni

Fanka Vassileva

Gabriela Zagarova

MentorMate Alumni

Gary Conkright

CEO, physIQ

Gary Fingerhut

Executive Director, Cleveland Clinic Innovations

Gavin Finden

MentorMate Alumni

Georgi

Graham Klang

Hyusein Hyuseinov

Senior Automation QA

Ian Good

Global VP, Operations

Iva

Jack Cosentino

James Williams

John Byrne

Kaloyan Stoilkov

MentorMate Alumni

Kosta Hristov

Krasimir Gatev

Senior Android Developer

Lazar Petrakiev

Lyubomir Dobrev

Senior .NET Developer

Lubomir Velkov

Marin Yotovski

Mark Smith

MentorMate Alumni

Martin Dimitrov

MentorMate Alumni

Martin Kalyonski

Mike Hagan

MentorMate Alumni

Nikolay Andonov

Nikolay Arhangelov

Riley Panko

Guest Contributor

Roger Ferguson

MentorMate Alumni

Ryan Sysko

Chairman, WellDoc

Ryan Blake

MentorMate Alumnus

Sarah Rockholt

MentorMate Alumni

Sean McDevitt

CEO, Sensei

Siyana Slavova

Stanislas Walden

MentorMate Alumni

Stanislav Atanasov

Stanislava Bogdanova

MentorMate Alumni

Stefanie Trimble

MentorMate Alumnus

Stephen Fluin

Stoyan Stoyanov

MentorMate Alumnus

Tessa Cacek

Staffing Manager

Tom Clemens

MentorMate Alumnus

V8 JavaScript Engine

Viktor Mitev

Yolanda Petkova

Marketing Design Lead

Pete Anderson

Lead Product Owner, Target

MentorMate Software Development Lead Vasil Nonchev

Vasil Nonchev

Java Software Development Manager

Dilyana Totseva

QA Manager

Stanimir Nikolov profile picture

Stanimir Nikolov

Software Development Lead - iOS, MentorMate

Rosen Kolev

Technology Principal

Dimitar Mihaylov

MentorMate Alumni

Nikola Genov

Software Architect - .NET

Neli Todorova

Software Development Manager - LAMP

Yavor Dimitrov

MentorMate Alumni

Georgi Karanedyalkov

Software Development Lead - Android, MentorMate

Denislav Ganchev

Technology Principal

Stefan Shopov

QA Manager

Konstantin Rusev

Java Developer

Borislav Dimitrov profile picture

Borislav Dimitrov

Senior Android Developer, MentorMate

Tsvetelina Lazarova

MentorMate Alumni

Dimitar Gadzhev

Developer

Plamen Stoev

Software Development Manager - Front-end

Jake Nelsen

Senior Experience Designer

Zlati Pehlivanov

Zlati Pehlivanov

Senior Software Engineer II

Kate Tolmie, MentorMate Senior UX Designer

Kate Tolmie

Senior Experience Designer

Martin Angelov

Director, Software Engineering - LAMP&FE, MentorMate

Dimitar Zhelev

Senior .NET Developer

Joel Swenson, MentorMate Content Writer

Joel Swenson

Content Manager

Kiril Ivanov

Quality Assurance Analyst

Viktor Hristoskov profile picture

Viktor Hristoskov

Software Development Lead - iOS, MentorMate

Violeta Nikolcheva

Database Developer

Biliana Kadakevlieva

Senior Quality Assurance Analyst

Chris McLeod

Senior Solutions Consultant

Antonii Georgiev

Junior .NET Developer

Alexander Rusev

Front-End Developer

Matt Erickson, MentorMate PR and Social Media Manager

Matt Erickson

MentorMate Alumni

Brian Buchkosky

Global Director, PMO

David Tran, MentorMate VP of Solutions

David Tran

MentorMate Alumni

Kristin Krueger

MentorMate Alumni

Magdalena Chervenkova

Business Analyst

Denny Royal

Chief Design Officer

MentorMate Technical Account Strategist Joe Bodell

Joe Bodell

MentorMate Alumni

Viktoriya Chuchumisheva - MentorMate HR Manager

Viktoria Chuchumisheva

HR Manager

Kalina Tekelieva Headshot

Kalina Tekelieva

Senior Content Marketing Artist

Daniel Rankov profile picture

Daniel Rankov

MentorMate Alumni

MentorMate Senior Business Analyst Alexander Alexandrov

Alexander Alexandrov

BA Lead

MentorMate

Clint Rowles

VP, Business Development

Nikola Donev - SysAdmin

Nikola Donev

SysOps & DevOps Lead

Tseko Tsolov

Frontend Developer

Denislav Lefterov

Automation QA Analyst

MentorMate Content Writer Dilyana Kodjamanova

Dilyana Kodjamanova

MentorMate Alumni

MentorMate Project Manager Emma Jorstad

Emma Jorstad

Project Manager, Lead

Georgi Georgiev profile picture

Georgi Georgiev

Software Development Lead - LAMP, MentorMate

Martin Panayotov profile picture

Martin Panayotov

Senior iOS Developer, MentorMate

John Blake

John Blake

Senior Account Manager

Tyler Compton

Tyler Compton

Solutions Architect

MentorMate Software Developer Nikola Peevsk

Nikola Peevski

Software Developer — Lamp & Front-End

Aaron Whitney

Director of Client Strategy

MentorMate Senior Cloud Engineer Veliko Ivanov

Veliko Ivanov

Senior Cloud Engineer

MentorMate Senior Project Manager Suzanne O'Brien

Suzanne O’Brien

Senior Project Manager

Svetlin Stanchev profile picture

Svetlin Stanchev

Software Development Lead - Front-end, MentorMate

MentorMate Senior Cloud Engineer Todor Todorov

Todor Todorov

Senior Cloud Engineer

MentorMate Senior QA Analyst Kate Stamatova

Kate Stamatova

Senior QA Analyst

Frank Anselmo profile pic

Frank Anselmo

Global Director, Project Management

Gyuner Zeki Headshot

Gyuner Zeki

Solutions Architect

Galin Stanchev

QA Analyst

Sarah Hoops

Business Development Manager

Brenden Diehl

Business Development Manager

Anna Krivova profile picture

Anna Krivova

Software Development Lead - Front-end, MentorMate

Ivelina Kavalova profile picture

Ivelina Kavalova

Senior Business Analyst, MentorMate

Paul Sanders

MentorMate Alumni

Jim Cikanek

Senior Client Strategist

Samuil Yanovski profile picture

Samuil Yanovski

Software Development Manager - Android, MentorMate

Krasimir Gatev profile picture

Krasimir Gatev

Senior Android Developer, MentorMate

Kristina Goryalova headshot

Kristina Goryalova

Talent Acquisition Manager

Elena Petrova Headshot

Elena Petrova

HR Specialist

Jay Matre

Senior Business Architect, MentorMate

Lilyana Dimitrova

QA Specialist

Josh Marquart

Chief Strategy Officer

Mario Gorki

Senior Mobile Developer

Simeon Zhekov Headshot

Simeon Zhekov

Cloud Engineer

Hristo Stoyanov Headshot

Hristo Stoyanov

Cloud & DevOps Lead

Ben Wallace

Enterprise Architect

Boyan Stoyanov

Data & Dota Specialist

MentorMate Director of Software Engineering Petya Ivanova

Petya Ivanova

Director, Software Engineering - Java

Sebastian Ortiz-Chamorro

VP of Engineering, Latin America

Consuelo Merino profile pic

Consuelo Merino

Director of Operations

In this article we will show how to setup Jenkins CI Job that run code analyze tools like StyleCop and FxCop for general .NET coding practices, CPD for code duplication and JSHint for JavaScript coding practices. This project analyze tools help for the estimate of the overall project quality, security and portability. Help estimate the effort needed to put a project back on track or see of the project is started following a bad practices in early stage before going too far off tack. This can save company money and developers headache.
This is part of the series for Jenkins CI with the previous posts here and here, but code analyze tools can be run separate in a dedicated job if you wont to.
The setup project that can be used to test this setup can be found here.

The tools

I will provide a short description of the tools used for analyze and report tasks.

  • FxCop – Microsoft tool that evaluate build assemblies for some coding practices defined by a rulesets.
  • StyleCop – This tool uses predefined sets of rules to evaluate the styling of the code documents and identify bad practices.
  • StyleCop+ – Add some extra rules to StyleCop.
  • JSHint – Fork of the JSLint framework that analyze JS files for some bad client script practises (rules can be predefined).
  • CPD – Java tool that can analyze code fails for CPD (copy-paste detection). The goal is to fallow the do-not-repeat-yourself principal.
  • JRE – Java runtime environment is used to run the PDM – CPD plugin. Jenkins CI include JRE, also.
  • PhantomJS – Headless browser to run the JSHint against.
  • Violations plugin – Jenkins CI plugin for visualizing analysis information.
  • Email ext – Extended email notifications

Setup a project in Visual Studio

StyleCop

StyleCop is run as a separate target in any Visual Studio project. In order to run a code analysis in Visual Studio you need to do the following steps:

  1. Download the StyleCop and StyleCop+ libraries. You can do this in two ways.
    • Download from the official sites the StyleCop and StyleCop+ and copy the files in single directory.
    • Download them from here.
  2. Copy the StyleCop folder (with StyleCop and StyleCop+ files) to your project folder at “MY_SOLUTION_FOLDER/Tools/StyleCop”.
  3. Edit the StyleCop.ruleset to fit your project preferences.
  4. Edit all your solution .csproject to include the following import:
    <Import Project="..ToolsStyleCopStyleCop.Targets" />
  5. Open Visual Studio and build to see the result.

Note: Running 2 instances of Visual Studio with different StyleCop rulesets can mixes the evaluation results.

FxCop

Visual Studio 2013 and above has integrated solution for running FxCop rules. You can find it in the Analyze menu.
In order to setup a FxCop plan you need to go to ‘Analyze -> Configure code analysis for solution’. There you can select from a Microsoft provided list of rule sets.
Generally you would like to setup your own set of rules and use that. The way you do this is as follow:

  • Copying some of the Microsoft ruleset (possible location is “root:Program Files (x86)Microsoft Visual Studio XX.XTeam ToolsStatic Analysis ToolsRule Sets”) and modifying it or download a predefined rulesets like this one.
  • Open the .ruleset file with text editor and change the name of the ruleset <RuleSet Name=”{MyRuleSetName}” …>.
  • Place it in “MY_SOLUTION_FOLDERTools” and open it in the same Visual Studio instance your project is opened in.
  • Go to ‘Analyze -> Configure code analysis for solution’ and the your ruleset should be part of the rule sets list.
  • Select your rule set and run it by selecting ‘Analyze -> Run code analysis on solution’.
JSHint

You can run JSHint in Visual Studio by using the Web Essentials Visual Studio extension or by using SharpLinter and the instructions here.
Another option is to use post build script that that run it in PhantomJs as shown below.

Run Jenkins CI job

Build Variables

The build variables are the same as the ones in the previous article Jenkins CI in MVC.NET Part 2: Tests and Coverage. An extra variable is:

  • CPDMAXTOKENS – the maximum number of letters that will not fire CPD violation.
StyleCop

If you have followed the instructions in setup StyleCop in Visual Studio your project have already StyleCop integrated. All you need to do is build the project with MSBuild as described here and StyleCop analyze report will be created.

Setup the report file location

In order to set the report file location you need to open the StyleCop.targets file at ‘MY_SOLUTION_FOLDERToolsStyleCopStyleCop.targets’. There you will find the following section:

  <PropertyGroup Condition="'$(StyleCopOutputFile)' == ''">
    <StyleCopOutputFile>$(SolutionDir)Reports$(ProjectName).StyleCopViolations.xml</StyleCopOutputFile>
  </PropertyGroup>

This setup will output a StyleCop violations report in the Reports folder under the WORKSPACE (Solution) folder. If your project is called MyProject.Web then the path will be ‘%WORKSPACE%ReportsMyProject.Web.StyleCopViolations.xml’.

FxCop

In order to create a FxCop analyze report file we need the FxCop command line tool. This can be downloaded from the Microsoft download page here or here (for both options you need to install Microsoft Windows SDK for Windows 7 and .NET Framework 4) or if you have Visual Studio installed use if from there (located in the ‘Team ToolsStatic Analysis Tools’ folder).
If we assume you have FxCopCmd installed in “C:ToolsFxCop” folder we can run the analysis like this:

REM 'Start FxCop to check for general coding problems.'
"C:ToolsFxCopFxCopCmd.exe" /out:ReportsFxCopViolations.xml /ruleset:=ToolsDotNetCodingStandards.ruleset /file:%WEBPROJECT%/bin/%ASSEMBLIES%.*.dll /gac /ignoregeneratedcode

After this a FxCop violations report will be generated in the Reports folder.
Note: The script assum the FxCop .ruleset file is called DotNetCodingStandards.ruleset. You must change it or name the .ruleset file like this.

JSHint

In order to run JSHint we will use PhantomJS. I have tried to use all kind of solutions to run JSHint in Jenkins and have found it difficult to do this with standard tools. So what we will do is use the PhantomJS API to run JSHint directly and output the result in xml report file. For this purpose I have written a JavaScript file that uses the phantomJs API to load the JSHint.js and the js files in a given folder recursively and check them against a set of rules.
You can find all that here. Just download it and place it inside “C:ToolsJSHint” folder and run it like this:

REM 'Start JSHint to check for JavaScripts coding problems.'
"C:ToolsJSHintphantomjs.exe" C:ToolsJSHintjshint-runner.js %WEBPROJECT%Scripts ToolsJSHintRules.json ReportsJSHintViolations.xml

  • The first argument is the jshint script to perform the JSHint check.
  • The second argument is path where all script files are located in (recursive check).
  • The third argument is the config file. In it should be all JSHint rules like ‘{ “unused” : true, “latedef” : false, “strict” : true, … }’.
  • The forth argument is the output report file (the report file is in jslint4java report format).
Copy-paste detection

For copy-paste detection we will use the PMD tool at http://pmd.sourceforge.net/. In order to use it we need 2 things:

  • JRE – Java runtime is used for running the the tool. Download it from here and install it. You don’t need the JDK only the JRE.
    Jenkins also include JRE in ‘Jenkinsjrebin’, but using it may provide compatibility problems.
  • PMD – Download the archive from the main site and extract in folder ‘C:ToolsPMD’. The required files are ‘commons-io-X.X.jar’, ‘jcommander-X.X.jar’, ‘pmd-core-X.X.X.jar’, ‘pmd-cs-X.X.X.jar’, ‘pmd-javascript-X.X.X’.
  • Make sure the java.exe path is in the system environment path.
  • Make a file called fix.bat in the “c:ToolsPMD” with the following text:
    @ECHO OFF
    for /F "delims=" %%A in ('dir /b /s %1') do (
    	IF %%~zA equ 0 (
    		echo %%A Fixed
    		echo ^<?xml version="1.0"?^>^<pmd-cpd /^> > %%A
    	) ELSE (
    		echo %%A Ok
    	)
    )

The following script will generate the CPD violations report for .js and .cs files:

REM 'Check for CPD in C# files'
java.exe -Djava.ext.dirs="C:ToolsPMD" net.sourceforge.pmd.cpd.CPD --minimum-tokens %CPDMAXTOKENS% --files * --exclude packages/ --language cs --encoding windows-1252 --format xml > ReportsCSCPDViolations.xml

REM 'Check for CPD in JavaScript files'
java.exe -Djava.ext.dirs="C:ToolsPMD" net.sourceforge.pmd.cpd.CPD --minimum-tokens %CPDMAXTOKENS% --files * --exclude packages/ --language ecmascript --encoding windows-1252 --format xml > ReportsJSCPDViolations.xml

REM 'Check if CPD report files are empty'
call c:ToolsPMDfix.bat Reports*CPDViolations.xml

Other

We can also include the HTML 5 validator that can be run with JRE and do some nice checks of your HTML. However it can not check templates, like Razor.
Another tool is the Css Lint, that check your css files and can be runned with JRE and Rhino.

Visualization

For visualization of the analysis we will use the Violations plugin. The plugin can read the results for JSLint, FxCop, StyleCop and other reports and generate nice graphics for them.
Note: Maven Integration plugin is required with the Violations plugin, but is not automatically installed.
In the Post-Action select ‘Add post-build action -> Report Violations’ and config it like this:

cpd: Reports*CPDViolations.xml
fxcop: Reports*CPDViolations.xml
jslint: ReportsJSHintViolations.xml
stylecop: Reports*.StyleCopViolations.xml

Report

In order to provide this information to the developers we can use Extended Email Plugin for Jenkins. The plugin can be setup to use different email templates, according to your needs. The plugin have default templates. You can download additional ones or create one on your own.
In the template you can include Unit test reports, analysis results, system variables, etc.
In order to use the email-ext select ‘Add post-build action -> Editable Email Notification’. Setup can be:

Project Recipient List: List of email addresses separated by comma (ex: [email protected], [email protected]).
Project Reply-To List: Reply to addresses as the recipients.
Content Type: Html or plain text email type.
Default Content: One of the predefined templates like ‘${SCRIPT, template=”groovy-html.template”}’ or your custom template.
Attach Build Log: You can specify if you want to attach the Jenkins build log to the email.
Advance Options -> Triggers: Specify what triggers the notification.

In order to be able to send email you need to configure in the Jenkins configuration page -> section ‘E-mail Notification’.

Executed Command

The final script for the analysis should be as follow:

Before MSBuild

REM 'Clean Up'
rd /S /Q TestResults
rd /S /Q Reports
md TestResultsHTMLJSReport
md Reports

After MSBuild

REM 'Start FxCop to check for general coding problems.'
"C:ToolsFxCopFxCopCmd.exe" /out:ReportsFxCopViolations.xml /ruleset:=ToolsDotNetCodingStandards.ruleset /file:%WEBPROJECT%/bin/%ASSEMBLIES%.*.dll /gac /ignoregeneratedcode

REM 'Start JSHint to check for JavaScripts coding problems.'
"C:ToolsJSHintphantomjs.exe" C:ToolsJSHintjshint-runner.js %WEBPROJECT%Scriptsapp ToolsJSHintRules.json ReportsJSHintViolations.xml

REM 'Check for CPD in C# files'
java.exe -Djava.ext.dirs="C:ToolsPMD" net.sourceforge.pmd.cpd.CPD --minimum-tokens %CPDMAXTOKENS% --files * --exclude packages/ --language cs --encoding windows-1252 --format xml > ReportsCSCPDViolations.xml

REM 'Check for CPD in JavaScript files'
java.exe -Djava.ext.dirs="C:ToolsPMD" net.sourceforge.pmd.cpd.CPD --minimum-tokens %CPDMAXTOKENS% --files * --exclude packages/  --language ecmascript --encoding windows-1252 --format xml > ReportsJSCPDViolations.xml

REM 'Check if CPD report files are empty'
call c:ToolsPMDfix.bat Reports*CPDViolations.xml

EXIT 0

Note that in the end we do a EXIT 0, because FxCop and some of the others tool can fail the build and this is not our intention.

References

  • Code analysis team blog
  • PhantomJs
Tags
  • Development
Share
  • Share on Facebook
  • Share on LinkedIn
  • Share on Twitter
Share
  • Share on Facebook
  • Share on LinkedIn
  • Share on Twitter
Sign up for our monthly newsletter.
Sign up for our monthly newsletter.

Read what's next.

Blog

How to Write Effective Technical Tasks and User Stories

Blog

Measuring the Business Value of Technology Investments

  • Twitter
  • LinkedIn
  • Instagram
  • Facebook
United States
MentorMate1350 Lagoon Ave, Suite 800
Minneapolis
, MN 55408

+1 612 823 4000
Bulgaria
67 Prof. Tsvetan Lazarov Blvd.
Sofia 1592, Bulgaria,
+359 2 862 2632
Sweden
Drottninggatan 29
411 14 Göteborg

+46 3 199 0180
Paraguay
Carlos M. Gimenez 4855
Asunción, Paraguay

+595 21 327 9463

Copyright © 2023 MentorMate, Inc.

  • Cookies
  • Privacy
  • Terms
  • Continuity Policy
This site is registered on wpml.org as a development site.