IP:192.168.50.131

环境: CentOS 6.6、 JDK7、 MySQL5.6 、 SonarQube-4.5.4(LTS)

root 用户操作

准备工作: 已安装 JDK7 并配置好了环境变量

一、安装配置数据库


1 、 安装 MySQL5.6


详情请参见博文《SVN之——SVN 版本管理系统的安装(基于CentOS6.5 + Subversion + Apache + Jsvnadmin)》

2、配置 MySQL

结合 SonarQube, MySQL 数据库最好使用 InnoDB 引擎, 可提高性能。

看你的 mysql 现在已提供什么存储引擎: mysql> show engines;

注:MySQL5.6默认的存储引擎为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 编码)

二、 安装 SonarQube 的 Web Server



下载 LTS 版的 SonarQube 安装包(我这里的版本为 sonarqube-4.5.4.zip ):

下载地址:​​ http://www.sonarqube.org/downloads/​



SonarQube之——SonarQube 代码质量管理平台的安装与配置_maven


1、下载

​https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-4.5.4.zip​

# wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-4.5.4.zip

2、解压安装

# unzip sonarqube-4.5.4.zip
# mv sonarqube-4.5.4 sonarqube

3、编辑 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&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9000

保存以上配置(注意,要看看默认的 9000 端口是否已被占用)防火墙中打开 9000 端口:

# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9000 -j ACCEPT

重启防火墙, 使端口配置生效


# service iptables restart

启动 SonarQube Web Server


# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start

(初次启动会自动建表和做相应的初始化)


浏览器中输入: http://192.168.50.131:9000/sonarqube/


SonarQube之——SonarQube 代码质量管理平台的安装与配置_SonarQube_02

登录, 默认用户名/密码为 admin/admin

SonarQube之——SonarQube 代码质量管理平台的安装与配置_eclipse_03

SonarQube之——SonarQube 代码质量管理平台的安装与配置_maven_04

到此, SonarQube 已安装完毕, 接下来是对 SonarQube 做相应的配置和使用

三、配置SonarQube


1、配置常用插件


大家可以到链接下载常用的插件,直接拷贝到SonarQube的extensions/plugins目录下,重启SonarQube即可。

我们重启后的效果图如下:

SonarQube之——SonarQube 代码质量管理平台的安装与配置_eclipse_05

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​

(请参考官方文档操作, 在此不作讲解,我们重点讲 SonarQube 结合 Maven 插件的使用)

3、 Maven 分析器插件的配置与使用

​http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+Maven​

在 Maven 本地库中的 settings.xml(我这里是 settings_lyz.xml) 配置文件中的<profiles></profiles>节点中添加如下配置:

<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url>
jdbc:mysql://192.168.50.131:3306/sonarqube?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>root</sonar.jdbc.password>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>http://192.168.50.131:9000/sonarqube</sonar.host.url>
</properties>
</profile>

使用 Maven 分析器进行分析, 命令:


纯 Maven 命令:


mvn clean install sonar:sonar

我们在命令行进入项目pom.xml所在目录,在命令行中直接执行命令


mvn clean install sonar:sonar

注:需提前配置好Maven环境变量

Eclipse 中执行:


clean install sonar:sonar

具体操作为:右键pom.xml->Run as->Maven build->

SonarQube之——SonarQube 代码质量管理平台的安装与配置_mysql_06

(如果你是第一次运行此命令, 看执行日志你会发现它会先下载 sonar-runner 等插件)

成功执行完分析命令后便可到 Web Server 中查看代码质量分析结果数据。

四、温馨提示

大家可以到链接​​​​下载sonarqube 4.5.x 常用Java插件包及汉化包