安裝準(zhǔn)備
云環(huán)境
CentOS Linux release 7.3.1611 (Core)
JAVA
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
Mysql
Server version: 5.7.17 MySQL Community Server (GPL)
安裝前檢查Mysql配置
確認(rèn)是否使用InnoDB引擎
可用下列命令來查看
mysql>show engines;
結(jié)果如下圖

圖1
修改配置文件/etc/my.cnf
加入以下內(nèi)容
[mysqld]
innodb_buffer_pool_size = 256M
query_cache_type=1
query_cache_size=32M
設(shè)置好之后,重啟mysql服務(wù)
service mysqld restart
安裝
下載
下載地址:http://www.sonarqube.org/downloads/
選擇最新 LTS 版的 SonarQube 安裝包(當(dāng)前版本為 sonarqube-5.6.6.zip)
下載之后上傳到云環(huán)境服務(wù)器,解壓安裝
我的云環(huán)境解壓后路徑如下圖

圖2
指定jdk1.8
解壓后,打開conf目錄,修改wrapper.conf文件,我的配置如下
# Path to JVM executable. By default it must be available in PATH.
# Can be an absolute path, for example:
#wrapper.java.command=/path/to/my/jdk/bin/java
wrapper.java.command=/home/jdk18111/bin/java
創(chuàng)建 Database
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
配置sonar.properties
conf目錄下,修改sonar.properties
sonar.jdbc.username=root
sonar.jdbc.password=自己密碼
#----- MySQL 5.x
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9000
# 啟用 sonar.web.javaOpts 并添加 -server參數(shù)
sonar.web.javaOpts=-server -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
打開9000端口
vi /etc/sysconfig/iptables
添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9000 -j ACCEPT
重啟防火墻,使端口配置生效
service iptables restart
啟動(dòng) SonarQube
第一次啟動(dòng)會(huì)自動(dòng)建表和做相應(yīng)的初始化
$SONAR_HOME/bin/linux-x86-64/sonar.sh start
瀏覽器中輸入
http://172.104.119.101:9000/sonarqube
見到下圖,即安裝成功

圖3
配置服務(wù)
創(chuàng)建文件 /etc/init.d/sonar
內(nèi)容如下
#!/bin/sh
#
# rc file for SonarQube
#
# chkconfig: 345 96 10
# description: SonarQube system (www.sonarsource.org)
#
### BEGIN INIT INFO
# Provides: sonar
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: SonarQube system (www.sonarsource.org)
# Description: SonarQube system (www.sonarsource.org)
### END INIT INFO
/usr/bin/sonar $*
依次執(zhí)行如下命令
ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar
chmod 755 /etc/init.d/sonar
chkconfig --add sonar
啟動(dòng)和停止服務(wù)
啟動(dòng)
service sonar start
停止
service sonar stop
重啟
service sonar restart
注:第一次啟動(dòng)服務(wù),可查看日志 /conf/sonar.log 觀察進(jìn)度,因?yàn)榈谝淮螘?huì)初始化數(shù)據(jù)庫和相關(guān)配置,需要一段時(shí)間
配置Maven
settings.xml 文件
內(nèi)容如下
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
....
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://172.104.119.101:9000/sonarqube
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
pom.xml 文件
每個(gè)java+maven項(xiàng)目的pom.xml文件配置內(nèi)容如下
<properties>
.....
<sonar.language>java</sonar.language>
<sonar.host.url>http://172.104.119.101:9000/sonarqube</sonar.host.url>
</properties>
....
<build>
<plugins>
....
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.3.0.603</version>
</plugin>
</plugins>
</build>
運(yùn)行
執(zhí)行
mvn sonar:sonar -Dmaven.test.skip -U
或
mvn sonar:sonar -Dmaven.test.skip
運(yùn)行后,部分結(jié)果如下圖

圖4
此時(shí)登錄
http://172.104.119.101:9000/sonarqube
顯示界面如下

圖5