In my previous post I discussed about the Sonar analysis of your project with some introduction to Sonar.
Setting
up Sonar
1.) Install
Sonar Server Prerequisites which includes follows:
a) JRE
6 or 7
b) Install
Tomcat Apache Server
2.) Install
Sonar Server
3.) Install
Sonar Runner (Client)
4.) Install
C# Plugins
5.) Update
Plugin Paths if any
Like,
the default URL for FxCop plugin is C:/Program
Files /Microsoft FxCop 10.0, but if in your machine FxCop is installed at C:/Program Files x(86) /Microsoft FxCop
10.0 (64 bit machine) then update this path in Sonar Properties file of
Sonar Runner(Client)
6.) Integrate
Sonar Runner with Visual Studio
7.) Running Sonar Analysis
Installing
JRE 7
Go to this page
and download and install the JRE 7 in your machine.
Install
Tomcat Apache Server
·
Go
to this page and download 32-bit/64-bit Windows Service Installer for tomcat Apache
7. Install the set up. After installing this setup the apache server will get
installed on your machine.
·
Set the JAVA_HOME
environmental variable as follows:
JAVA_HOME=location of JRE 7 on your machine (Default: C:\Program
Files\Java\jre7)
Install Sonar Server
- Download the sonar Server. At the time of writing this document the latest version was sonar- 3.5.1.
- This will give you a .zip file. Decompress it into the location you want; I used C: / sonar-3.5.1
- In the conf/sonar.properties file , we can change some sonar server default settings like the port on which we want to install sonar server (the default port is 9000) . Let’s not change any settings as of now.
- Now install Sonar Server as Service. For this go to C:\sonar-3.5.1\bin\windows-x86-64 (64 bit machines) and run InstallNTService.bat. Configure this service to be automatic to support reboot etc.
- Sometimes you may find error to start the service. Generally the exception is java.lang.IllegalStateException: Unable to create file in temporary directory, please check existence of it and permissions: C:\Windows\system32\config\systemprofile\AppData\Local\Temp
- To remove this error run the installed service under your user account rather than the system account and create the “Temp” folder in the systemprofile\AppData\Local folder. (C:\Windows\system32\config\systemprofile\AppData\Local\Temp\)
- After this much, your sonar server is installed and you can access the sonar server by typing http://localhost:9000 in the browser.
Install
Sonar Runner (Client)
- Download the sonar runner from url (http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Runner)
- This will come as a Zip file. Decompress it in an appropriate location. For me, I put it in C:/ SONAR_RUNNER_HOME/
- Set environmental variable as follows:
- SONAR_RUNNER_HOME: C:\$SONAR_RUNNER_HOME
- In C:\$SONAR_RUNNER_HOME\conf\sonar-runner.properties file, make changes if any like to put the sonar server path. If we have not make any changes to the property file of sonar server and using default DB then there is no need to alter this file.
- Run sonar-runner –h in command prompt you will get something like as follows:
- usage: sonar-runner [options]
- Options:
- -h,--help Display help information
- -X,--debug Produce execution debug output
- -D,--define
Define property
It means that sonar client
is installed successfully.
Install
C# Plugins
You'll
need to download and install the "C# Plugins Ecosystem" (ie: The plugins to
analyze C# code). This will be a zip
file with each of the C# plugins .jar files. Decompress them and place them
into the /extensions/plugins folder
of your Sonar server installation folder.
You can enable/disable each one individually, so no harm in putting them
all in there at once. You may need to
restart the Sonar service after you place the files for them to be available.
The
plugins consist of the following:
- C# Core (support for the C# language - general API used by other plugins -- required)
- C# Squid (support for parsing C# language -- required)
- FxCop support (general analysis)
- StyleCop support (formatting analysis)
- Gendarme support (general analysis)
- Gallio support (unit testing / coverage)
- NDeps (assembly dependency parsing) (new in v1.3)
If you're going to use
FxCop, you'll need to install FxCop 10 (the FxCop installer is installed as
part of the Win7 SDK install) on the client machine as well. Give attention that the tools hould be installed on the client machine.
Update
Plugin Paths if any
You have to update the plugin installation folder path in the sonarrunner properties of the client.
eg:
You have to update the plugin installation folder path in the sonarrunner properties of the client.
eg:
The
default URL for FxCop plugin is C:/Program
Files /Microsoft FxCop 10.0, but if in your machine FxCop is installed at C:/Program Files x(86) /Microsoft FxCop
10.0 (64 bit machine) then update this path in Sonar Properties file of Sonar
Runner(Client) at location ( C:\$SONAR_RUNNER_HOME\conf\sonar-runner.properties)
Integrate
Sonar Runner with Visual Studio
·
Go to Visual Studio ->
tools->Extension manager and search for VSSonarExtension
online. Download and Install the plugin. This will integrate the Sonar with
Visual Studio.
RUN SONAR ANALYSIS
Create Sonar-Project.properties file
Create a sonar-project.Properties File in the folder where your project solution file is located.
Each Solution will need to have it's own sonar-project.properties file. This file will need to exist in the folder from which you execute the sonar-runner. To make this easy, I would suggest putting the file in the same folder as your .sln file.
The file will have a few sections, which I will describe here. Note that some of these, I believe, can be put in the sonar-runner.properties file if you have the same settings for each project.
Important Note: Any folder names in the config file will need to either escape the backslash with another backslash (\\) or use a forward slash (/). I've chosen the latter.
Project Identification:
This section will provide the project key used by the Sonar server to group analysis results over time, as well as provide a useful name in the UI, etc. This should be unique across projects. The project version can be used to track different branches, etc.
# Project identification
sonar.projectKey=Jwright:DemoApp
sonar.projectVersion=trunk
sonar.projectName=DemoApplication
Then, describe the source code layout. The "sources" field points to the top-level folder where the source code exists. If you're .sln and .csproj files have relative paths internally, then this should be the top-level folder. Assuming you don't have any strange layouts, this will likely be the same folder as your .sln file (which is likely where your .properties file exists), so can just be ".". Additionally, you need to denote that the language is C# using the "cs" value.
# Info required for Sonar
sonar.sources=.
sonar.language=cs
C#-specific settings:
Here, you'll need to provide information about where the .sln file is located and where key libraries are located, and which version of .Net you're using.
#Core C# Settings
sonar.dotnet.visualstudio.solution.file=DemoApp.sln
sonar.silverlight.4.mscorlib.location=
C:/Program Files (x86)/Reference Assemblies/Microsoft/Framework/Silverlight/v4.0
sonar.dotnet.excludeGeneratedCode=true
sonar.dotnet.4.0.sdk.directory=C:/WIndows/Microsoft.NET/Framework/v4.0.30319
sonar.dotnet.version=4.0
Plug-in Specific Sections:
For each plugin, there is a "mode" setting. If blank, then the plugin will run. If you want to skip/not run a plugin, set the mode to "skip".
For Gallio, you can stipulate if you want to use OpenCover (free) or NCover (not free). You can also stipulate the runner mode. I had trouble using anything other than "Local". You will also need to stipulate the naming pattern (regular expressions, I believe) for the Visual Studio projects that include unit tests. You can have multiple patterns, seperated by semicolons.
#Gendarme
sonar.gendarme.mode=
# 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=skip
#StyleCop
sonar.stylecop.mode=
#NDeps
sonar.ndeps.mode=
The tools for which the mode property is blank, all those tools will run if we command to run sonar analysis on that project.
Run analysis
So, now we are done with our setup and ready to run the analysis.
Go to CMD -> Go to project solution file folder and run sonar-runner. The sonar will execute and you can see the results on http://localhost:9000. We can also run the analysis by going to Sonar tab in Visual Studio.
Click here to know about the common errors which may come while running sonar analysis.
Related Posts:
sonar.projectKey=Jwright:DemoApp
ReplyDeletehttps://wrightfully.com/setting-up-sonar-analysis-for-c-projects ?