Sonar 安裝使用推薦

SonarQube Scanner 安裝使用文檔

代碼質量相信是每一個團隊的最高追求之一,質量高的團隊,開發成本、維護成本都很低;
一樣人數的團隊,一年內高質量團隊是低質量團隊產出的10倍;打個比方,一個團隊開發完產品,1000行代碼出一個bug和100行代碼一個bug的團隊。能想象場景了。php

介紹一款代碼質量檢測工具Sonar,爲正在辛苦代碼審覈的同窗提供一點便利;官網提供了很方便的教程;這裏再作一箇中文推廣html

適合場景:一個代碼冗餘多,代碼邏輯重複多(對,你沒看錯,這裏的重複真的是重複),分格隨意項目的系統檢查,重構,架構調整;java

1、 Sonar環境介紹

sonar_platform_support.png圖片描述mysql

一般檢查代碼是項目用,因此例子安裝在阿里雲的服務器上。
教程環境介紹:linux

  • [ ] OS平臺:centos6.xweb

  • [ ] 數據庫:mysql5.6.xsql

2、下載

下載最近版本,兼容性會比較好:數據庫

  1. 下載Sonar sonarqube-6.4.zipvim

  2. 下載掃描器sonar-scanner-cli-3.0.3.778-linuxcentos

3、安裝

  1. 檢測java:

[root@xx]# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

顯示這樣就ok了;

若是java OpenJDK低於8:
官網下載 jdk-8u111-linux-x64.tar.gz

  1. 檢測mysql 5.6.x以上 :

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.13    |
+-----------+
1 row in set (0.00 sec)
mysql> CREATE DATABASE `sonar` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonarUser'@'127.0.0.1' IDENTIFIED BY 'sonarPassword';
mysql> GRANT ALL ON *.* TO 'sonarUser'@'%';
mysql> GRANT select,insert,update,delete,create,drop on *.* to sonarUser@127.0.0.1 IDENTIFIED BY 'sonarPassword';
mysql> flush privileges;
mysql> exit
[root@xx] mysql -h127.0.0.1 -usonarUser -psonarPassword

注意:安裝sonar 須要在mysql提早建庫,並配置字符編碼utf-8;給sonar建一個帳號;

/app/mysql/my.cnf 配置buffer開大點,比較你的代碼會挺多:
innodb_buffer_pool_size = 128M

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
  1. 存放目錄:/app/original/
    下載並解壓:

[root@xx] unzip sonarqube-6.4.zip unzip;
[root@xx] sonar-scanner-cli-3.0.3.778-linux.zip;
drwxr-xr-x 10 root root      4096 Jun  2 08:43 sonarqube-6.4
-rw-r--r--  1 root root 139755847 Jun 13 15:27 sonarqube-6.4.zip
drwxr-xr-x  6 root root      4096 May 12 12:49 sonar-scanner-3.0.3.778-linux
-rw-r--r--  1 root root  73799876 Jun 13 15:02 sonar-scanner-cli-3.0.3.778-linux.zip

vim sonarqube-6.4/conf/sonar.properties
sonar.properties 兩處必須配置:

配置mysql:

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonarPassword

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

配置web server:
端口號:9090:
容許ip:0.0.0.0 表示容許全部;

# Binding IP address. For servers with more than one IP address, this property specifies which
# address will be used for listening on the specified ports.
# By default, ports will be used on all IP addresses associated with the server.
sonar.web.host=0.0.0.0
# Web context. When set, it must start with forward slash (for example /sonarqube).
# The default value is root context (empty value).
#sonar.web.context=
# TCP port for incoming HTTP connections. Default value is 9000.
sonar.web.port=9090

啓動:

[root@xx] cd /app/original/sonarqube-6.4/
[root@xx]  ./bin/linux-x86-64/sonar.sh start
[root@xx] ps aux | grep sonar

若是沒起來檢查log

[root@xx]cd /app/original/sonarqube-6.4/logs
[root@xx]vim web.log;
2017.06.13 17:08:04 INFO  web[][o.s.s.p.w.MasterServletFilter] Initializing servlet filter org.sonar.server.authentication.ws.LogoutAction@96ee351 [pattern=UrlPattern{inclusions=[/api/authentication/logout], exclusions=[]}]
2017.06.13 17:08:04 INFO  web[][o.s.s.p.w.MasterServletFilter] Initializing servlet filter org.sonar.server.authentication.ws.ValidateAction@3f15fe01 [pattern=UrlPattern{inclusions=[/api/authentication/validate], exclusions=[]}]
2017.06.13 17:08:04 INFO  web[][o.s.s.p.Platform] WebServer is operational

[root@xx]  ./bin/linux-x86-64/sonar.sh restart

配置掃描器:
vim /app/original/sonar-scanner-3.0.3.778-linux/conf/sonar-scanner.properties

#----- Default SonarQube server
sonar.host.url=http://xxx.xxx.xxx:9090
#----- Default source code encoding
sonar.sourceEncoding=UTF-8

4、使用

[root@xx] sudo ln -s /app/original/sonar-scanner-3.0.3.778-linux/bin/sonar-scanner /usr/bin/sonar-scanner
讓sonar-scanner可執行文件加入全局
項目根目錄下新建文件
cd /app/project/
vim sonar-project.properties

sonar.projectKey=project:admin
sonar.projectName=project
sonar.projectVersion=1.4
sonar.sources=.
sonar.language=php
sonar.sourceEncoding=UTF-8

執行:

[root@xx project]# sonar-scanner 
INFO: Scanner configuration file: /app/original/sonar-scanner-3.0.3.778-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /app/project/sonar-project.properties
INFO: SonarQube Scanner 3.0.3.778

5、舉栗子

  1. 安裝好,啓動後的界面圖片描述

  2. 方便QA白盒的界面圖片描述

  3. 生產環境應該去掉的註釋 圖片描述

  4. 掃了一個開源插件,原來有好多bug,這裏靜態方法裏使用的動態調用. 圖片描述

  5. 查到的代碼冗餘 圖片描述

資料來源:
官網:https://www.sonarqube.org
兩分鐘安裝:https://docs.sonarqube.org/di...QQ:2764239385

相關文章
相關標籤/搜索