使用 Sonar 檢測代碼質量


 

經歷了一段時間的加班趕項目進度之后,今天終於閑下來了。忽然不知道干啥。於是,想着做點什么吧。突然想起了碼雲上面有個代碼分析的功能,用的是 Sonar

於是想來玩玩這個。

 

一、下載Sonar,和初始化,啟動

打開瀏覽器,搜索sonarqube,進入官網,找到download按鈕,下載安裝包。瀏覽器下載慢的話, 可以復制下載鏈接 到迅雷里邊下載。

下載之后,解壓到任意目錄。

解壓后,如果是 windows 64位系統,進入windows-x86-64  目錄,雙擊InstallNTService.bat,成功之后,雙擊StartSonar.bat 啟動。

然后打開瀏覽器輸入:http://192.168.2.100:9000/。可以看到一個界面。

 

二、配置和創建數據庫

 

sonar需要java環境的支持,分析數據會保存到保存到數據庫。支持mysql,oracle,PostgreSQL,SQLServer

所以,這里需要默認你已經配置好了Java開發環境,已經安裝了mysql數據庫。

打開 sonar  解壓目錄的,conf,可以看到sonar.properties 文件中有如下配置:

# 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=sonar

#----- 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://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance


#----- Oracle 11g/12c
# - Only thin client is supported
# - Only versions 11.2.x and 12.x of Oracle JDBC driver are supported
# - The JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/
# - If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE

上面紅色加粗的東西本來是注釋掉的,現在我給解開了。並增加了配置。這里的配置,說明的數據庫信息,和連接的用戶信息。

但是,咱們還沒有這個用戶,和這個數據庫,所以需要執行如下命令來創建用戶和數據庫。:

#mysql -u root -p

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

數據庫弄好之后,同時需要復制一個數據庫驅動放到安裝目錄的:extensions\jdbc-driver\mysql  下面。比如我用的是:mysql-connector-java-6.0.3.jar

 

到這里工作完成一半了。如果要想運行分析,則還需要一個東西。根據官方的文檔,有如下內容:

 

Once the SonarQube platform has been installed, you're ready to install an analyzer and begin creating projects. A project is created in the platform automatically on its first analysis. However, if you need to set some configuration on your project before its first analysis, you have the option of provisioning it.

Running Analysis

First, you should install the plugin(s) for the language(s) of the project to be analyzed, either by a direct download or through the update center.

Then, you need to choose an analysis method. The following are available:

Note that we do not recommend running an antivirus on the machine where a SonarQube analysis runs, it could result in unpredictable behavior.

SonarQube.com User?

也就是,我們需要下載一個SonarQube Scanner 才能運行代碼分析。上面提供了6種,我選擇了第一種。下載點這里

1.下載之后,解壓到隨意目錄。假設目錄是:D:\develop\sonarqube-6.2\sonar-scanner-2.8

2. 將目錄D:\develop\sonarqube-6.2\sonar-scanner-2.8\bin 配置到環境變了path中。

3.打開D:\develop\sonarqube-6.2\sonar-scanner-2.8\conf 下的sonar-scanner.properties 文件,進行如下配置:

#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor

 

三、運行代碼分析

假設上的步驟你都配合好了。並且,啟動了SonarQube。

假設現在有項目myproject,進入到項目的根目錄創建一個文件sonar-project.properties,文件內容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=my:myproject #key是唯一的
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=myproject   #項目名字
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

 

進入CMD.進入到項目的目錄,就是剛才包含sonar-project.properties文件的這個目錄。

執行命令:sonar-scanner   

然后就是等待執行結果,大概幾分鍾吧。成功之后,進入:http://192.168.2.100:9000/

可以看到項目分析的結果,當然了,可能需要等待一會兒才能看到,因為有處理時間,我在運行上面的命令之后,看到cmd窗口成功完成了。馬上到這個頁面看,發現還沒有。

大概一分鍾之后刷新,發現,有了!如下所示:

 

點這個進去看就知道了。里邊有很詳細的結果匯報。有問題的代碼片段展示,並提示如何修改。還是挺好的。

 

 

錯誤記錄:

當時分析了三四個項目,其中有一個失敗了,其他都成功了,觀看日志發現描述是這樣的:

INFO: Analysis report generated in 9263ms, dir size=11 MB
INFO: Analysis reports compressed in 5699ms, zip size=4 MB
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 6:28.455s
INFO: Final Memory: 29M/1274M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: Failed to upload report - 500: An error has occurred. Please contact your administrator
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

結果是“EXECUTION FAILURE” 失敗了。

失敗原因是啥呢? 注意我加粗的文字。 分析的報告大小是 11MB,壓縮之后4MB。還是很大的。上傳失敗的原因是 MYSQL配置問題。

在mysql的終端窗口輸入如下命令:

 

show VARIABLES like '%max_allowed_packet%';

  可以看到max_allowed_packet的大小,用這個值 max_allowed_packet / 1024 /1024 = ? M

所以把這個設置大點就好了。在mysql的命令行窗口執行如下命令:

set global max_allowed_packet=6*1024*1024

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM