测试框架:使用SONAR分析代码质量

介绍

Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量。经过插件机制,Sonar 能够集成不一样的测试工具,代码分析工具,以及持续集成工具,好比pmd-cpd、checkstyle、findbugs、Jenkins。经过不一样的插件对这些结果进行再加工处理,经过量化的方式度量代码质量的变化,从而能够方便地对不一样规模和种类的工程进行代码质量管理。同时 Sonar 还对大量的持续集成工具提供了接口支持,能够很方便地在持续集成中使用 Sonar。此外,Sonar 的插件还能够对 Java 之外的其余编程语言提供支持,对国际化以及报告文档化也有良好的支持。 html

 

SONAR安装&运行mysql

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

 

运行:解压后,根据平台运行bin下不一样目录下的启动脚本。对于linux x86_64,运行bin/linux-x86-64/sonar.sh。sql

可用命令:数据库

./sonar.sh { console | start | stop | restart | status | dump }编程

 

安装插件:浏览器

SONAR中文包:http://docs.codehaus.org/display/SONAR/Chinese+Pack服务器

将插件放置在${SONARHOME}/extensions/plugins下,重启sonar后生效。注意版本号要匹配。本例中,SonarQube版本为4.4,因此选择插件版本为1.8的。maven

  

SONAR + Maven分析代码质量tcp

1)设置sonar使用的数据库信息。

本例设置sonar使用mysql数据库存储分析数据。保存设置后,执行restart使其生效。

 

${SONARHOME}/conf/sonar.properties:

 
  1. # Permissions to create tables, indices and triggers must be granted to JDBC user.
  2. # The schema must be created first.
  3. sonar.jdbc.username=root
  4. sonar.jdbc.password=root
  5. # Comment the following line to deactivate the default embedded database.
  6. #sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
  7. #----- MySQL 5.x
  8. # Comment the embedded database and uncomment the following line to use MySQL
  9. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

 

2)须要在Maven的settings.xml设置sonar信息。

其中<sonar.host.url>http://localhost:9000</sonar.host.url>指明了sonar服务器的地址。因此在执行maven命令的时候,<sonar.host.url>指明的服务器必须已运行起来。

 

${MAVEN_HOME}/conf/settings.xml:

 
  1. <profiles>
  2.     <profile>
  3. <id>sonar</id>
  4. <properties>
  5. <sonar.jdbc.url>jdbc:mysql://192.168.198.128:3306/sonar</sonar.jdbc.url>
  6. <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
  7. <sonar.jdbc.username>root</sonar.jdbc.username>
  8. <sonar.jdbc.password>root</sonar.jdbc.password>
  9. <sonar.host.url>http://localhost:9000</sonar.host.url> <!-- Sonar服务器访问地址 -->
  10. </properties>
  11.     </profile>
  12. </profiles>
  13. <activeProfiles>     <activeProfile>sonar</activeProfile>
  14. </activeProfiles>

3)执行mvn sonar:sonar命令进行代码分析。

咱们能够在Eclipse中,对一个标准maven工程执行sonar。说明:因为maven对sonar有很好的支持,会自动执行相应的脚本,因此无需在pom中添加sonar说明。

在执行maven进行sonar分析以前,必须确保sonar服务器已经处于运行状态。本例中sonar服务器运行在localhost:9000上。

 

首先,执行sonar:sonar命令,最后获得输出以下输出。若是输出”BUILD SUCCESS“说明已经构建成功。

而后,咱们能够在浏览器查看分析结果。

 

查看分析结果

对于使用sonar自带服务器来讲,在浏览器访问:http://sonar_ip:9000,打开sonar结果页面。可以使用admin/admin帐号登陆进入。

1)home页

下面是home页,右边PROJECTS页面列出了全部的工程。点击红色框内的连接,能够查看详细状况。

 

2)工程总面板视图

Dashboard包含了不少信息,好比程序统计信息、问题统计信息、技术债务、代码复杂度、单元测试覆盖度等。

 

3)Hotspots热点区

在热点区,能够查看比较主要(hot)的信息。

 

4)问题视图

点击左侧导航树的“问题”,打开问题视图页。经过点击问题数,以下红框所示,能够查看具体问题。

 

点击问题数后,进入具体问题页。SonarQube容许管理员对问题进行从新确认,好比能够认为一个打开的问题是误判的。

 

下面是认为一个问题是误判后的状况。

 

在问题页面,能够经过“状态”搜索问题。下面是搜索“误判”问题的结果。

 

5)技术债务

这里列出了修复问题所须要的时间,所谓技术债务。出来混总要还的,遗留的问题越多,技术债务越大。

 

6)问题明细

这里列出问题明细,包括问题严重级别,对应的问题数量,问题的描述。

 

结合Jenkins

能够将SONAR服务器放置在任意master或者slave节点上,在进行sonar分析时,必须在maven的conf/settings.xml中配置sonar服务器信息。而后就能够在jenkins中进行sonar分析了。

有两种方法使jenkins与sonar结合:一种就是上面介绍的经过maven(jenkins -maven - sonar),另一种是直接在jenkins中调用sonar。

 

参考:

SONAR+MAVEN项目质量管理:http://www.linxiaosheng.com/post/2013-12-06/40060353952

Jenkins + sonar持续代码审查:http://www.cnblogs.com/gao241/p/3190701.html

SONAR介绍: http://www.cnblogs.com/gao241/p/3190701.html

相关文章
相关标签/搜索