工具安裝:代碼檢測工具SonarQube安裝步驟

SonarQube 是一個用於管理代碼質量的開放平臺,可以快速的定位代碼中潛在的或者明顯的錯誤。目前支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十幾種編程語言的代碼質量管理與檢測。持續檢查項目整體的健康程度,如項目整體的Bugs、Vulnerabilities、Code Smells。

下載地址:https://www.sonarqube.org/downloads/

下載社區版本: community 的版本

下載後直接解壓,如下圖 windows系統啓動:

直接啓動

一些小問題:雙擊 StartSonar.bat 可能會失敗,再次打開的時候提示正在運行:

這是系統的一個小Bug,此時需要打開資源管理器(Ctrl + Shift + Esc) 找到Java(TM) Platform SE binary,直接結束任務,可能會多個,都要結束掉。再次重啓即可!

配置數據庫

找到 conf 目錄,如下圖:

打開 sonar.properties 文件,添加內容如下:

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin

效果如下:

配置結束後,需要在數據庫中創建一個 sonar 庫,創建空數據庫即可,系統會自動初始化數據表。

此時,再次啓動即可,初次啓動可能比較慢,需要幾分鐘的時間。

登錄

啓動成功後,在瀏覽器中輸入 http://localhost:9000 即可打開登錄頁面,用戶名/密碼爲默認的 admin/admin

使用:

Maven命令式啓用

在工程根目錄(pom.xml所在目錄)執行命令: 

mvn sonar:sonar

Sonar Scanner 方式啓用

Windows系統下

下載 sonar-scanner-cli-3.3.0.1492-windows 包,直接解壓即可

  • 1. 修改配置文件 

內容如下:

#Configure here general information about the environment, such as SonarQube server connection 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
  • 2. 配置環境變量: 

新建變量:SONAR_SCANNER_HOME = D:\software\sonar-scanner-cli-3.3.0.1492-windows

配置path: %SONAR_SCANNER_HOME%\bin;

配置結束後,在cmd命令中執行  sonar-scanner -h  如下,即配置成功:

在項目父工程pom.xml同目錄創建 sonar-project.properties 

# must be unique in a given SonarQube instance
sonar.projectKey=project-name
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=project-name
sonar.projectVersion=1.0 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=./
sonar.java.sources=./
sonar.java.binaries=./
sonar.exclusions**/*Test.java, target/**
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

在同目錄,執行cmd命令:

sonar-scanner

編譯結果如下:

打開指定鏈接地址,即可查看檢測結果

 

Linux 系統下(配置環境變量)

vim /etc/profile  添加內容如下:

export SONAR_SCANNER_HOME=/app/sonarqube/sonar-scanner-4.2.0.1873-linux
export PATH=$PATH:$SONAR_SCANNER_HOME/bin

其它步驟與windows系統下一致

發佈了65 篇原創文章 · 獲贊 23 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章