Skip to content

Getting started with the Sonar activity

rfennell edited this page Aug 3, 2014 · 3 revisions

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

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 !

Clone this wiki locally