Use sonar to manage code quality (I)-Brief Introduction and Installation

Source: Internet
Author: User


I. Brief Introduction

Sonar is an open-source platform for managing the quality of Java source code. From sonar 1.6, sonar has changed from a quality data reporting tool to the current code quality management platform.

Main features

1) code coverage: the unit test will show the lines of code selected.

2) Improve coding rules

3) Search encoding rules: query by name, plug-in, activation level, and category.

4) project search: query by project name.

5) comparison data: Compare the trend of any measurement in the same table.

This tutorial uses the latest sonar2.3 version. The new version is improved as follows.

1) You can activate encoding rules, back up and restore the Quality Profile multiple times.

2) You can search to activate all the rules once.

3) New Rule API.

4) add static resources to the plug-in.

5) support quality models such as ISO9126 through the meta-model.

6) Add new APIs and new findbugs rules.


System Support

Sonar must run in systems that support Java and maven. This is a prerequisite before installation:

Java Development Kit v1.5 or higher

Maven 2.0.9 +

Database Support

In addition to the default Derby database, sonar also supports the following databases:

MySQL 5.x+

Oracle 10g +

PostgreSQL 8.3 +

Ms SQL Server 2005

Browser support

Firefox 2.x, 3.x

Internet Explorer 6.x, 7.x, 8.x

Safari (Latest Version)

Opera (Latest Version)

Chrome (Latest Version)

Technical Architecture


The system functions in the following order:

1. Use MVN Sonar: sonar to inject code into sonar for analysis, and insert the analysis results to the database in XML format.

2. access the website http: // sonar to obtain the display result.

3. If you continuously run Maven build, the analysis results will be displayed iteratively and delivered to the database.

4. Sonar also provides eclipse and idea plug-ins to display analysis results.

2. One-minute installation tutorial

Download and decompress the package.
Run the following command on Windows:
Other platforms:
Bin/[OS]/sonar. Sh Console
Run the following commands in the maven2 project:
MVN clean install Sonar: sonar
Open your browser and enter http: // localhost: 9000

Enter the default username and password admin/admin in the background.

Step 3 and Step 5 complete installation tutorial

Step 1-create a database

Apache Derby is the default database installed by sonar and does not require installation. It can be well used for sonar demonstration, but in practical use, I recommend you use a database with better performance and stronger performance. Sonar supports the following databases: MySQL 5.x, Oracle 10g XE, PostgreSQL and Ms sqlserver 2005.

The first thing is to create a database for sonar. Tables and indexes are automatically created after sonar is activated. At the same time, sonar users must be given the permission to create, disable, and update objects in database tables.

Step 2-install the server

For more information about this step, see "one-minute tutorial ".

Step 3-configure the database

If you do not use the default database, you can edit CONF/sonar. properties to configure the database access permission. Comment out the Derby configuration and copy a custom modification. The following is the sonar Database Configuration template:

Sonar. JDBC. url: Database URL
Sonar. JDBC. DRIVER: Driver Class
Sonar. JDBC. User: default username: sonar
Sonar. JDBC. Password: the default password is sonar.

MySQL example:

# Sonar. JDBC. url: JDBC: Derby: /localhost: 1527/sonar; Create = true
# Sonar. JDBC. DRIVER: org. Apache. Derby. JDBC. clientdriver
# Sonar. JDBC. defaulttransactionisolation: 1
# Sonar. JDBC. validationquery: values (1)
Sonar. JDBC. url: JDBC: mysql: // localhost: 3306/sonar? Useunicode = true & characterencoding = utf8
Sonar. JDBC. validationquery: select 1

For Oracle, you must manually copy the JDBC driver class to the/extensions/jdbc-Driver/Oracle/directory. Other supported databases have provided drivers.

Step 4-start the server

Method 1-start independently

Sonar uses the default port "9000", the default context path "/", and the default network interface " ". Once activated, the sonar server can use http: // localhost: 9000. You can modify these parameters in CONF/sonar. properties. The following is an example of http: // localhost: 80/Sonar:

Sonar. Web. HOST:
Sonar. Web. Port: 80
Sonar. Web. Context:/Sonar

You can run the following script to start the sonar Server:

Linux/mac: bin/[your plateform]/sonar. sh start
Windows: bin/windows-x86-32/StartSonar. bat

Similarly you can start bin/windows-x86-32/InstallNTService. bat to register it as a Window service, and then start bin/windows-x86-32/StartSonar. bat.

Now you can access it through http: // localhost: 9000.

Method 2: deploy to Tomcat

Sonar can be packaged into a WAR and deployed to other JEE application servers. Currently, it only supports Tomcat 5.x, 6.x, and 7.x.

The packaging procedure is as follows:

Edit conf/sonar. properties and restore it to the standard format (that is, do not modify the port or the like ). Make sure that conf/wrapper. conf has not been used when deployed to the application server.
Run the script in the war/directory (run the build-war.bat in Windows ).
Deploy war/sonar. war on the application server.
Use http: // loaclhost: 8080/sonar to continue the installation.

To avoid memory overflow, increase the size of the memory stack. Set the CATALINA_OPTS environment variable before Tomcat starts:

CATALINA_OPTS = "-Xms1024m-Xmx1024m-Dorg. apache. jasper. runtime. BodyContentImpl. LIMIT_BUFFER = true-XX: MaxPermSize = 256m"

For more information, see here.

Step 5-configure to Maven2

Because Sonar uses the Maven2 plug-in to analyze the source code and inject the result to the database. This is why we must set database attributes in Maven configuration. Edit the file at $ MAVEN_HOME/conf or ~ The settings. xml file under/. m2, and then add the parameter in step 3.

If the database and server are not on the same host, you must specify the server address through 'sonar. host. url.


<Id> sonar </id>
<ActiveByDefault> true </activeByDefault>
<! -Mysql->
<Sonar. JDBC. url>
JDBC: mysql: // localhost: 3306/sonar? Useunicode = true & amp; characterencoding = utf8
</Sonar. JDBC. url>
<Sonar. JDBC. Driver> com. MySQL. JDBC. Driver </sonar. JDBC. Driver>
<Sonar. JDBC. Username> sonar </sonar. JDBC. Username>
<Sonar. JDBC. Password> sonar </sonar. JDBC. Password>
<! -Remote Host->
<Sonar. Host. url> http: // MySQL Server: 1234 </sonar. Host. url>

Note: The <sonar. Host. url> attribute cannot end. Otherwise, the maven sonar plug-in will report an error where the driver class cannot be found.

Similarly, we recommend that you increase the size of the memory stack to avoid memory overflow. Set maven_opts environment variables:

Export maven_opts = "-xmx512m-XX: maxpermsize = 256m"

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.