Historically, .Net projects have used technologies from within Microsoft stack to achieve their goals and objectives. With the strong emergence of excellent Open Source tools and the phenomenal growth of OpenStack, the trend is changing. Projects that use Microsoft Technologies are now seen building affinity towards open source tools for continuous integration, development and improvement. However, not many sites/tutorials are available, that helps a project in Microsoft Technology stack to leverage open source tools.
SonarQube is an open platform to manage code quality. This article guides a user on how to setup SonarQube in Jenkins server and simplify the adoption of the tool.
Installation of Sonarqube
1. Download SonarQube from http://www.sonarqube.org/downloads/
2. Unzip the SonarQube distribution C:\sonarqube
3. Start the SonarQube server:
C:\sonarqube\bin\windows-x86-xx\StartSonar.bat
4. Download SonarQube Runner from http://www.sonarqube.org/downloads/
5. Unzip the SonarQube Runner
C:\sonar-runner
6. Download examples
7. Unzip some project samples C:\sonar-examples
8. Analyze Project
cd C:\sonar-examples\projects\languages\java\sonar-runner\java-sonar-runner-simple
C:\sonar-runner\bin\sonar-runner.bat
9. Browse results at http://localhost:9000
10. Default system administrator credentials are admin/admin
Install .Net Plugins for Sonar:
1. Download c# Ecosystem plugin
2. Extract and copy the plugins to C:\sonarqube\extensions\plugins
3. Start SonarQube
4. Verify that you are able to browse http:// localhost:9000 and login
5. Login to the SonarQube website as an Admin user and
6. Click the “Settings” link in the upper right-hand corner,
7. Go to Configuration
8. Set the appropriate path for Galileo, FxCop, StyleCop and any other plugin that you want to invoke.
9. Install Win7 SDK if you are analyzing projects .net framework 2.0 /4.0; if the target framework is 4.5, install Win 8 SDK.
10. Download command-line tool from Resharper at: http://www.jetbrains.com/resharper/features/command-line.html
11. Unzip it to a folder C:\jetbrains-commandline-tools
12. Go to update center -> Available plugins
13. Choose .Net Resharper
14. Restart the SonarQube server
15. Login to SonarQube using admin-admin
Modify Your Project’s Quality Profile
1. Activate all 650 Resharper rules
Go to Quality Profiles -> C# -> Sonar way
2. Run Sonar runner from solution file to ensure Sonar is working correctly.
Modify the port where Sonar is running
1. Change the sonar-properties files to have the port as 9010
sonar.web.host: 0.0.0.0
sonar.web.port: 9010
sonar.web.context: /
2. Change the wrapper.conf and add following line
wrapper.java.additional.3=-Djava.io.tmpdir=../../temp/
3. Run Sonar as a Windows Service
%SONARQUBE_HOME%/bin/windows-x86-32/InstallNTService.bat
4. Start Sonar service
%SONARQUBE_HOME%/bin/windows-x86-32/StartNTService.bat
5. Change the Sonar runner properties file
sonar.host.url=http://localhost:9010
Install SonarQube Plugin on Jenkins
1. Go to ‘Jenkins’ – ‘Manage Plugins’
2. Go to ‘Available tab’
3. Select ‘Sonar plugin’
4. Choose ‘install without restart’
5. Go to ‘Manage Jenkins’ and verify if ‘Sonar’ is listed in the installed plugin list.
6. You can verify your installed list.
7. Now go to http://jenkins IP/manage page
8. Go to ‘Configure System’:
9. Go to ‘Sonar configuration section’ and click on ‘Add Sonar:’
10. Go to ‘Configure sonar-runner’
11. Go to the ‘Build section’, click on ‘Add build step’ and choose ‘Invoke Standalone Sonar Analysis’: http://<IP>:<port>/job/<jobName>/configure
12. Add Invoke Standalone Sonar analysis
13. Configure the SonarQube analysis. You can either point to an existing sonar-project.properties-1 file or set the analysis properties directly in the Project properties field:
Note : Following is a sample Sonar Properties file:
# Project identification
sonar.projectKey=HM:Sonar.TestApplication
sonar.projectVersion=1.0
sonar.projectName= Sonar.TestApplication
# C# Specific Properties
sonar.sources=.
sonar.language=cs
sonar.dotnet.key.generation.strategy=safe
sonar.sourceEncoding=UTF-8
sonar.dotnet.assemblies=/trunk/dotnet/vstudio/Sonar.TestApplication.Projects/Sonar.TestApplication.Data \bin\Debug\$(AssemblyName)\$(OutputType)
# Plugin Specific Properties
sonar.resharper.mode=reuseReport
sonar.resharper.report.path=resharper-results.xml
sonar.resharper.mode=
sonar.resharper.installDirectory=c:/jetbrains-commandline-tools
# Gallio / Unit Tests
sonar.gallio.mode=
sonar.gallio.coverage.tool=OpenCover
sonar.gallio.runner=Local
sonar.dotnet.visualstudio.testProjectPattern=*UnitTest*;Testing*
sonar.opencover.installDirectory=C:/Program Files (x86)/OpenCover/
# FXCop
sonar.fxcop.mode=
# StyleCop
sonar.stylecop.mode=
14. Now the Sonar is setup
15. You can trigger Sonar either from Jenkins or from command prompt.
1Sonar project properties: A Sonar project file is the metadata that helps the sonar-runner to identify projects in the solution and run analysis against the solution. The Sonar runner file should be placed in the same folder that contains the c# solution. The Sonar runner file has three sections:
a. Project Properties
b. C# Specific Properties
c. Plugin Specific Properties