How to integrate the Sonar build activity

Install and configure Sonar

Install the sonar runner on your build server. This is way beyond this guide, you should go to the SonarQube web site for that : http://docs.codehaus.org/display/SONAR/C%23+Ecosystem
You should make it running without TFS first on a test project.

You may also have a look at the guide I wrote for configuration Sonar: http://www.vlaquest.com/2013/08/how-to-integrate-sonar-with-tfs-part-1/.

Prepare the Sonar runner for the Sonar activity

The Sonar build activity will launch the sonnar-runner from the Solution folder during the build.
Before that, the sonar.properties file necessary for the sonar-runner can be optionnally generated :
  • if a sonar.properties exists already (from the source controller), nothing is generated and the file is taken as is
  • if no sonar.properties file is found, the template is processed

Here are the variables available in the template :
  • %BUILD_DEFINITION%
  • %BUILD_DEFINITION_UNDERSCORE%
  • %BUILD_NUMBER%
  • %BUILD_NUMBER_UNDERSCORE%
  • %BUILD_NUMBER_DEFAULT_SUFFIX%
  • %SOLUTION_FILE%
  • %SOLUTION_FILE_PATH%
  • %SOLUTION_DIRECTORY_PATH%
  • %BINARIES_DIRECTORY_PATH% : the binaries folder of the TFS build (as defined in the build process template)
  • %BINARIES_DIRECTORY_PATH_SLASH% : same path with slashes instead of backslashes (for Sonar)


Here's an example of a working sonar properties template format :

# Project identification
sonar.projectKey=mycompany:%BUILD_DEFINITION_UNDERSCORE%
sonar.projectVersion=%BUILD_NUMBER_DEFAULT_SUFFIX%
sonar.projectName=%BUILD_DEFINITION%

# Info required for Sonar
sonar.sources=.
sonar.language=cs

# If multiple solutions in folder, use the following line to disambiguate
sonar.dotnet.visualstudio.solution.file=%SOLUTION_FILE%

# sonar.fxcop.mode=skip
# sonar.gendarme.mode=skip
# sonar.gallio.mode=skip
# sonar.ndeps.mode=skip
# sonar.squid.mode=skip
# sonar.stylecop.mode=skip

sonar.fxcop.installDirectory=D:/Sonar.Net/Microsoft Fxcop 10.0
sonar.fxcop.assemblyDependencyDirectories=%BINARIES_DIRECTORY_PATH_SLASH%
sonar.dotnet.visualstudio.testProjectPattern=*.Tests;*.Tests.Unit;*.Tests.Integration

sonar.dotnet.assemblies=%BINARIES_DIRECTORY_PATH_SLASH%/$(AssemblyName).$(OutputType)
sonar.dotnet.test.assemblies=%BINARIES_DIRECTORY_PATH_SLASH%/$(AssemblyName).$(OutputType)

sonar.gallio.filter=exclude Type:/\.Integration\./
sonar.gallio.it.mode=skip

sonar.gallio.installDirectory=D:/Sonar.Net/Gallio
sonar.gallio.coverage.tool=OpenCover
sonar.gallio.runner=IsolatedProcess
sonar.gallio.timeoutMinutes=10

sonar.opencover.installDirectory=D:/Sonar.Net/OpenCover

sonar.gallio.coverage.excludes=Microsoft.Practices.*

Integrate the build activity in your build template

It is useful to add a global parameter (browsable) "Run Sonar Analysis" to enable Sonar to launch or not during a build.

You can read more about how to prepare build servers and integrate the activity in your builds here: http://www.vlaquest.com/2013/09/how-to-integrate-sonar-with-tfs-part-2

Typically pass these parameters to the build activity :
  • BuildWorkspace <- Workspace
  • FailBuildOnAlert <- True (When a critical alert is raised, the activity can detect it and volountarily fail, requires the Sonar Build Breaker plugin)
  • FailBuildOnError <- False (because Sonar may not be always available, you don't want all your builds down if Sonar is down, but it's up to you)
  • GeneratePropertiesIfMissing <- if the sonar.properties file is missing, generates one from the template
  • ProjectsToAnalyze <- BuildSettings.ProjectsToBuild
  • SonarPropertiesFileName <- "sonar-project.properties"
  • SonarPropertiesTemplatePath <- "D:\Sonar.Net\sonar-properties.template"
  • SonarRunnerPath <- "D:\Sonar.Net\sonar.net-runner.cmd"
  • BinariesDirectory <- BinariesDirectory

Analysis result

This activity requires the Sonar Build Breaker plugin http://docs.codehaus.org/display/SONAR/Build+Breaker+Plugin to be installed on your Sonar server deployment in order to be able to break builds on Critical alerts.

FailBuildOnAlert (param) FailBuildOnError (param) Alerts in the build Any Sonar error
True True Failed Failed
True False Failed Partially Succeeded
False True Failed Failed
False False Partially Succeeded Partially Succeeded

Configure your build definition

You may want to disable unit tests execution now, since Sonar will do it (it's up to you if you want the coverage and test stuff in your Sonar portal).

Roadmap for this activity

Ability to pass sonar properties parameters from the build definition directly into the resulting sonar properties file.

Support

Please for problems with Sonar and TFS and .NET in general have a look at the sonar-users mailing list.
Thanks !



Last edited Oct 27, 2013 at 8:55 PM by vlabatut, version 5

Comments

honestguy Dec 3, 2013 at 4:27 AM 
BuildWorkspace <- Workspace... Is not been recognized by Sonar activity. Am I missing something. Kindly help. Regards.

vlabatut Oct 27, 2013 at 8:56 PM 
Sorry, the VS 2010 activity has been merged only recently... It is available in the VS 2010 branch now!

graydo64 Oct 14, 2013 at 3:15 PM 
...OK, so the download works in IE. Will try to build from source.

graydo64 Oct 14, 2013 at 3:10 PM 
Same here - I can see all the code quality activities - CodeMetrics, NUnit, StatLight and VSTest but there's no Sonar activity. The link to download the source is broken too...

CSJones Oct 4, 2013 at 5:09 PM 
I've downloaded the lastest version to the TFS BuildExtensions but don't see the Sonar activity in any of the .dlls inthe zip file for VS2010. Is it named something besides sonar?

Regards