sonar代码质量检测工具的配置和使用

背景:我们一直在重构,调整架构,但是一直不知道我们做的效果如何,也没有办法量化,所以需要引入一个工具和标准来量化我们的工作。

简介:sonarqube是一个开源的,代码质量检测的工具,我们可以使用它来检测我们的代码。

如何安装:

1.到官网下载安装文件http://www.sonarqube.org/downloads/

2.将安装包解压到指定目录

进入以下目录:

/Users/xiepengchong/opensource/sonarqube-5.6.3/bin/macosx-universal-64

执行:

./sonar.sh start

即可启动该功能。


如何使用:

下载sonarqube runner

http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

配置sonar-runner的环境变量

然后进入我们需要执行检查的工程下面。

新建一个名字是 sonar-project.properties的文件。(内容如下)

 

sonar.projectKey=UCarNew
sonar.projectName=UCarNew
sonar.projectVersion=5.0
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=./

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
sonar.my.property=value


最后直接运行:

sonar-runner

就可以进行检查了,

如何查看结果:

检查有如下日志: 

15:08:46.758 WARN  - This may lead to missing/broken features in SonarQube

15:08:46.758 INFO  - Sensor SCM Sensor (done) | time=21631ms

15:08:46.758 INFO  - Sensor Zero Coverage Sensor

15:08:46.962 INFO  - Sensor Zero Coverage Sensor (done) | time=204ms

15:08:46.962 INFO  - Sensor Code Colorizer Sensor

15:08:46.977 INFO  - Sensor Code Colorizer Sensor (done) | time=15ms

15:08:46.977 INFO  - Sensor CPD Block Indexer

15:08:46.978 INFO  - JavaCpdBlockIndexer is used for java

15:08:47.976 INFO  - Sensor CPD Block Indexer (done) | time=999ms

15:08:48.120 INFO  - Calculating CPD for 1179 files

15:08:48.432 INFO  - CPD calculation finished

15:08:50.380 INFO  - Analysis report generated in 1556ms, dir size=14 MB

15:08:53.133 INFO  - Analysis reports compressed in 2753ms, zip size=6 MB

15:08:53.357 INFO  - Analysis report uploaded in 224ms

15:08:53.358 INFO  - ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/UCarNew

15:08:53.358 INFO  - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report

15:08:53.358 INFO  - More about the report processing at http://localhost:9000/api/ce/task?id=AVgPRQzFP91kwbiuXjf5

打开网址,可以看到如下页面。


通过切换不同的tab,可以查看详细信息。


最后说一下如何提高我们的代码质量:

1.恰当的使用if,switch等判断逻辑的使用。因为每一个逻辑的判断都是一个复杂度的引入,

2.减少重复的代码,每一段重复的代码将会造成维护工作量的double,

3.减少每个文件的大小,文件的大小与维护成本成指数级别增加。


PS:时隔几天之后,启动的时候,突然不能用了。

wudideMacBook-Pro:bin xiepengchong$ ./sonar-runner -X

SonarQube Runner 2.4

Java 1.8.0_101 Oracle Corporation (64-bit)

Mac OS X 10.10.5 x86_64

INFO: Error stacktraces are turned on.

INFO: Runner configuration file: /Users/xiepengchong/opensource/sonar-runner-2.4/conf/sonar-runner.properties

INFO: Project configuration file: NONE

INFO: Default locale: "zh_CN", source code encoding: "UTF-8" (analysis is platform dependent)

INFO: Work directory: /Users/xiepengchong/opensource/sonar-runner-2.4/bin/./.sonar

ERROR: Sonar server 'http://localhost:9000' can not be reached

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

INFO: EXECUTION FAILURE

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

Total time: 0.032s

Final Memory: 2M/245M

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

ERROR: Error during Sonar runner execution

以为是版本的问题,后来发现是我启动的不同

/Users/xiepengchong/Downloads/sonarqube-5.6.3/bin/macosx-universal-64

在这个路径下,启动,就解决了,真是很奇怪,我以为路径没关系呢。


最近(2017年12.4日)再次未新公司的代码做检查的时候,在linux服务器上搭建了一个环境,(sonarqube-6.7.zip)

发现竟然报错了

Please provide compiled classes of your project with sonar.java.binaries property
之后又在配置文件中增加了一下路径:

sonar.projectKey=PowerPlus
sonar.projectName=PowerPlus
sonar.projectVersion=1.0
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=./

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
sonar.my.property=value
sonar.java.binaries=app/build/intermediates/classes


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值