Code Analysis For Java Using Sonar
Code Analysis For Java Using Sonar
USING
SONARQUBE
1
TABLE OF CONTENTS
1. INTRODUCTION ....................................................................................................................................................4
Prerequisites ......................................................................................................................................................17
2
6. Analysis using SonarQube Eclipse Plugin ...........................................................................................................19
7. References .........................................................................................................................................................30
3
1. INTRODUCTION
This document contains all essential information required to install and configure SonarQube and
execute SonarQube analysis in Maven and non-Maven projects.
1.1 SONARQUBE
SonarQube™ software (previously known as “Sonar”) is an open source project for Continuous
Inspection of code quality hosted at Codehaus. It is distributed under license LGPL v3.
Comments
Coding rules
Potential Bugs
Duplications
Unit Tests
Complexity
SonarQube can perform analysis on 25+ different languages. The outcome of this analysis will be
quality measures and issues (instances where coding rules were broken). However, what gets
analyzed will vary depending on the language:
On all languages, a static analysis of source code is performed (Java files, COBOL
programs, etc.)
A static analysis of compiled code can be performed for certain languages (.CLASS files
or jars in Java, .DLL files in C#, etc.)
A dynamic analysis of code can be performed on certain languages (execution of unit
tests in Java, C#, etc.)
4
SonarQube is also used for Android Development.
SonarQube internally uses PMD, Findbugs, CheckStyle etc.
Integration of SonarQube with standard ALM components such as Maven, Ant, SVN, Git, Mercurial,
JIRA, Mantis, Google Analytics, Piwik, Fortify etc. comes out of the box.
Java - Download and install latest version of Java if it is not already available.
SonarQube - Download latest version of SonarQube
from http://www.SonarQube.org/downloads and unzip to desired location.
SonarQube Runner - Download latest version of SonarQube runner from
http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner and
unzip to desired location.
NOTE:
SonarQube Runner is recommended as the default launcher to analyze a project with SonarQube.
5
2.2 SONARQUBE SERVER SETUP VALIDATION
1. Start the SonarQube server using the startup script available in SONAR_HOME\bin folder.
NOTE:
6
EXAMPLE : SONAR_HOME\bin\windows-x86-32\StartSonar.bat
2. After the server starts, access the web interface of the SonarQube at
http://localhost:9000/
Note:
For remote access, replace localhost with the address of the SonarQube server.
7
2.3 ENVIRONMENT VARIABLES SETUP
1. Set a new environment variable as SONAR_RUNNER_HOME. And its value should be the
unzipped path of sonar-runner zip file.
3. Update the global settings (database connection, server URL) by editing sonar-runner.properties
file under Sonar Runner’s conf folder.
<Sonar_runner_installed_directory>/conf/sonar-runner.properties
8
3. ANALYZING A NON - MAVEN PROJECT
A sonar-project.properties file has to be placed into the root of the project folder.
9
Minimum content of the sonar-project.properties file will be as follows.
# Required metadata
sonar.projectKey=java-sonar-runner-simple
sonar.projectVersion=1.0
sonar.sources=src
# Language
sonar.language=java
10
3.2 GENERATING AND VIEWING THE SONARQUBE REPORT
1. Ensure that the steps in Section 2 above are executed and the SonarQube server is started.
2. Go to command line and change directory (cd) to the project’s root directory and run the
command
[path of sonar-runner]\bin\sonar-runner.bat
3. Upon successful execution of the above command, check the SonarQube dashboard by opening
SonarQube Server’s admin page at http://localhost:9000 (default URL).
4. The project will be listed on the dashboard with the name given in sonar-project.properties file.
5. Click on the project name and explore further to view the code analysis report.
Note:
The above result is only from PMD and Checkstyle execution by SonarQube. To run code coverage as
well as Findbug plugins, the binary path is needed in the sonar-project.properties file.
11
4. ANALYZING A MAVEN PROJECT
INSTALLING MAVEN
Download and install Maven (with a version compatible with the SonarQube version installed) if not
already installed.
CONFIGURING MAVEN
1. Edit the settings.xml file located in $MAVEN_HOME/conf or ~/.m2, to set the database parameters
to be used as well as the SonarQube server URL.
Example:
<settings>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>
12
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
Note:
13
4.2 SETUP OF MAVEN PROJECT FOR SONARQUBE ANALYSIS
1. Go to the Maven project and edit pom.xml file adding the SonarQube plugin for the Maven version
(http://docs.codehaus.org/display/SONAR/Analyzing+with+Maven)
Maven3 configuration:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugins>
</pluginManagement>
</build>
2. A sonar-project.properties file has to be placed into the root of the project folder.
14
Minimum content of the sonar-project.properties file will be as follows.
# Required metadata
sonar.projectKey=java-sonar-runner-simple
sonar.projectVersion=1.0
sonar.sources=src
# Language
sonar.language=java
15
4.3 GENERATING AND VIEWING THE SONARQUBE REPORT
From the project root directory where the pom.xml file is also placed, execute the following command
giving user/password credentials:
mvn sonar:sonar
Note:
mvn sonar:sonar
1. Upon successful execution of the mvn sonar:sonar command, check the SonarQube dashboard
by opening SonarQube Server admin page http://localhost:9000 (default URL). Default
credentials – admin/admin.
2. The project will be listed on the dashboard with the name given in sonar-project.properties.
3. Click on project name and explore further to view the code analysis report.
16
5. ANALYZING WITH SONARQUBE ANT TASK
The SonarQube Ant Task allows integration of SonarQube analysis into an Apache Ant build script.
PREREQUISITES
Download Path:
http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-ant-task/2.1/sonar-ant-task-2.1.jar
build.xml
...
<!-- Define the SonarQube global properties (the most usual way is to pass these properties via the
command line) -->
<property name="sonar.jdbc.url"
value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8" />
...
17
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the SonarQube Ant
Task" />
...
<target name="sonar">
<!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
</taskdef>
<sonar:sonar />
</target>
...
Run the following command from the project base directory to launch the analysis:
ant sonar
18
VIEWING THE SONARQUBE REPORT
1. Upon successful execution of the mvn sonar:sonar command, check the SonarQube dashboard
by opening SonarQube Server admin page http://localhost:9000 (default URL). Default
credentials – admin/admin.
2. The project will be listed on the dashboard with the name given in sonar-project.properties.
3. Click on project name and explore further to view the code analysis report.
The SonarQube Eclipse Plugin provides a comprehensive integration of SonarQube in Eclipse for
Java, C/C++ and Python projects.
If a previous version of SonarQube Eclipse plugin is already installed, you can update it. Go to Help >
Check for Updates.
19
This should display the list of available plugins and components:
20
Or
21
1. Check the SonarQube components to install.
2. Click Next. Eclipse will then check to see if there is any issue which would prevent a successful
installation.
3. Click Finish to begin the installation process. Eclipse will then download and install the
necessary components.
4. Once the installation process is finished, Eclipse will ask if you want to restart the IDE. It is
strongly recommended that you restart the IDE.
22
6.2 CONFIGURING SONARQUBE ECLIPSE PLUGIN
A project should have been already created and configured with SonarQube server in order
to start analyzing it with SonarQube in Eclipse.
The SonarQube server connection should be established before attempting to run the
analysis. This can be ensured by testing the Server connection in Eclipse through the menu
option: Windows > Preferences > SonarQube > Servers.
23
If the test does not return the message “Successfully connected!”, then start the SonarQube server
and retest the connection using the same steps.
Once the SonarQube server is defined, the next step is to link your Eclipse project with a project
defined on this SonarQube server.
To do so, right-click on the project in the Project Explorer, and then choose the
option Configure > Associate with SonarQube
24
A dialog appears with the SonarQube project text field. Start typing the name of the project in the text
field against SonarQube project column and select it in the list box:
Click on Finish.
Now the project is associated to the one analyzed on the SonarQube server and the SonarQube reports
can be viewed within eclipse.
Note:
25
6.4 WORKING WITH SONARQUBE IN ECLIPSE
To run a new analysis, right click on the project and go to SonarQube > Analyze.
Note:
You can also hit Ctrl+Alt+Q wherever you are in your project to trigger a new analysis.
Four different views are available in Eclipse to browse the quality of the projects:
26
SonarQube Rule Description to get the detailed description of the rule that is violated
SonarQube Web Browser
SONARQUBE ISSUES
The SonarQube Issues view displays the list of issues of the selected component (project,
module, file, etc.).
To display this view, go to Window > Show View > Other... > SonarQube > SonarQube Issues.
Problems and Markers views also display issues.
New issues (compared to the latest version on the SonarQube server) are highlighted in yellow. This
allows you to focus on the new issues that you have introduced.
27
You can add/remove/order columns, group issues (by severity, new issues only, ...), filter issues (new
issues only, issues assigned to me, ...), sort (by assignee, ...).
Note:
This view allows you to review the selected issue (add comments, confirm it, plan it, etc.) the
same way you would do it through the web interface.
To display this view, go to Window > Show View > Other... > SonarQube > SonarQube Issue
Editor.
28
Note:
This view allows you to access the detailed description of the coding rule of the selected issue.
To display this view, go to Window > Show View > Other... > SonarQube > SonarQube Rule Description.
This view is automatically displayed when clicking on SonarQube > Open in SonarQube server.
It can also be displayed by going to Window > Show View > Other... > SonarQube > SonarQube Web
Browser.
29
7. REFERENCES
1. Compatibility matrix showing the versions compatible between SonarQube, SonarQube Ant
Task, SonarQube Runner and Maven at :
http://docs.codehaus.org/display/SONAR/Analyzing+Source+Code#AnalyzingSourceCode-
CompatibilityMatrix
2. http://docs.codehaus.org/display/SONAR/Working+with+SonarQube+in+Eclipse
30