1. 程式人生 > >持續集成之代碼質量管理 Sonar(五)

持續集成之代碼質量管理 Sonar(五)

0.0.0.0 recv cal perf 不同的 profile cache auto rpm

一、Sonar 介紹

Sonar 是一個用於代碼質量管理的開放平臺。通過插件機制,Sonar可以集成不同的測試工具,代碼分析工具,以及持續集成工具。與持續集成工具(例如 Hudson/Jenkins 等)不同,Sonar 並不是簡單地把不同的代碼檢查工具結果(例如 FindBugs,PMD 等)直接顯示在 Web 頁面上,而是通過不同的插件對這些結果進行再加工處理,通過量化的方式度量代碼質量的變化,從而可以方便地對不同規模和種類的工程進行代碼質量管理。

在對其他工具的支持方面,Sonar 不僅提供了對 IDE 的支持,可以在 Eclipse和 IntelliJ IDEA 這些工具裏聯機查看結果;同時 Sonar 還對大量的持續集成工具提供了接口支持,可以很方便地在持續集成中使用 Sonar。

此外,Sonar 的插件還可以對 Java 以外的其他編程語言提供支持,對國際化以及報告文檔化也有良好的支持。

二、Sonar 安裝

Sonar 的相關下載和文檔可以在下面的鏈接中找到:http://www.sonarqube.org/downloads 。 需要註意最新版的 Sonar 需要至少JDK 1.8及以上版本。

之前我們已經可以成功的使用 git 進行拉取,Sonar 的功能就是來檢查代碼是否有BUG。除了檢查代碼是否有 bug 還有其他的功能,比如說:你的代碼註釋率是多少,代碼有一些建議,編寫語法的建議。所以我們叫質量管理

Sonar 還可以給代碼打分,並且引用了技術宅的功能(告訴你有很多地方沒改)

1、安裝 jdk

JDK 可以去 Oracle 官網去下載,也可以使用 yum 倉庫的 JDK。

rpm -ivh jdk-8u45-linux-x64.rpm

2、下載安裝包

我這裏下載的是長期維護的一個版本6.7.5。

wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.5.zip

3、解壓

unzip sonarqube-6.7.5.zip
mv sonarqube-6.7.5 /usr/local/sonarqube

三、準備數據庫

因為 sonar 會用到數據庫,所以我們要事先準備好數據庫,它支持多種數據庫,我們這裏選擇的是 MySQL/Mariadb,註意 MySQL 的版本最低要5.6,因為我這裏有現有的數據庫,所以安裝過程省略。

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> GRANT ALL ON sonar.* TO ‘sonar‘@‘localhost‘ IDENTIFIED BY ‘sonar@pw‘;
mysql> GRANT ALL ON sonar.* TO ‘sonar‘@‘%‘ IDENTIFIED BY ‘sonar@pw‘;
mysql> FLUSH PRIVILEGES;

四、配置 Sonar

1、配置數據庫連接

編輯/usr/local/sonarqube/conf/sonar.properties,修改內容如下:

# 數據庫連接
sonar.jdbc.username=sonar           
sonar.jdbc.password=sonar@pw    
sonar.jdbc.url=jdbc:mysql://10.0.1.13:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

# Web 端口
sonar.web.host=0.0.0.0
sonar.web.port=9000

2、創建用戶 sonar

因為程序不允許我們使用 root 執行,所以我們需要註冊一個普通用戶

useradd sonar
chown sonar.sonar /usr/local/sonarqube -R

3、啟動 sonar

[sonar@monitor ~]$ /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.

4、檢查是否啟動成功

[root@monitor ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:32000             0.0.0.0:*                   LISTEN      8126/java           
tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      1804/zabbix_agentd  
tcp        0      0 0.0.0.0:10051               0.0.0.0:*                   LISTEN      2202/zabbix_server  
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      16611/rsync         
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2040/mysqld         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1586/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1677/master         
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      16486/java          
tcp        0      0 :::9000                     :::*                        LISTEN      8258/java           
tcp        0      0 ::ffff:127.0.0.1:9001       :::*                        LISTEN      8151/java           
tcp        0      0 :::873                      :::*                        LISTEN      16611/rsync         
tcp        0      0 :::8080                     :::*                        LISTEN      16486/java          
tcp        0      0 :::80                       :::*                        LISTEN      4841/httpd          

五、登錄 sonar

Web登錄:IP:9000
技術分享圖片

sonar跟jenkins類似,也是以插件為主。
sonar安裝插件有2種方式:第一種將插件下載完存放在sonar的插件目錄,第二種使用web界面來使用安裝存放插件路徑[/usr/local/sonarqube/extensions/plugins/]。

1、安裝中文插件

登陸:用戶名:admin 密碼:admin
技術分享圖片

技術分享圖片

重啟才會生效。

技術分享圖片

生效後界面如下。

技術分享圖片

和 jenkins 一樣,需要什麽插件直接去應用市場下載即可。

2、安裝 sonar 掃描器

Sonar通過SonarQube Scanner(掃描器)來對代碼進行分析。

wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
unzip sonar-scanner-2.8.zip
mv sonar-scanner-2.8 /usr/local/sonar-scanner

3、掃描器和 sonar 關聯起來

編輯文件/usr/local/sonar-scanner/conf/sonar-scanner.properties,修改為如下內容:

sonar.host.url=http://localhost:9000        
sonar.sourceEncoding=UTF-8                  
sonar.jdbc.username=sonar 
sonar.jdbc.password=sonar@pw
sonar.jdbc.url=jdbc:mysql:/10.0.1.13:3306/sonar?useUnicode=true&characterEncoding=utf8

六、代碼掃描

1、下載代碼

sonar 插件提供了一個測試的代碼庫。
github:https://github.com/SonarSource/sonar-scanning-examples
下載地址:https://codeload.github.com/SonarSource/sonar-scanning-examples/zip/master

wget https://codeload.github.com/SonarSource/sonar-scanning-examples/zip/master
unzip master
cd sonar-scanning-examples-master/sonarqube-scanner

2、sonar-project.properties

我們會看到項目下面有一個文件 sonar-project.properties,如果想要代碼被掃描,就需要在代碼路徑下放一個配置文件。

[root@monitor sonarqube-scanner]# cat sonar-project.properties 
sonar.projectKey=org.sonarqube:sonarqube-scanner            #Key
sonar.projectName=Example of SonarQube Scanner Usage    #這裏的名稱會顯示在web上面
sonar.projectVersion=1.0                                            #版本,也會顯示在web上面

sonar.sources=src,copybooks                            軟件包存放路徑

sonar.sourceEncoding=UTF-8                           #字體編碼

## Cobol Specific Properties

# comma-separated paths to directories with copybooks
sonar.cobol.copy.directories=copybooks
# comma-separated list of suffixes
sonar.cobol.file.suffixes=cbl,cpy
sonar.cobol.copy.suffixes=cpy

## Flex Specific Properties

# retrieve code coverage data from the Cobertura report
sonar.flex.cobertura.reportPath=coverage-report/coverage-cobertua-flex.xml

# PL/I Specific Properties
sonar.pli.marginLeft=2
sonar.pli.marginRight=0

3、掃描

需要在項目文件裏面進行執行,程序會在當面目錄下掃描sonar-project.properties文件,根據配置文件進行掃描工作。掃描之後我們在web界面上就可以看到代碼的掃描結果。

[root@monitor sonarqube-scanner]# /usr/local/sonar-scanner/bin/sonar-scanner
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner/sonar-project.properties
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_45 Oracle Corporation (64-bit)
INFO: Linux 2.6.32-573.el6.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=72ms
INFO: Server id: A737A953-AWVGKXYQugQwgVmc5Jb0
WARN: Property ‘sonar.jdbc.url‘ is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property ‘sonar.jdbc.username‘ is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property ‘sonar.jdbc.password‘ is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
INFO: User cache: /root/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=48ms
INFO: Plugin [l10nzh] defines ‘l10nen‘ as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: SonarQube server 6.7.5
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=13ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=30ms
INFO: Load active rules
INFO: Load active rules (done) | time=505ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=22ms
WARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
INFO: Project key: org.sonarqube:sonarqube-scanner
INFO: -------------  Scan Example of SonarQube Scanner Usage
INFO: Load server rules
INFO: Load server rules (done) | time=69ms
INFO: Base dir: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner
INFO: Working dir: /root/tmp/sonar-scanning-examples-master/sonarqube-scanner/.sonar
INFO: Source paths: src, copybooks
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 36 files indexed
INFO: Quality profile for flex: Sonar way
INFO: Quality profile for js: Sonar way
INFO: Quality profile for php: Sonar way
INFO: Quality profile for py: Sonar way
INFO: Quality profile for xml: Sonar way
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=10ms
INFO: Sensor Python Squid Sensor [python]
INFO: Python unit test coverage
INFO: Python integration test coverage
INFO: Python overall test coverage
INFO: Sensor Python Squid Sensor [python] (done) | time=397ms
INFO: Sensor SonarJavaXmlFileSensor [java]
INFO: 1 source files to be analyzed
INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=66ms
INFO: 1/1 source files have been analyzed
INFO: Sensor Flex [flex]
INFO: 2 source files to be analyzed
INFO: 2/2 source files have been analyzed
INFO: Sensor Flex [flex] (done) | time=96ms
INFO: Sensor Flex Cobertura [flex]
INFO: Analyzing Cobertura report: coverage-report/coverage-cobertua-flex.xml
WARN: Using absolute path pattern is deprecated. Please use relative path instead of ‘file:**/Circle.as‘
WARN: Using absolute path pattern is deprecated. Please use relative path instead of ‘file:**/UncoveredCircle.as‘
INFO: Sensor Flex Cobertura [flex] (done) | time=64ms
INFO: Sensor XML Sensor [xml]
INFO: Sensor XML Sensor [xml] (done) | time=124ms
INFO: Sensor PHP sensor [php]
INFO: 1 source files to be analyzed
INFO: 1/1 source files have been analyzed
INFO: No PHPUnit test report provided (see ‘sonar.php.tests.reportPath‘ property)
INFO: No PHPUnit coverage reports provided (see ‘sonar.php.coverage.reportPaths‘ property)
INFO: Sensor PHP sensor [php] (done) | time=382ms
INFO: Sensor Analyzer for "php.ini" files [php]
INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=2ms
INFO: Sensor JavaScript Squid Sensor [javascript]
INFO: 1 source files to be analyzed
INFO: Unit Test Coverage Sensor is started
INFO: 1/1 source files have been analyzed
INFO: Integration Test Coverage Sensor is started
INFO: Overall Coverage Sensor is started
INFO: Sensor JavaScript Squid Sensor [javascript] (done) | time=198ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=23ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=17ms
INFO: No SCM system was detected. You can use the ‘sonar.scm.provider‘ property to explicitly specify it.
INFO: 7 files had no CPD blocks
INFO: Calculating CPD for 6 files
INFO: CPD calculation finished
INFO: Analysis report generated in 92ms, dir size=91 KB
INFO: Analysis reports compressed in 26ms, zip size=42 KB
INFO: Analysis report uploaded in 140ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/org.sonarqube:sonarqube-scanner
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AWVGrCcUySdQw75xjxNM
INFO: Task total time: 3.969 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 4.929s
INFO: Final Memory: 54M/414M
INFO: ------------------------------------------------------------------------

4、web查看掃描結果

技術分享圖片

技術分享圖片

持續集成之代碼質量管理 Sonar(五)