Java Code Quality Monitoring Tool sonar installation

Source: Internet
Author: User
Tags coding standards

1. Code Quality seven Crimes

Sonar is a code quality management system, its help document opening Ming Yi, put forward the code quality of the seven crimes, summed up in place, may wish a look:

1. Bugs and Hidden Bugs (Bugs and potential Bugs)

2. Violation of code specification (Coding standards Breach)

3. Copy and paste (duplications)

4. Lack of unit testing (Lack of unitTests)

5. Poor complexity distribution (baddistribution of complexity)

6. Pasta Designs (spaghettidesign)

7. Insufficient or excessive comments (notenough or Too many Comments)

2. Install the sonar server

First look at Sonar's requirements for the installation environment, see the documentation:

Http://docs.codehaus.org/display/SONAR/Requirements

2.1. Installing the JRE is no longer detailed. 2.2. Install the database

Sonar supports a variety of databases such as MySQL, Oracle, PostgreSQL, SQL Server, and MySQL.

Reference Document: Http://docs.codehaus.org/display/SONAR/Installing

2.3. Configure the database

Sonar requires a specific user name and database in the database before booting. The following is the MySQL configuration script.

# Https://github.com/SonarSource/sonar-examples/tree/master/scripts/database/mysql

# Create SonarQubedatabase and user.

#

# command:mysql-u root-p< create_database. SQL

#

CREATE DATABASE sonar CHARACTER SET UTF8COLLATE utf8_general_ci;

CREATE USER ' Sonar ' identified by ' Sonar ' ;

GRANT All on sonar. * to ' Sonar ' @ '% ' identified by ' Sonar ' ;

GRANT All on sonar. * to ' Sonar ' @ ' localhost ' identified by ' Sonar ' ;

FLUSH privileges;


2.4. Download and configure Sonar

This article downloads the Sonarqube 3.7.4 LTS and does not need to download other tools.

Modify the Sonar.jdbc.url in the Sonar-3.7.4/conf/sonar.properties file, Sonar.jdbc.username and Sonar.jdbc.password properties, specifically refer to the previous MySQL server configuration.

The values set in this article are:

Sonar.jdbc.username:sonar

Sonar.jdbc.password:sonar

Sonar.jdbc.url:jdbc:mysql://localhost:3306/sonar?useunicode=true&amp;characterencoding=utf8

The sonar.properties configuration file can also set other items, such as an HTTP listening port, to temporarily leave the default.

2.5. Start the sonar server

Execute the script for the specified operating system under the sonar-3.7.4/bin/directory to start sonar.

After Sonar starts:

(1) The MySQL server will be connected, and the relevant tables can be established in the sonar database;

(2) Start an HTTP server, the listening port is 9000, of course, this port number is configurable.

Of course, sonar can also be installed as a service to the operating system, which is no longer described in this article.

2.6. Log in to the server

The sonar server can be configured, installed, and so on by the browser.

The URL address is http://{sonar_server_ip}:9000 by default.

The default user name and password are admin/admin.

This article keeps sonar's default configuration.

3. Use MAVEN to submit analysis tasks to the sonar server

There are several ways to submit analysis tasks to sonar, and this article leverages maven. Note Sonar 3.7.4 only supports the maven3.1 version.

3.1. Modify the Maven settings.xml file.

Reference Document: Http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Maven

<profile>

<properties>

<sonar.jdbc.url>jdbc:mysql://192.168.150.11:3306/sonar?useunicode=true&amp;characterencoding=utf8 </sonar.jdbc.url>

<sonar.jdbc.username>sonar</sonar.jdbc.username>

<sonar.jdbc.password>sonar</sonar.jdbc.password>

<sonar.host.url>http://192.168.150.11:9000</sonar.host.url>

</properties>

</profile>

</profiles>

3.2. Modify the Pom.xml file to increase the MySQL driver

<build>

<extensions>

<extension>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.24</version>

</extension>

</extensions>

</build>

3.3. Submit an Analysis task

Reference Document: Http://docs.codehaus.org/display/SONAR/Analyzing+with+Maven

Execute the following MAVEN command:

MVN clean Install

MVN Sonar:sonar

Note Sonar:sonar must be executed separately. If you mix sonar:sonar with other target, such as mvn clean install Sonar:sonar, it can cause unexpected problems.

3.4. View Analysis Results

Log in to the sonar server via the browser to view the analysis results.

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: info-contact@alibabacloud.com 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.