SonarQube代碼品質管理平台的安裝及使用
- 一、SonarQube代碼品質管理平台的安裝
-
- 1、安裝MySQL5.1
- 2、配置MySQL
- 3、建立sonarqube資料庫(UTF-8編碼)
- 4、安裝SonarQube的Web Server
- 二、SonarQube代碼品質管理平台的配置與使用
-
- 1、安裝中文漢化包:
- 2、MyEclipse/Eclipse中安裝SonarQube插件的安裝、配置、使用
- 3、Maven分析器插件的配置與使用
- 4、配置:
- 5、品質配置介紹(切換預設配置)
- 6、代碼規則介紹(自定義規則)
- 7、問題處理介紹(品質管理的展現)
- 8、指表
- 9、儀表盤(自定義)
一、SonarQube代碼品質管理平台的安裝
- IP:192.168.0.221
- 環境:CentOS 6.6、JDK7、MySQL5.1 、SonarQube-4.5.4(LTS)
- root使用者操作
- 準備工作:已安裝JDK7并配置好了環境變量
1、安裝MySQL5.1
- (可參考前面SVN管理平台的MySQL安裝步驟,如果已安裝則無需安裝)
-
rpm -qa | grep mysql
- 檢視該作業系統上是否已經安裝了mysql資料庫,有的話,可以通過 rpm -e 指令 或者 rpm -e --nodeps 指令來解除安裝掉
-
yum install mysql-server mysql mysql-devel
-
service mysqld start
-
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:offchkconfig --list | grep mysqld
- 用上面的指令檢視到MySQL并沒有設定開機啟動,是以需要設定開機啟動
-
chkconfig mysqld on
- 為了友善遠端管理,防火牆中打開3306端口
-
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPTvi /etc/sysconfig/iptables
- 重新開機防火牆,使端口配置生效
-
service iptables restart
- 設定MySQL資料庫root使用者的密碼:
-
mysqladmin -u root password 'hk.123'
- 登入資料庫:
-
mysql -u root -p
- MySQL授權遠端通路(先用root登入mysql)
-
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'hk.321' WITH GRANT OPTION;
-
mysql> FLUSH PRIVILEGES;
2、配置MySQL
- 結合SonarQube,MySQL資料庫最好使用InnoDB引擎,可提高性能。
- 看你的mysql現在已提供什麼存儲引擎:
mysql> show engines;
- 看你的mysql目前預設的存儲引擎:
-
mysql> show variables like '%storage_engine%';
- 修改MySQL存儲引擎為InnoDB, 在配置檔案/etc/my.cnf中的[mysqld] 下面加入default-storage-engine=INNODB
# vi /etc/my.cnf
[mysqld]
default-storage-engine=INNODB
- 重新開機mysql伺服器
-
service mysqld restart
- 再次登入MySQL檢視預設引擎設定是否生效
mysql> show variables like '%storage_engine%';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+
- innodb_buffer_pool_size 參數值設定得盡可能大一點
- 這個參數主要作用是緩存innodb表的索引,資料,插入資料時的緩沖
- 預設值:128M,專用mysql伺服器設定的大小:作業系統記憶體的70%-80%最佳。
- 設定方法:my.cnf檔案[mysqld] 下面加入innodb_buffer_pool_size參數
# vi /etc/my.cnf
[mysqld]
innodb_buffer_pool_size = 256M
(我們這裡設定為256M,因為我們的不是專用的MySQL資料庫伺服器,還有很多其他的服務需要占用系統記憶體)
- 設定MySQL的查詢緩存query_cache_size ,最少設定15M
vi /etc/my.cnf
[mysqld]
query_cache_type=1
query_cache_size=32M
- 重新開機mysql伺服器
-
service mysqld restart
- 驗證緩存設定是否生效:
mysql> show variables like '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 33554432 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
3、建立sonarqube資料庫(UTF-8編碼)
CREATE DATABASE IF NOT EXISTS sonarqube DEFAULT CHARSET 'UTF8';
4、安裝SonarQube的Web Server
- 下載下傳最新LTS版的SonarQube安裝包(目前版本為sonarqube-4.5.4.zip):
- 下載下傳位址:http://www.sonarqube.org/downloads/
- http://dist.sonar.codehaus.org/sonarqube-4.5.4.zip
- 下載下傳:
-
wget http://dist.sonar.codehaus.org/sonarqube-4.5.4.zip
- 解壓安裝:
-
unzip sonarqube-4.5.4.zip
-
mv sonarqube-4.5.4 sonarqube
- 編輯sonar配置:
cd sonarqube/conf/
vi sonar.properties
sonar.jdbc.username=root
sonar.jdbc.password=hk.123
- ----- MySQL 5.x
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9090
- 儲存以上配置(注意,要看看預設的9000端口是否已被占用)
- 防火牆中打開9090端口:
-
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9090 -j ACCEPTvi /etc/sysconfig/iptables
- 重新開機防火牆,使端口配置生效
-
service iptables restart
- 啟動 SonarQube Web Server
-
/root/sonarqube/bin/linux-x86-64/sonar.sh start
- (初次啟動會自動建表和做相應的初始化)
- 浏覽器中輸入:http://192.168.0.221:9090/sonarqube/
- 登入,預設使用者名/密碼為 admin/admin
- 到此,SonarQube已安裝完畢,接下來是對SonarQube做相應的配置和使用
二、SonarQube代碼品質管理平台的配置與使用
- SonarQube的配置(前提,先用admin使用者登入)
1、安裝中文漢化包:
- Setting >> Update Center >> Available Plugins >> LOCALIZATION >> Chinese Pack>> Install
- 安裝完漢化包之後需要重新開機SonarQube才能生效(重新開機前可順便把CheckStyle、PMD等插件安裝下)
- ####針對老版本更新不了語言包等插件,點選以下連結進行下載下傳(資源描述有使用說明)####
- https://download.csdn.net/download/hekaihaw/11029427
-
/root/sonarqube/bin/linux-x86-64/sonar.sh restart
Stopping SonarQube...
Stopped SonarQube.
Starting SonarQube...
Started SonarQube.
- 重新開機完之後重新整理SonarQube:http://192.168.0.221:9090/sonarqube/
- 漢化成功(注意,漢化包并沒有完全漢化)
- 重新登入
2、MyEclipse/Eclipse中安裝SonarQube插件的安裝、配置、使用
http://docs.sonarqube.org/display/SONAR/SonarQube+in+Eclipse
http://docs.sonarqube.org/display/SONAR/Installing+SonarQube+in+Eclipse
http://docs.sonarqube.org/display/SONAR/Configuring+SonarQube+in+Eclipse
http://docs.sonarqube.org/display/SONAR/Working+with+SonarQube+in+Eclipse
3、Maven分析器插件的配置與使用
- http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+Maven
- 在Maven本地庫中的settings.xml(我這裡是settings_edu.xml)配置檔案中的
- 節點中添加如下配置:
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url>
jdbc:mysql://192.168.0.221:3306/sonarqube?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>hk.3211</sonar.jdbc.password>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>http://192.168.0.221:9090/sonarqube</sonar.host.url>
</properties>
</profile>
- 使用Maven分析器進行分析,指令:
- 純Maven指令:
mvn clean install sonar:sonar
- Eclipse中執行:
clean install sonar:sonar
- (如果你是第一次運作此指令,看執行日志你會發現它會先下載下傳sonar-runner等插件)成功執行完分析指令後便可到Web Server中檢視代碼品質分析結果資料。
4、配置:
- 配置
- 權限
- 系統