程式碼分析工具--sonar
阿新 • • 發佈:2019-01-02
一 什麼是sonar
Sonar (SonarQube)是一個開源平臺,用於管理原始碼的質量。Sonar 不只是一個質量資料報告工具,更是程式碼質量管理平臺。支援的語言包括:Java、PHP、C#、C、Cobol、PL/SQL、Flex 等。二 安裝
下載和解壓,在sonar官網 http://www.sonarqube.org/downloads/ 上下載並解壓sona,我的是sonarqube-4.3.2。注意: 不要放在中文目錄下。
三 執行sonar
在sonar的 根目錄裡的bin目錄下,在bin下找到對應的作業系統版本的啟動檔案,執行 sonar.sh start 。訪問:四 Mysql 建立資料庫和sonar使用者(用於存取專案分析後的資料)
-
建立一個sonar的資料庫:CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
-
建立一個sonar使用者:grant all privileges on sonar.* to 'sonar'@'%' identified by 'sonar';
-
flush privileges;
五 配置sonarqube-4.3.2\conf\sonar.properties 檔案
1)配置啟動的http埠
sonar.web.host=0.0.0.0
sonar.web.port=9000
sonar.web.context=
三句前本來被註釋,取消註釋
2)取消mysql連線的註釋
#----- MySQL 5.x/6.x
# Comment the embedded database and uncomment the following properties to use MySQL. The validation query is optional.
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
sonar.jdbc.validationQuery: select 1
3) 配置連線資料庫的使用者名稱和密碼sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
六 配置maven
首先確認倆工具的版本是否匹配在maven的settings.xml中加入:
<profiles>
<profile>
<id>sonar</id>
<properties>
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url> <!-- Sonar資料庫訪問地址 -->
<sonar.jdbc.driver >com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password >sonar</sonar.jdbc.password>
<sonar.host.url >http://localhost:9000</sonar.host.url> <!-- Sonar伺服器訪問地址 -->
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sonar</activeProfile>
</activeProfiles>
七 利用sonar分析程式碼
在pom.xml所在目錄下 執行:mvn clean install -DskipTests=true mvn sonar:sonar
設定 skipTests=true 是為了避免單元測試執行兩次。install 和 sonar 指令預設都會執行單元測試。你可以禁用單元測試,相關的配置參考 Maven 的相關文件。
然後就是漫長的等待, 直到你看到介面列印執行成功語句。