SonarQube是管理代碼質量一個開源平台,可以快速的定位代碼中潛在的或者明顯的錯誤。
SonarQube安裝
1、環境准備
(1)sonarQube 下載地址https://www.sonarqube.org/downloads/
注:官網顯示目前最新版本是7.1
(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.7+)
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 看到歡迎界面即成功
(5)這里如果你是root用戶的話,可能會遇到問題,sonar.log顯示服務器stop了,沒有起來,查看./logs/es.log
java.lang.RuntimeException: can not run elasticsearch as root
elasticsearch不能使用root,網上查了點資料,說是在bin/elasticsearch加入ES_JAVA_OPTS="-Des.insecure.allow.root=true"就能解決問題,但是發現也不管用,查了下是新版本這條命令也廢棄了,於是悲劇的只能新建用戶了,
在linux中useradd yourname, passwd yourname, 建立用戶,然后在/etc/sudoers加入你的權限,登入用戶,復制過來,在繼續弄......
(6)繼續失敗,上面的問題是解決了,又出現新問題了:
Failed to initialize end point associated with ProtocolHandler ["http-nio-127.0.0.1-9001"]
貌似是端口被暫用了,改一個,改成9011,在起一次,ps ux | grep Sonar一下,程序起來了,用地址登入,失敗,
最后都像放棄了,把端口改回9000, sonar.web.host注釋掉, 也注釋掉sonar.web.context, 把
sonar.jdbc.username=sonar sonar.jdbc.password=sonar, 然后就OK了,能進去了
總算明白了, 這里的username, password應該是mysql數據庫的用戶和密碼, 打開網站后,默認的用戶和密碼是admin/admin, 這坑爹的網絡資料,故意讓我們入坑
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=sonar #數據庫用戶 sonar.jdbc.password=sonar #數據庫密碼 sonar.jdbc.url=jdbc:mysql://數據庫IP:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance #sonar.web.host=你的本地IP #sonar.web.context= sonar.web.port=9000
(4)重啟服務,觀察日志
4、SonarQube漢化
(1)按照如下步驟安裝中文插件
(2)重啟,重新訪問即可發現漢化成功。
( 3 ) 安裝C++插件,由於sonar自帶的CFamily收費,而且貴的死人,屌絲程序員只好自選出路, 有大神介紹了一個C++插件,那作者就是看CFamily插件收錢不爽,自己寫了個更好的,牛啊,看連接
https://github.com/SonarOpenCommunity/sonar-cxx/tree/cxx-1.0.0
下載圖中的c-plugin.jar cxx-plugin.jar文件, 按照下圖操作:
簡單使用
默認用戶名是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的集成