SonarQube是管理代碼質量一個開源平台,可以快速的定位代碼中潛在的或者明顯的錯誤。
SonarQube安裝
1、環境准備
(1)sonarQube 下載地址https://www.sonarqube.org/downloads/
注:官網顯示目前最新版本是6.2,但是實在是下載不下來,迫於無奈選擇了5.6.4。
(2)sonarQube Scanners 下載地址http://docs.sonarqube.org/display/SCAN/Analyzing+Source+Code
(2)jdk1.8 (注:根據官網信息,需要用到jdk1.8,如果你的環境已經配置了JAVA_HOME是jdk1.7,沒關系我們可以手動指定sonar的運行jdk為1.8(后面配置的時候說明))
(4)安裝mysql,可以是遠程連接(注:mysql版本需要是5.6+)
2、SonarQube 安裝
(1)指定jdk1.8(如果系統環境變量已經是1.8忽略此步)
下載好sonarQube后,解壓打開conf目錄,修改 wrapper.conf
(2)啟動sonar
[root@fastdfs1 sonarqube-5.6.4]# ./bin/linux-x86-64/sonar.sh start
(3) 觀察啟動日志
[root@fastdfs1 sonarqube-5.6.4]# tail -200f ./logs/sonar.log
(4)訪問http://你的IP:9000/sonar 看到歡迎界面即成功
3、SonarQube配置
(1)mysql 添加sonar用戶
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;
(2)sonarQube配置mysql,修改/conf/sonar.properties
sonar.jdbc.username=test sonar.jdbc.password=1234 sonar.jdbc.url=jdbc:mysql://數據庫IP:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.web.host=你的本地IP sonar.web.context=/sonar sonar.web.port=9000
(4)重啟服務,觀察日志
4、SonarQube漢化
(1)按照如下步驟安裝中文插件
(2)重啟,重新訪問即可發現漢化成功。
簡單使用
默認用戶名是admin 密碼admin
(1)配置sonar-scanner(如果SonarQube和Sonar-scanner不在同一台服務器,修改/sonar-scanner-2.8/conf/sonar-scanner.properties)
sonar.host.url=http://sonarQubeIP:9000 sonar.jdbc.username=test sonar.jdbc.password=1234 sonar.jdbc.url=jdbc:mysql://數據庫IP:3306/sonar?useUnicode=true&characterEncoding=utf8
(2)sonar-scanner指定jdk1.8
修改/sonar-scanner-2.8/bin/sonar-runner
至此,SonarQube和Sonar-Scanner配置成功了。后面介紹Jenkins和Sonar的集成