Sonarqube Code Quality Management platform installation and use

Source: Internet
Author: User
Tags utf 8 checkstyle

About Sonar

Sonar is an open source platform for code Quality management that manages the quality of your source code, and can detect code quality from seven dimensions

In the form of plug-in, you can support code quality management and testing of more than 20 programming languages including Java,c#,c/c++,pl/sql,cobol,javascrip,groovy and so on.

what can Sonarqube bring?

Developers ' Seven Deadly Sins
1. Poor complexity distribution
Files, classes, methods, and so on, if the complexity is too high will be difficult to change, which makes it difficult for developers to understand them,

And without automated unit testing, changes to any component in the program can lead to a need for comprehensive regression testing

2. Repeat
Obviously, the code that contains a lot of copy-and-paste in the program is poor quality

Sonar can show where the source code repeats serious.

3. Lack of unit testing

Sonar makes it easy to count and present unit test coverage

4. No Code standards
Sonar can be written by Code Rule Detection Tool specification code, such as Pmd,checkstyle,findbugs
5. Not enough or too many comments
No comments will make the code less readable, especially when people change inevitably, and the readability of the program will drop dramatically.
Too many comments can cause developers to spend too much time reading comments and violating their intentions.
6. Potential bugs

Sonar can detect potential bugs through the Code rule Detection Tool, such as Pmd,checkstyle,findbugs

7. Bad design (original spaghetti design, spaghetti style)
Sonar lets you find loops that show the interdependencies between packages and packages, classes, and classes
Can detect custom schema rules
Use sonar to manage third-party jar packages
Use LCOM4 to detect the application of individual task rules
Detecting coupling

About Spaghetti design:

Sonar can effectively detect the above seven problems in the program development process

Sonarqube Installation

Preset conditions
1. The Java environment is installed
2 A MySQL database is installed

Download Sonarqube and Sonarqube Runner
Chinese patch pack Download: Http://

1. Database Configuration
Enter Database command
#mysql-U root-p

mysql> CREATE USER ' sonar ' identified by ' sonar ';
Mysql> GRANT all on sonar.* to ' sonar ' @ '% ' of ' identified by ' sonar ';
Mysql> GRANT all on sonar.* to ' sonar ' @ ' localhost ' identified by ' sonar ';
mysql> FLUSH privileges;

2. Installing Sonar and Sonar-runner
Extract the downloaded package to a Linux path such as/usr/local
Unzip the downloaded packet to a path/usr/local
Add Sonar_home, Sonar_runner_home environment variables, and join Sonar_runner_home to Path

Modifying the sonar configuration file
Edit the <install_directory>/conf/ file, configure database settings, and provide support for various types of databases by default
MySQL is used here, so uncomment the MySQL module

[Java]View PlainCopy
  1. Sonar.jdbc.username:sonar
  2. Sonar.jdbc.password:sonar
  3. Sonar.jdbc.url:jdbc:mysql://localhost:3306/sonar?useunicode=true&characterencoding=utf 8&rewritebatchedstatements=true
  4. # Optional Properties
  5. Sonar.jdbc.driverClassName:com.mysql.jdbc.Driver

Modify the Sonar-runner configuration file
Switch to Sonar-runner's installation directory and modify the
Uncomment based on actual use of database

[Java]View PlainCopy
  1. #Configure Here general information on the environment, such as SonarQube DB details for example
  2. #No information about specific project should appear here
  3. #-----Default SonarQube Server
  5. #-----PostgreSQL
  6. #sonar. Jdbc.url=jdbc:postgresql://localhost/sonar
  7. #-----MySQL
  8. Sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useunicode=true&characterencoding=utf8
  9. #-----Oracle
  10. #sonar. Jdbc.url=jdbc:oracle:thin:@localhost/xe
  11. #-----Microsoft SQL Server
  12. #sonar. Jdbc.url=jdbc:jtds:sqlserver://localhost/sonar; Selectmethod=cursor
  13. #-----Global Database Settings
  14. Sonar.jdbc.username=sonar
  15. Sonar.jdbc.password=sonar
  16. #-----Default Source Code encoding
  17. sonar.sourceencoding=utf-8
  18. #-----Security (when ' sonar.forceauthentication ' are set to ' true ')
  19. Sonar.login=admin
  20. Sonar.password=admin

3. Adding a database driver
In addition to the Oracle database, other database drivers are already provided by default, and these added drivers are the only supported by sonar, so there is no need to modify
If it is an Oracle database, you need to replicate the JDBC driver to the <install_directory>/extensions/jdbc-driver/oracle directory

4. Start the service
Directory switch to Sonar's <install_directory>/bin/linux-x86-64/directory to start the service
#./ Start Service
#./ Stop Stop Service
#./ Restart Restart Service

So far, sonar is installed.
Visit http:\\localhost:9000 to

5.sonar Chinese Patch Pack installation
Chinese pack installation
Installing the Chinese patch Pack can be installed by accessing http:\\localhost:9000, after opening sonar, into the update center
Or download the Chinese patch pack, put it in the Sonarqube_home/extensions/plugins directory, and then restart the Sonarqube service

Sonar as a Linux service and boot from
To create a new file/etc/init.d/sonar, enter the following:

[Java]View PlainCopy
  1. #!/bin/sh
  2. #
  3. # rc file for SonarQube
  4. #
  5. # chkconfig: 345
  6. # Description:sonarqube System (
  7. #
  8. # # # BEGIN INIT INFO
  9. # Provides:sonar
  10. # Required-start: $network
  11. # Required-stop: $network
  12. # Default-start: 3 4 5
  13. # Default-stop: 0 1 2 6
  14. # Short-description:sonarqube System (
  15. # Description:sonarqube System (
  16. # # # END INIT INFO
  17. /usr/bin/sonar $*

Sonarqube boot (Ubuntu, 32-bit):

sudo ln-s $SONAR _home/bin/linux-x86-32/
sudo chmod 755/etc/init.d/sonar
sudo update-rc.d sonar defaults

Sonarqube Boot from boot (RedHat, CentOS, 64-bit):

sudo ln-s $SONAR _home/bin/linux-x86-64/
sudo chmod 755/etc/init.d/sonar
sudo chkconfig--add sonar

Using Sonarqube Runner to analyze source code

Preset conditions
Sonarqube Runner is installed and the environment variable is configured, that is, the Sonar-runner command can be executed in any directory

1. Create a profile in the root directory of the project source code
Take the Android project as an example:

[Java]View PlainCopy
    1. Sonar.projectkey=android-sonarqube-runner
    2. Sonar.projectname=simple Android Project Analyzed with the SonarQube Runner
    3. sonar.projectversion=1.0
    4. Sonar.sources=src
    5. Sonar.binaries=bin/classes
    6. Sonar.language=java
    7. sonar.sourceencoding=utf-8
    8. Sonar.profile=android Lint

Note: To use Android Lint
Rule analysis requires access to the HTTP:\\LOCALHOST:9000 Update Center to add the Android lint plugin so that it can analyze Android lint rules

2. Performing the analysis
Switch to the project source root directory, execute the command
# Sonar-runner
Access http:\\localhost:9000 to view analysis results after successful analysis

The meaning of the different parameters:
Sample source code analysis for different projects download:

associating with the IDE

Finally, of course, it has to be associated with the IDE to make it easier to see in real time

Take Eclipse as an example, see: Http://


Sonarqube website Address:
Sonarqube Official document Address: Http://
Sonarqube Example Address:

Two other related articles on the Web:

Sonarqube Code Quality Management platform installation and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.