关于Sonar

正文

1. Wikipedia关于Sonar

1.1. 简介

       SonarQube(以前叫Sonar)是持续检测代码质量的开源平台。

1.2. 特性

  • l 支持25种以上的语言:JavaC/C++C#PHPFlex,GroovyJavaScriptPythonPL/SQLCOBOL
  • l 可以用在Android开发中
  • l 提供重复代码,代码标准,单元测试,代码覆盖,复杂代码,潜在代码,注释以及设计和架构的报表
  • l 记录度量历史以及提供演化图时间机器(“time machine”)和不同视图
  • l 提供全自动的分析:集成Maven,AntGradle以及持续集成工具(Atlassian BambooJenkinsHudson等)
  • l 集成Eclipse开发环境
  • l 集成外部工具:JIRAMantisLDAPFortify
  • l 可以通过插件扩展
  • l 实现SQALE方法论来计算技术负债(technical debt
  • l 支持Tomcat,但是Tomcat最终支持是SonarQube 4.1。单独模式是唯一支持Tomcat的,其中内嵌一个Tomcat服务器。

1.3. 名声

2009年Sonar获得测试工具目录下Jolt大奖,是全特性的持续集成工具。在Andalusian Autonomous Government,eXo Platform,Apache Software Foundation,Eclipse Foundation中被使用。

2013年,代码质量检测工具的使用比例[2]: 

1.4. 外部连接

1.5. 进一步阅读

1.6. 另见

这个列表中的很多工具值得研究,有些工具听说过,更多的工具没有听说过,Wikipedia的真是个好地方。

2. 参考[1]中关于Sonar

2.1. 简介

Sonar(代码质量管理平台)是一个开源平台,用于管理Java源代码的质量。从 Sonar 1.6 版本开始,Sonar从一个质量数据报告工具,转变成为的代码质量管理开源平台。Sonar主要特点:

  • l 代码覆盖:通过单元测试,将会显示哪行代码被选中
  • l 改善编码规则
  • l 搜寻编码规则:按照名字,插件,激活级别和类别进行查询
  • l 项目搜寻:按照项目的名字进行查询
  • l 对比数据:比较同一张表中的任何测量的趋势

架构图:

 

通过插件,Sonar可以从七个维度检测代码质量,支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScript,Groovy等等二十几种编程语言,其可以处理代码中如下的问题:

1.糟糕的复杂度分布
文件、类、方法等,如果复杂度过高将难以改变,这会使得开发人员难以理解它们,且如果没有自动化的单元测试,对于程序中的任何组件的改变都将可能导致需要全面的回归测试

2.重复
包含大量复制粘贴的代码的程序显然是质量低下的,sonar可以展示源码中重复严重的地方

3.缺乏单元测试
sonar可以很方便地统计并展示单元测试覆盖率

4.没有代码标准
sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具规范代码编写
5.没有足够的或者过多的注释
没有注释将使代码可读性变差,特别是当不可避免地出现人员变动时,程序的可读性将大幅下降而过多的注释又会使得开发人员将精力过多地花费在阅读注释上,亦违背初衷 (参考[1])

6.潜在的bug
sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具检测出潜在的bug
7.糟糕的设计(原文Spaghetti Design,意大利面式设计)
sonar可以找出循环,展示包与包、类与类之间的相互依赖关系可以检测自定义的架构规则,可以管理第三方的jar包,可以利用LCOM4检测单个任务规则的应用情况。

通过sonar可以有效检测以上在程序开发过程中的七大问题

2.2. SonarQube安装

2.2.1. 预置条件

1.已安装JAVA环境

2.已安装有MySQL数据库(5.6直接支持,之前的版本需要Sonar提供的Driver)

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

下载SonarQube与SonarQube Runner

中文补丁包下载:http://docs.codehaus.org/display/SONAR/Chinese+Pack

关于最新版的Sonar的安装参考文献[8],百度文库上有别人翻译的早期Sonar的官方文档[8]。

2.2.2. 正式安装

1.数据库配置

进入数据库命令:

#mysql -u root -p
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;

当然,也可以将上述的语句写到一个test.sql脚本中,进入mysql后,使用source  全路径名/test.sql。

2.安装sonar与sonar-runner
将下载的sonar-3.7.4zip(长期支持版,2014年4月最新的版本为4.2)包解压至Linux中任意路径中,比如/usr/local 

将下载的sonar-runner-dist-2.3.zip包解压Linux中任意路径中,比如/usr/local 。添加SONAR_RUNNER_HOME(就是sonar-runner-2.3的全路径名,比如/usr/local/sonar-runner-2.3)环境变量,并将SONAR_RUNNER_HOME/bin加入PATH变量中(在主路径的.bashrc文件中添加)。

修改sonar配置文件

编辑sonar所在的目录(比如/usr/local/sonar-3.7.4)中conf/sonar.properties文件,配置数据库设置,默认已经提供了各类数据库的支持,只要将注释去掉就可以。这里使用mysql,因此取消mysql模块的注释,并将sonar中原有的嵌入式的数据库的jdbc.url注释掉。

#vi sonar.properties

#需要注释下面这条语句

#sonar.jdbc.url:                            jdbc:h2:tcp://localhost:9092/sonar

sonar.jdbc.username:                       sonar

sonar.jdbc.password:                       sonar

sonar.jdbc.url:                            jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

# Optional properties

sonar.jdbc.driverClassName:                com.mysql.jdbc.Driver

修改sonar-runner的配置文件

切换至sonar-runner的安装目录下,修改sonar-runner.properties根据实际使用数据库情况取消相应注释,这里需要和sonar.properties中保持一致。

#Configure here general information about the environment, such as SonarQube DB details for example

#No information about specific project should appear here

#----- Default SonarQube server

sonar.host.url=http://localhost:9000

#----- PostgreSQL

#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

sonar.jdbc.driverClassName=com.mysql.jdbc.Driver

#----- Oracle

#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer

#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor

#----- Global database settings

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

#----- Default source code encoding

sonar.sourceEncoding=UTF-8

#----- Security (when 'sonar.forceAuthentication' is set to 'true')

sonar.login=admin

sonar.password=admin

添加数据库驱动

除了Oracle数据库外,其它数据库驱动都默认已经提供了,且这些已添加的驱动是sonar唯一支持的,不需要修改。如果是Oracle数据库,需要从oracle官方网站下载JDBC驱动并将其需要复制至<install_directory>/extensions/jdbc-driver/oracle目录

4.启动Sonar

目录切换至sonar的<install_directory>/bin/linux-x86-64/目录,启动服务

#./sonar.sh start   启动服务

#./sonar.sh stop    停止服务

#./sonar.sh restart 重启服务

当然,如果每次都是这样使用cd 然后在./sonar.sh启动和关闭服务比较麻烦,这里,可以考虑在/usr/local/bin中编写一个名为sonar的shell脚本,然后在添加执行权限。Sonar脚本的内容如下(假设sonar安装在/usr/local目录下);

#!/bin/sh

cd /usr/local/sonar-3.7.4/bin/linux-x86-32/

./sonar.sh "$@"

至此,sonar就安装好了。此时访问http:\localhost:9000时被重定向到http://localhost:9000/maintenance,并且浏览器中输出如下界面:

 

这是由于修改了上面的数据库的原因,使用mysql数据库而不是sonar自带的H2数据库,需要处理升级的问题,并且以后如果迁移数据库或者升级sonar都需要处理下述的步骤。(备注:后来我在一个全新的环境安装了sonar,发现并不需要升级数据库,也能使用,想了很久,才想明白,我在修改sonar的配置之前,先使用过内嵌数据库H2对例子进行运行,后来才修改为mysql,这先后迁移数据库时需要运行setup步骤)

键入http://localhost:9000/setup,单击图中的upgrade按钮,然后等待完成,并重定向到http://localhost:9000,等待时间一般为1分钟。

 

Upgrade结束后,可以看到sonar的启动的界面了如下:

 

5.sonar中文补丁包安装

安装中文补丁包可以通过访问http:\localhost:9000,打开sonar后,进入更新中心安装,或者下载中文补丁包后,放到SONARQUBE_HOME/extensions/plugins目录,然后重启SonarQube服务。

6.设置sonar作为Linux服务并开机自启动

在与持续集成服务器(比如Hudson)进行集成时,需要将sonar设置为Linux服务开机自启动,步骤如下,新建文件/etc/init.d/sonar,输入如下内容:

#!/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 $*

SonarQube开机自启动(Ubuntu,32位),步骤如下,建立软连接,修改文件权限,更新启动项:

sudo ln -s $SONAR_HOME/bin/linux-x86-32/sonar.sh /usr/bin/sonar
sudo chmod 755 /etc/init.d/sonar
sudo update-rc.d sonar defaults
SonarQube开机自启动(RedHat, CentOS, 64位):
sudo ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar
sudo chmod 755 /etc/init.d/sonar
sudo chkconfig --add sonar

2.3. Sonar使用分析Java程序

Sonar可以有多种使用方式,在maven中使用,在ant中使用,在持续集成服务器中使用,以及通过sonar-runner使用。其中最简单的是通过sonar-runner进行使用,下面介绍Sonar-runner的使用方法。本节中使用样例来自sonar官方提供的sonar-examples-master.zip (原来的网站下载速度不敢恭维,为次,创建了百度网盘的连接).

预置条件

已安装Sonar Runner且环境变量已配置,即sonar-runner命令可在任意目录下执行。

下载sonar-examples-master.zip ,解压使用其sonar-examples/projects/languages/java/sonar-runner/java-sonar-runner-simple作为执行运行分析。要对一个项目运行sonar分析,必须在项目源码的根目录下创建sonar-project.properties配置文件,这里以java-sonar-runner-simple下提供的配置文件为例:

# Required metadata

sonar.projectKey=java-sonar-runner-simple

sonar.projectName=Simple Java project analyzed with the SonarQube Runner

sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)

sonar.sources=src

# Language

sonar.language=java

# Encoding of the source files

sonar.sourceEncoding=UTF-8

2.执行分析

cd到java-sonar-runner-simple根目录下,执行命令

# sonar-runner

备注:如果出现EXECUTION FAILURE,可以通过运行sonar-runner -X参看出错的原因,此外,也可以通过查看sonar目录下的logs/sonar.log来查看sonar服务器记录,从而确定错误原因。

Sonar-runner执行成功会输出EXECUTION SUCCESS

SonarQube Runner 2.3

Java 1.6.0_30 Sun Microsystems Inc. (32-bit)

Linux 3.2.0-60-generic-pae i386

INFO: Runner configuration file: /home/xiajian/software/sonarqube/sonar-runner-2.3/conf/sonar-runner.properties

INFO: Project configuration file: /home/xiajian/software/sonarqube/sonar-examples/projects/languages/java/sonar-runner/java-sonar-runner-simple/sonar-project.properties

INFO: Default locale: "en_US", source code encoding: "UTF-8"

INFO: Work directory: /home/xiajian/software/sonarqube/sonar-examples/projects/languages/java/sonar-runner/java-sonar-runner-simple/.sonar

INFO: SonarQube Server 4.2

..........

INFO: ------------------------------------------------------------------------

INFO: EXECUTION SUCCESS

INFO: ------------------------------------------------------------------------

Total time: 10.777s

Final Memory: 10M/299M

INFO: ------------------------------------------------------------------------

此时访问http:\\localhost:9000即可查看分析结果,具体的结果如下(项目比较简单,分析结果不复杂):

除了可以在本地使用localhost访问外,可以通过浏览器和相应的ip地址来访问,此时的地址实例如下: http://222.192.41.13:9000

关于Sonar分析结果的不同参数的意思可以参考如下:

http://docs.codehaus.org/display/SONAR/Analysis+Parameters或者参考[8]的中文介绍。

2.4. Sonar安装的问题

如果不修改sonar配置文件中关于数据库部分的配置,一切运行都很正常。如果在修改sonar配置文件之后,没有upgrade,并在浏览器中输入http:\\localhost:9000,结果被重定向到:http://localhost:9000/maintenance 浏览器中输入如下:

此时,如果对任意项目运行sonar-runner,就会出现如下结果:

SonarQube Runner 2.3

Java 1.6.0_30 Sun Microsystems Inc. (32-bit)

Linux 3.2.0-60-generic-pae i386

....中间省略...

INFO: ------------------------------------------------------------------------

INFO: EXECUTION FAILURE

INFO: ------------------------------------------------------------------------

Total time: 0.176s

Final Memory: 0M/58M

INFO: ------------------------------------------------------------------------

ERROR: Error during Sonar runner execution

java.lang.IllegalStateException: Fail to download libraries from server

at org.sonar.runner.impl.Jars35.dowloadFiles(Jars35.java:78)

。。。。

at org.sonar.runner.impl.Jars35.dowloadFiles(Jars35.java:72)

... 12 more

起初,在google上一圈,一开始怀疑是Mysql数据库的问题(使用自带的H2数据库就没有问题),后来怀疑是Sonar的版本的问题,折腾了一个下午加一个晚上,就这样几个文件改来改去,然后在不断的重复运行,不断的搜索,不断的纠集在一个问题上。后来,认真看了一下http://docs.codehaus.org/display/SONAR/Upgrading,看到升级Sonar后需要http://localhost:9000/setup,然后按照setup的指令来处理。

此外,sonar创建的java进程耗内存以及CPU,并且不正常启动时,Kill不能杀死它,必须使用End Process才能终结该进程。

花了5,6个小时,一直纠结在一个问题,感觉十分不值得,自己又不愿意放弃。获得一个经验,遇到问题时,不要急躁,静下心来,细心仔细的查看出现问题的现场,认真思考出现问题的原因,就会能找到解决问题的方案,不要像无头蝇一样乱飞,费时又费力。

解决问题的灵感站点:http://sonarqube.15.x6.nabble.com/Sonar-went-under-manteinance-td5008852.html

3. Eclipse中关于Sonar

3.1. 关于代码质量分析

3.1.1. Why?

代码质量分析可使代码:更少错误,更可持续性,更可靠,更可读,对新贡献者友好。

3.1.2. How?

Code quality analysis mainly relies on a set of tools that look at your code and give you hints. The most famous tools are Findbugs, PMD, Checkstyle; but also code coverage tools such as Jacoco. JDT itself provides very powerful quality checks, but there are not enabled by default. You should go to Error/Warnings in preferences and replace all "ignore" by "Warning". You can (and should) enable such tools in IDE.

代码质量分析主要依赖一组查看代码并给出线索的工具。著名的工具有Findbugs,PMD,Checkstyle;代码分析工具有诸如Jacocoemmacobertura这样的。JDT自身提供非常强大的质量检查,但默认没有启动。可以在preference菜单中Error/Warnings中将所有ignore替换为Warning。也可以在IDE中启动这样工具。(备注:Eclipse中存在Eclemma,codecover这样代码插桩工具中)

Code quality can also be analyzed out of the IDE, running those tools and using their reports to find out the "hot spots" in your code.

也可在IDE之外分析代码质量,运行上述工具并使用工具的报道来寻找代码中的热点。

3.2. About Sonar

Sonar is an open-source product which is used to gather several metrics about code quality, put them all in a single dashboard, and provide some tips to help you making your code better, more sustainable, more reliable, less bugged.

Sonar是一个收集某些代码质量度量的开放源代码的工具,将度量信息全部放到单个白板(dashboard)上,并提供一些使代码更加好,可靠,持续和更少bug的建议。

Enable Hudson Sonar plugin on your job or running mvn sonar:sonar on your Maven build will result in the following flow of actions:

  • l Sonar will locally analyze code and generate reports from many analyzers
  • l Sonar will push those reports to the Sonar dashboard

在工作中启动Hudson(可扩展的持续集成服务器)的Sonar插件,或者对Maven(一种类似make的java构建工具)构建运行sonar,命令为mvn sonar,将会导致下面的行为:

  • n Sonar局部分析代码并从不同分析器中生成报告
  • n Sonar将这些报告推送到Sonar白板上

3.3. Sonar for Eclipse.org

3.3.1. Usage

Sonar can be found on https://dev.eclipse.org/sonar .  Several projects already have quality reports enabled. You can drill-down on code to  see Sonar annotations  on each  class, or navigate through the different widget on dashboard to focus on dedicated issues

Sonar在https://dev.eclipse.org/sonar。一些项目已经启动了质量报告。可挖掘代码查看Sonar中每个类中的注释,或导航到白板中不同的widget中关注特定的主题。

关于如何在Eclipse中使用Sonar,可以参考[9] Eclipse Sonar Tutorial,其中大体介绍了如何安装Sonar的Eclipse插件之类的,对于如何阅读和使用Sonar主要是参考Sonar的官方文档。

3.3.2. Enable Sonar for your project

See http://mickaelistria.wordpress.com/2012/10/08/sonar-at-eclipse-org/ . You'll need a Tycho-based build, and a Job on Hudson sandbox to be able to push reports to Sonar.

参考http://mickaelistria.wordpress.com/2012/10/08/sonar-at-eclipse-org/。在对项目启动Sonar推送报告,可能需要一个基于Tycho的构建以及Hudson沙箱。

3.3.3. Permissions on Sonar

Sonar is currently (and will remain) public to all, but only an admin can log it. So it's not yet possible to store user preferences or be made an administrator on a project. Follow bug 391343 for more details.

Sonar是对公共开放的,但只有admin可以记录。所以,Sonar也许不能存储用户设置或成为某个项目的管理员,更多信息查看bug 391343。

3.4. Infrastructure

Sonar is installed on a VM accessible from inside Eclipse infrastructure and with hostname sonar. It uses its embedded Jetty server to publish to HTTP, and uses a PostgreSQL database on the same VM.

Sonar安装在可从Eclipse内部架构中访问并命名为主机名的+sonar的VM中。Sonar使用内置的Jetty服务器通过HTTP推送内容,并使用PostgreSQL(Oracle支持伯利克利大学开发的开源数据库)数据库。

The database is made accessible from Eclipse.org servers and has a user for Sonar, and another user for Hudson. When running the Hudson Sonar plugin, the plugin uses this user to push to the Sonar database the metrics about your project.

该数据库可从Eclipse.org的服务器中访问,并为Sonar和Hudson分别建立了两个用户。当运行Hudson Sonar插件时,插件使用Sonar用户向Sonar数据库推送项目度量信息。

3.4.1. Bugzilla

l Open issues: https://bugs.eclipse.org/bugs/buglist.cgi?list_id=6604883&classification=Eclipse%20Foundation&query_format=advanced&component=Sonar&product=Community

l User to follow to get notified of new bugs on Sonar component: [email protected]

3.4.2. Maintenance notes

  • l Database requires to be tweak to add some "GRANT" permissions to the sonar user. Sonar could start otherwise.
  • l 为了给Sonar用户添加一些“GRANT”权限,数据库需要一些调整,然后Sonar才而启动。
  • l March 2013: https://bugs.eclipse.org/bugs/show_bug.cgi?id=407658 . Removed big log file and restarted Sonar,
  • l July 2013: Got an OutOfMemory on Sonar side while running Platform-Sonar job. Increased max memory in conf/wrapper.conf and restarted Sonar.
  • l July 2013: No space left on device. A lot of big memory dumps files in bin/linux-x86-64 consumed half of disk space. Removed them
  • l October 2013: Migration to SonarQube 3.7.1 to provide compatibility with Maven
  • https://bugs.eclipse.org/bugs/show_bug.cgi?id=417978 basic migration
  • https://bugs.eclipse.org/bugs/show_bug.cgi?id=418502 Issue with HTTPS
  • n org.postgresql.util.PSQLException: ERROR: permission denied for relation permission_templates => Give permissions to hudson user with psql GRANT SELECT ON ALL TABLES IN SCHEMA public TO user; GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO user;
  • l Reboot: INFO | jvm 1 | 2014/01/24 06:06:27 | java.lang.OutOfMemoryError

小结

收集了这么多关于Sonar的信息,我突然想到王垠大神写的开源工具PySonar命名的含义了,Python的Sonar工具。Sonar原来这么NB的工具,只怪我太孤陋寡闻。

学习配置并使用sonar,花费了将近两三天的时间,觉得效率有点低。

参考文献

[1]SonarQube代码质量管理平台安装与使用

[2]Code Quality Tools Review for 2013: Sonar, Findbugs, PMD and Checkstyle http:// zeroturnaround.com/rebellabs/code-quality-tools-review-for-2013-sonar-findbugs-pmd-and-checkstyle/

[3]eclipse中关于Sonar的简介 https://wiki.eclipse.org/Sonar

[4]wikipedia中关于Sonar的简介 http://en.wikipedia.org/wiki/SonarQube

[5]Sonar官网:http://www.sonarqube.org

[6]oschina中关于sonar的介绍:http://www.oschina.net/question/tag/sonar

[7]SonarQuake官方文档:http://docs.codehaus.org/display/SONAR/Documentation

[8]Sonar实战 http://wenku.baidu.com/view/f1306661ccbff121dd3683d9.html

[9] Eclipse Sonar Tutorial 

猜你喜欢

转载自my.oschina.net/u/2548090/blog/1334994
今日推荐