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

时间 : 15-01-16 栏目 : CI, 集成测试 作者 : meiw@z3a105.com 评论 : 0 点击 : 314 次

Sonar简介

Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从七个维度检测代码质量

通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二十几种编程语言的代码质量管理与检测

sonarQube能带来什么?

Developers' Seven Deadly Sins
1.糟糕的复杂度分布
文件、类、方法等,如果复杂度过高将难以改变,这会使得开发人员难以理解它们,

  且如果没有自动化的单元测试,对于程序中的任何组件的改变都将可能导致需要全面的回归测试

2.重复
显然程序中包含大量复制粘贴的代码是质量低下的

  sonar可以展示源码中重复严重的地方

3.缺乏单元测试

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

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

  sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具检测出潜在的bug

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

关于Spaghetti Design:http://docs.codehaus.org/display/SONAR/Spaghetti+Design

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

SonarQube安装

预置条件
1.已安装JAVA环境
2.已安装有MySQL数据库

软件下载地址:http://www.sonarqube.org/downloads/
下载SonarQube与SonarQube Runner
中文补丁包下载:http://docs.codehaus.org/display/SONAR/Chinese+Pack

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;

2.安装sonar与sonar-runner
将下载的sonar-3.7.zip包解压至Linux某路径如/usr/local
将下载的sonar-runner-dist-2.3.zip包解压某路径/usr/local
添加SONAR_HOME、SONAR_RUNNER_HOME环境变量,并将SONAR_RUNNER_HOME加入PATH

修改sonar配置文件
编辑<install_directory>/conf/sonar.properties文件,配置数据库设置,默认已经提供了各类数据库的支持
这里使用mysql,因此取消mysql模块的注释
#vi sonar.properties

 

  1. sonar.jdbc.username:                       sonar  
  2. sonar.jdbc.password:                       sonar  
  3. sonar.jdbc.url:                            jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true  
  4.   
  5. # Optional properties  
  6. sonar.jdbc.driverClassName:                com.mysql.jdbc.Driver  

修改sonar-runner的配置文件
切换至sonar-runner的安装目录下,修改sonar-runner.properties
根据实际使用数据库情况取消相应注释
 

 

  1. #Configure here general information about the environment, such as SonarQube DB details for example  
  2. #No information about specific project should appear here  
  3. #----- Default SonarQube server  
  4. sonar.host.url=http://localhost:9000  
  5. #----- PostgreSQL  
  6. #sonar.jdbc.url=jdbc:postgresql://localhost/sonar  
  7. #----- MySQL  
  8. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8  
  9. #----- Oracle  
  10. #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE  
  11. #----- Microsoft SQLServer  
  12. #sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor  
  13. #----- Global database settings  
  14. sonar.jdbc.username=sonar  
  15. sonar.jdbc.password=sonar  
  16. #----- Default source code encoding  
  17. sonar.sourceEncoding=UTF-8  
  18. #----- Security (when 'sonar.forceAuthentication' is set to 'true')  
  19. sonar.login=admin  
  20. sonar.password=admin  

 

3.添加数据库驱动
除了Oracle数据库外,其它数据库驱动都默认已经提供了,且这些已添加的驱动是sonar唯一支持的,因此不需要修改
如果是Oracle数据库,需要复制JDBC驱动至<install_directory>/extensions/jdbc-driver/oracle目录
4.启动服务
目录切换至sonar的<install_directory>/bin/linux-x86-64/目录,启动服务
#./sonar.sh start   启动服务
#./sonar.sh stop    停止服务
#./sonar.sh restart 重启服务

至此,sonar就安装好了
访问http:\\localhost:9000即可

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

 

sonar作为Linux服务并开机自启动
新建文件/etc/init.d/sonar,输入如下内容:

 

  1. #!/bin/sh  
  2. #  
  3. # rc file for SonarQube  
  4. #  
  5. # chkconfig: 345 96 10  
  6. # description: SonarQube system (www.sonarsource.org)  
  7. #  
  8. ### BEGIN INIT INFO  
  9. # Provides: sonar  
  10. # Required-Start: $network  
  11. # Required-Stop: $network  
  12. # Default-Start: 3 4 5  
  13. # Default-Stop: 0 1 2 6  
  14. # Short-Description: SonarQube system (www.sonarsource.org)  
  15. # Description: SonarQube system (www.sonarsource.org)  
  16. ### END INIT INFO  
  17.    
  18. /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

 

使用SonarQube Runner分析源码

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

1.在项目源码的根目录下创建sonar-project.properties配置文件
以android项目为例:

 

  1. sonar.projectKey=android-sonarqube-runner  
  2. sonar.projectName=Simple Android project analyzed with the SonarQube Runner  
  3. sonar.projectVersion=1.0  
  4. sonar.sources=src  
  5. sonar.binaries=bin/classes  
  6. sonar.language=java  
  7. sonar.sourceEncoding=UTF-8  
  8. sonar.profile=Android Lint  

 

注:要使用Android Lint
规则分析需要先访问http:\\localhost:9000更新中心添加Android Lint插件,使其可以分析Android Lint规则

2.执行分析
切换到项目源码根目录,执行命令
# sonar-runner
分析成功后访问http:\\localhost:9000即可查看分析结果

不同参数的意思:
http://docs.codehaus.org/display/SONAR/Analysis+Parameters
不同项目的源码分析示例下载:
https://github.com/SonarSource/sonar-examples/zipball/master

 

与IDE关联

最后,当然了,得与IDE相关联,才能更方便地实时查看

以Eclipse为例,请见:http://docs.sonarqube.org/display/SONAR/SonarQube+in+Eclipse

附:

sonarQube官网地址:http://www.sonarqube.org/
sonarQube官方文档地址:http://docs.codehaus.org/display/SONAR/Documentation
sonarQube示例地址:http://nemo.sonarqube.org/

网上另两篇相关的文章:http://www.cnblogs.com/gao241/p/3190701.html
http://www.myexception.cn/open-source/1307345.html

 

 转至http://blog.csdn.net/hunterno4/article/details/11687269

本文标签

除非注明,文章均为( meiw@z3a105.com )原创,转载请保留链接: http://blog.z3a105.com/?p=399

SonarQube代码质量管理平台安装与使用:等您坐沙发呢!

发表评论





       ==QQ:122320466==

 微信    QQ群

推广

  • 104726drf320ff09r7i62f.jpg
  • 103217vn3333zbk3snwz2u.jpg

0

Aujourd’hui, une partie avec le développement du e-commerce, achats en ligne est devenu une partie de la vie pour beaucoup de gens. La mariage a commencé achats en ligne. Si vous choisissez achats les mariages en ligne, il peut être beaucoup moins cher que le salon de la Robe de mariée pas chermariée local, réduisant le budget de mariage. vous pouvez avoir beaucoup de choix si acheter de mariage en ligne. vous pouvez ramasser une robe de mariée bon marché sur Internet.
Piercing fascinerande figur, och nu tittar vi på 2016 senast brudklänning, kan du vara den vackraste bruden det!2016 senaste Bra brudklänning, söt temperament Bra design, romantiska spetsar blomma kjol, som du lägger till en elegant och charmig temperament.Kvinnan tillbaka mjuka linjer, människor brudklänningofta få en känsla av oändlig frestelse. Fall 2016 mässan, lämnar uppgifter om ditt bröllop charmig.
Yesterday afternoon, the Chinese team was training in the Guangzhou Gymnasium, when the reporter asked Zhao Yunlei the feeling of wearing the new cheap jersey , cheap jerseys online shopshe readily took a shirt from the bag crumpled ball to reporters, and she said with a smile: ” This shirt is light. ”Zhao Yunlei said: “Our material is very light like with the clothes of the tennis King Nadal, Federer, after the sweat, sweat does not drip down to the ground, when we do move, it is easy pace slipping if the sweat drip on the floor.”Tennis players Zhang Yawen, told reporters: “You might think the clothes attached to the body, fearing we swing will be affected, in fact, we do not feel anything, because the clothes are very light, very soft, put on quite comfortable. And it’s particularly good clothes to dry, washing and will dry in 15 minutes. ”
China’s sports enthusiasts NFL sweatshirt with mad love and the pursuit of, and therefore, NFL jerseys have a good market in China and development. China is a populous country, is the consumer, the economic momentum is so good, the sales prospects sportswear is immeasurable. With hot sales sweatshirt, but also to promote the importance of sports fans, on health, on the other hand is a matter of concern for the World Cup, fans wearing NFL jerseys and also can express themselves more fully love and obsession Therefore, NFL jerseys Wholesale jerseys online shopwholesale has good prospects and development in China.
ANTA-ANTA Sports Products Limited, referred to as ANTA Sports, Anta, is China’s leading sporting goods companies, mainly engaged in the design, development, manufacture and marketing of ANTA brand sporting goods, including sports footwear, apparel and accessories. Anta sweatshirt design advantages, warm stretch knit fabric, using Slim version of model, more personal fit, bid farewell to bloated, so wearing more stylish.GUIRENNIAO-This logo is a spiritual totem, smooth graphics implication unstoppable force; flexible deliver an elegant arc Wholesale jerseys china shop movement, strength and speed of the United States, a symbol of passion and rationality publicity “Heart” and “meaning”, “concept” unity; pass the fearless and enterprising mind, showing beyond the realm of self, to unstoppable force to create the future.XTEP-Xtep (China) Co., Ltd. is a comprehensive development wholesale jerseys china shop, production and marketing of Xtep brand (XTEP) sports shoes, clothing, bags, caps, balls, socks mainly large sporting goods industry enterprises.
There are a lot of fans in identifying the authenticity of the above cheap jerseys have great distress, so here to i will show you some methods to definitely affordable inexpensive cheap jerseys : Firstly, we should look at if it is working fine. China has been called the world’s factory, a lot cheap jerseys factories in China have foundries, but our cheap jerseys are all from here! Secondly, should to see whether it is the , we all know that it is difficult to get out of print once a genuine cheap cheap jerseys free shipping jersey was print. and we have all kind of stocka on the whole website, in other words, we have all you want ! Finally, look at the price, our price is not necessarily the lowest in the whole website but it must be most fair on the whole website, we certainly you will not regret later when you buy it. Of course, except that cheap jerseys, we also have the other products, such as socks, leggings and some other related products, everyone can enjoy the best services of here!