【SonarQube 】程式碼質量管理平臺的安裝與搭建
準備工作:已安裝 JDK7 並配置好了環境變數
1 安裝 MySQL5.1
# rpm -qa | grep mysql ## 檢視該作業系統上是否已經安裝了 mysql 資料庫,
有的話,可以通過 rpm -e 命令 或者 rpm -e --nodeps 命令來解除安裝掉
# yum install mysql-server mysql mysql-devel
# service mysqld start
# chkconfig --list | grep mysqld
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
用上面的命令檢視到 MySQL 並沒有設定開機啟動,所以需要設定開機啟動
# chkconfig mysqld on
為了方便遠端管理,防火牆中開啟 3306 埠
# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
重啟防火牆,使埠配置生效
# service iptables restart
設定 MySQL 資料庫 root 使用者的密碼:
# mysqladmin -u root password 'root'
登入資料庫:
# mysql -u root -p
MySQL 授權遠端訪問(先用 root 登入 mysql)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root123' 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%';
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
設定 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%';
3、建立 sonarqube 資料庫(UTF-8 編碼)
3安裝sonarqube的webserver
下載最新 LTS 版的 SonarQube 安裝包(當前版本為 sonarqube-4.5.4.zip): 下載地址:http://www.sonarqube.org/downloads/
下載:
# 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 =root
#----- MySQL 5.x
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterE ncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9090
儲存以上配置(注意,要看看預設的 9000 埠是否已被佔用)
防火牆中開啟 9090 埠:
# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9090 -j ACCEPT
重啟防火牆,使埠配置生效
# service iptables restart
啟動 SonarQube Web Server
# /root/sonarqube/bin/linux-x86-64/sonar.sh start (初次啟動會自動建表和做相應的初始化)
如上圖所示是sonarqube的程式碼質量管理平臺,已經搭建好了
使用者名稱admin 密碼admin就可以登陸進去了
登入以後的介面
至此sonaqube質量管理平臺已經安裝完成。
如何使用sonarqueb
1、 安裝中文漢化包: Setting >> Update Center >> Available Plugins >> LOCALIZATION >> Chinese Pack >> Install
2 重啟sonarqube
# /root/sonarqube/bin/linux-x86-64/sonar.sh restart
3、 Maven 分析器外掛的配置與使用
配置profile
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url> jdbc:mysql://192.168.4.221:3306/sonarqube?useUnicode=true&chara cterEncoding=utf8 </sonar.jdbc.url>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>wusc.321</sonar.jdbc.password>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>http://192.168.4.221:9090/sonarqube</sonar.host.url>
</properties>
</profile>
使用 Maven 分析器進行分析,命令:
純 Maven 命令:mvn clean install sonar:sonar
Eclipse 中執行:clean install sonar:sonar (如果你是第一次執行此命令,看執行日誌你會發現它會先下載 sonar-runner 等外掛) 成功執行完分析命令後便可到 Web Server 中檢視程式碼質量分析結果資料。