應用介紹:sonarqube是一個用於代碼質量管理的開源平臺,用於管理源代碼的質量經過插件形式;能夠支持包括Java、C#/C++、PL/SQL、Cobol、javascrip、Groovy等等二十幾種編程語言的代碼質量管理與檢測;java
Sonarqube官網:https://www.sonarqube.org/mysql
環境:Java環境:jdk1.8+linux
Mysql數據庫:mysql5.6+git
系統:centos6.9github
安裝步驟:web
官方文檔:https://docs.sonarqube.org/display/SONAR/Installing+the+Serversql
官方下載:https://www.sonarqube.org/downloads/數據庫
一、下載及準備:編程
[root@dxm-oss02 src]#pwdbootstrap
/usr/local/src
[root@dxm-oss02 src]#wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.zip
[root@dxm-oss02 src]#unzip sonarqube-6.7.zip
[root@dxm-oss02 src]#useradd sonar //新建一個普通用戶,而後用普通用戶啓動sonarqube服務
[root@dxm-oss02 src]#chown -R sonar. Sonarqube-6.7
[root@dxm-oss02 src]#vim /etc/sysctl.conf //增長下面這行,否則啓動sonarqube的時候會報錯
vm.max_map_count=262144
[root@dxm-oss02 src]#sysctl -p //使配置生效
首先建立數據庫和用戶密碼
>CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
>grant all privileges on sonar.* to 'sonar'@'%' identified by 'sonar123654';
>GRANT ALL ON sonar.* TO ‘sonar’@’localhost’ IDENTIFIED BY ‘sonar123654’;
>flush privileges;
下面進入到sonarqube的主目錄:
二、編輯sonarqube和elasticsearch的配置文件:
(1) 、首先編輯sonarqube的配置文件:$sonarqube-6.7/conf/sonar.properties
(2) 、修改elasticsearch配置文件:$sonarqube/elasticsearch/config/elasticsearch.yml
修改這兩行配置的緣由是:在啓動sonarqube的時候它會先啓動elasticsearch服務,然而沒有作上面兩行修改的話,會報下面這個錯誤:
ERROR: bootstrap checks failed
system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
報這個錯的緣由是:這是在由於Centos6不支持SecComp,而ES5.2.0以上版本默認bootstrap.system_call_filter爲true進行檢測,因此致使檢測失敗,失敗後直接致使ES不能啓動。
三、運行腳本啓動服務:
[root@dxm-oss02 sonarqube-6.7]#su sonar ./bin/linux-x86-64/sonar.sh start
//以普通用戶起服務,否則es啓動會報錯,用戶:console、start、status、stop、restart
[root@dxm-oss02 sonarqube-6.7]#su sonar ./bin/linux-x86-64/sonar.sh status
注意:這裏當即查看狀態,短期內意義不大,由於一開始顯示running,並不表明會必定在後面的時間內running!也就是說,在啓動完全完成前不可信!什麼叫啓動"完全完成"? 見下面:
這裏所有啓動成功了才真的啓動成功了!
再次確認下:
至此,sonarqube就啓動成功了。
排錯說明:能夠先跟着sonar.log日誌,若是報了es錯誤,能夠去查看es.log;若是報了web錯誤,那麼就是查看web.log。我在安裝的時候,碰見了"root用戶沒法啓動elasticsearch問題","mysql鏈接問題","版本過低問題",或"內存溢出"...均可以在日誌裏有跡可尋。(es.log,web.log跟sonar.log在同一個目錄下)
注意:啓動成功後需在防火牆和騰訊雲開啓相應的端口外網纔可訪問
四、登入web端:在瀏覽器輸入:http://ip:port
默認登入帳號和密碼:admin/admin
登入後會先叫你生成一個token:
而後還得在maven的settings.xml設置sonar信息:$MAVEN_HOME/conf/settings.xml
<profiles>
<profile>
<id>sonar</id>
<properties>
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar123654</sonar.jdbc.password>
<sonar.host.url>http://localhost:9090</sonar.host.url> <!--Sonar服務器訪問地址-->
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sonar</activeProfile>
</activeProfiles>
配置完保存,退出!
五、進到項目裏有pom.xml同級目錄下,執行下面命令進行代碼分析,分析結果會在web客戶端顯示
mvn sonar:sonar \
-Dsonar.host.url=http://ip:9090 \
-Dsonar.login=feb1256d8b16847ae9a9c4340d5ac759f4f72a3d
這個命令就是剛纔生成token那裏複製過來的。我在進行分析的時候出現了下面這個錯誤:
[ERROR] Failed to execute goalorg.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar (default-cli) on project qishi: Error 500 on http://sonar/api/ce/submit?projectKey=com.xxx:xxx&;projectName=xxx : {"errors":[{"msg":"An error has occurred. Please contact your administrator"}]} -> [Help 1] //上傳分析報告失敗
經過查看sonarqube的web.log發現是mysql配置項max_allowed_packet的值太小
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (12774723 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable. at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3671) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2508) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
解決方案:
在mysql配置文件/etc/my.cnf裏[mysqld]添加下面這行配置並重啓mysql
max_allowed_packet=500M
重啓完mysql後把sonar也重啓下,不然雖然數據庫配置已變動,但對sonarqube的數據庫鏈接不會生效。
六、添加中文插件:
去這個網站下載對應版本的中文包:
https://github.com/SonarQubeCommunity/sonar-l10n-zh
而後把中文包拷貝到$sonarqube-6.7/extensions/plugins/目錄下,並重啓sonar便可
重啓完從新進行掃描,獲得結果以下: