1、SonarQube的介绍

SonarQube是一个管理代码质量的开放平台。

可以从七个维度检测代码质量(为什么要用SonarQube):

(1)复杂度分布(complexity):代码复杂度过高将难以理解、难以维护
(2)重复代码(duplications):程序中包含大量复制粘贴的代码是质量低下的表现

(3)单元测试(unit tests):统计并展示单元测试覆盖率

(4)编码规范(coding rules):通过Findbugs,PMD,CheckStyle等规范代码编写

(5)注释(comments):少了可读性差,多了看起来费劲
(6)潜在的Bug(potential bugs):通过Findbugs,PMD,CheckStyle等检测潜在的bug
(7)结构与设计(architecture & design):依赖、耦合等

Sonar 可以集成不同的测试工具、代码分析工具、持续集成工具、IDE。

Sonar通过对代码质量分析结果数据进行再加工处理,通过量化的方式来度量代码质量的变化,从而可以方便地对工程进行代码质量管理。

支持语言包括:Java,PHP,C#,C等

SonarQube代码质量管理平台的介绍
1、SonarQube的介绍
SonarQube平台的组成:
(1)数据库:存放SonarQube的配置数据、代码质量的快照数据
(2)Web服务:用于查看SonarQube的配置数据、代码质量的快照数据
(3)分析器:对项目代码进行分析,生成质量结果数据并存入数据库中(分析器有多种,我们选用 SonarQube Maven Plugin)

IP:192.168.1.51
环境:CentOS 6.6、JDK7、MySQL5.1 、SonarQube-4.5.4(LTS)
root 用户操作
准备工作:已安装 JDK7、MySQL 并配置好了环境变量,

MySql数据库配置

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

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

mysql> show engines;

[root@yxq ~]# mysql -u root -p
Enter password: 
mysql> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MyISAM     | YES     | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.08 sec)
看你的 mysql 当前默认的存储引擎:

MyISAM查询快

mysql> show variables like '%storage_engine%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | MyISAM|
+----------------+--------+
1 row in set (0.01 sec)

修改 MySQL 存储引擎为 InnoDB, 在配置文件/etc/my.cnf 中的
[mysqld] 下面加入 default-storage-engine=INNODB
[root@yxq ~]# vi /etc/my.cnf

[mysqld]
default-storage-engine=INNODB

重启 mysql 服务器

[root@yxq ~]# service mysqld restart

再次登录 MySQL 查看默认引擎设置是否生效

mysql> show variables like '%storage_engine%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+
1 row in set (0.00 sec)

innodb_buffer_pool_size 参数值设置得尽可能大一点

这个参数主要作用是缓存 innodb 表的索引,数据,插入数据时的缓冲默认值:128M,

专用 mysql 服务器设置的大小:操作系统内存的 70%-80%最佳。

设置方法:my.cnf 文件[mysqld] 下面加入 innodb_buffer_pool_size 参数

[root@yxq ~]# vi /etc/my.cnf

[mysqld]

innodb_buffer_pool_size=256M

设置 MySQL 的查询缓存 query_cache_size ,最少设置 15M
[root@yxq ~]# vi /etc/my.cnf
[mysqld]

query_cache_size=32M

query_chace_type=1

query_cache_size=32M

重启 mysql 服务器

[root@yxq ~]# 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             | 0       |
| query_cache_type             | ON      |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
6 rows in set (0.00 sec)

创建 sonarqube 数据库(UTF-8 编码

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

上传sonarqube-4.5.4.zip到CI服务器192.168.1.51

下载最新 LTS 版的 SonarQube 安装包(当前版本为 sonarqube-4.5.4.zip):

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

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

[root@yxq ~]# cd /root

[root@yxq ~]# ls
anaconda-ks.cfg              install.log.syslog             Public
apache-tomcat-7.0.61.tar.gz  jdk1.7.0_45.tar.gz             sonarqube-4.5.4.zip
Desktop                      log.log                        svnadmin.war
Documents                    Music                          Templates
Downloads                    nexus                          Videos
druid-0.2.23.jar             nexus-2.11.2-03-bundle.tar.gz
install.log                  Pictures
[root@yxq ~]# unzip sonarqube-4.5.4.zip 
[root@yxq ~]# mv sonarqube-4.5.4 sonarqube
[root@yxq ~]# cd sonarqube
[root@yxq sonarqube]# cd conf
[root@yxq 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 端口是否已被占用,CI上9000已被svnadmin占用)

防火墙中打开 9090 端口:
[root@yxq conf]# vi /etc/sysconfig/iptables
[root@yxq conf]# cat /etc/sysconfig/iptables | grep 9090
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9090 -j ACCEPT
[root@yxq conf]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[root@yxq conf]#

启动SonarQube
[root@yxq conf]# /root/sonarqube/bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.

(初次启动会自动建表和做相应的初始化)
[root@yxq conf]# 
2016.03.26 01:57:12 INFO  app[o.s.p.m.JavaProcessLauncher] Launch process[web]: /usr/java/jdk1.7.0_45/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -

Djruby.management.enabled=false -Djruby.compile.invokedynamic=false -Xmx768m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=/root/sonarqube/temp

-cp ./lib/common/*:./lib/server/*:/root/sonarqube/lib/jdbc/mysql/mysql-connector-java-5.1.27.jar org.sonar.server.app.WebServer /tmp/sq-

process4984998373950270314properties
2016.03.26 01:57:13 WARN  web[o.s.p.ProcessEntryPoint]  Starting web
2016.03.26 01:57:14 INFO  web[o.s.s.app.Webapp]  Webapp directory: /root/sonarqube/web
2016.03.26 01:57:15 INFO  web[o.a.c.h.Http11Protocol]  Initializing ProtocolHandler ["http-bio-0.0.0.0-9090"]
2016.03.26 01:57:15 INFO  web[o.a.c.s.ContextConfig]  No global web.xml found
2016.03.26 01:57:16 INFO  web[o.e.plugins]  [sonar-1458982609553] loaded [], sites []
2016.03.26 01:57:17 INFO  web[o.s.s.p.ServerImpl]  SonarQube Server / 4.5.4 / eccfe672f5e585c39cc6a0b79122ad516fd61f3e
2016.03.26 01:57:17 INFO  web[o.s.c.p.Database]  Create JDBC datasource for jdbc:mysql://localhost:3306/sonarqube?
useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
2016.03.26 01:57:21 INFO  web[o.s.s.p.DefaultServerFileSystem]  SonarQube home: /root/sonarqube

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

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

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

JDK1.7

一、SonarQube 的配置(前提,先用 admin 用户登录)

1、 安装中文汉化包:

Setting >> Update Center >> Available Plugins >> LOCALIZATION >> Chinese Pack >> Install

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

安装完汉化包之后需要重启 SonarQube 才能生效(重启前可顺便把 CheckStyle、PMD 等插件安装一下)

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

[root@yxq ~]# /root/sonarqube/bin/linux-x86-64/sonar.sh restart
Stopping SonarQube...
Stopped SonarQube.
Starting SonarQube...
Started SonarQube.
[root@yxq ~]#

重启完之后刷新 SonarQube

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

在cmd中执行sonar分析

mvn clean install sonar:sonar

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

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

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

在 Maven 本地库中的 settings.xml

<profiles></profiles>节点中添加如下配置:

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

启动sonarqube

[root@localhost ~]# /root/sonarqube/bin/linux-x86-64/sonar.sh start

在pom.xml中加入

  1. <span style="white-space:pre">    </span><build>
  2. <pluginManagement>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.codehaus.sonar</groupId>
  6. <artifactId>sonar-maven-plugin</artifactId>
  7. <version>4.5.1</version>
  8. </plugin>
  9. </plugins>
  10. </pluginManagement>
  11. </build>

执行maven命令

clean install sonar:sonar

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

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

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

持续集成篇_05_SonarQube代码质量管理平台的介绍与安装-LMLPHP

04-27 05:48