4

持续集成实战三——持续代码质量管理Sonar部署

 3 years ago
source link: https://chegva.com/1192.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

持续集成实战三——持续代码质量管理Sonar部署

持续代码质量管理Sonar部署实践

1 Sonar简介及部署

Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。

在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
  此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。

2 Sonar实用操作

  • 启动Sonar,登陆密码默认为admin/admin
    [root@linux-node1 ~]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
    Starting SonarQube...
    Started SonarQube.

2 Sonar代码分析

2.1 Sonar代码分析原理

  • 通过下载语言jar包插件,使用扫描器Sonar Scanner扫描器来实现代码扫描

[root@linux-node1 src]# unzip sonar-scanner-2.6.1.zip    #解压下载好的扫描器
[root@linux-node1 src]# mv sonar-scanner-2.6.1 /usr/local/
[root@linux-node1 src]# ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scanner

2.2 配置Sonar Scanner扫描器关联

[root@linux-node1 /usr/local/sonar-scanner/conf]# grep  ^[a-z] sonar-scanner.properties 
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

2.3 下载官方语言代码测试包进行测试

[root@linux-node1 ~]# cd sonar-examples-master/projects/languages/php/    #进入php语言的测试目录
[root@linux-node1 ~/sonar-examples-master/projects/languages/php]# ll
total 0
drwxr-xr-x 4 root root 97 Oct  4 03:10 php-sonar-runner
drwxr-xr-x 5 root root 89 Aug 14 02:12 php-sonar-runner-unit-tests
[root@linux-node1 ~/sonar-examples-master/projects/languages/php]# cd php-sonar-runner
[root@linux-node1 ~/sonar-examples-master/projects/languages/php/php-sonar-runner]# ll
total 12
-rw-r--r-- 1 root root 453 Aug 14 02:12 README.md                    #测试使用说明
-rw-r--r-- 1 root root 331 Aug 14 02:12 sonar-project.properties     #测试定义文件,没有不能测
drwxr-xr-x 2 root root  21 Aug 14 02:12 src                          #测试源码包
-rw-r--r-- 1 root root 272 Aug 14 02:12 validation.txt
#在当前目录下调用扫描器文件执行测试
[root@linux-node1 ~/sonar-examples-master/projects/languages/php/php-sonar-runner]# /usr/local/sonar-scanner/bin/sonar-scanner 
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /root/sonar-examples-master/projects/languages/php/php-sonar-runner/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_101 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-327.18.2.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global repositories
INFO: Load global repositories (done) | time=176ms

再用java语言同样操作一次,可在web端查看报告

3 最终效果图,可以自定义仪表盘显示关注的信息

anzhihe安志合个人博客,版权所有丨 如未注明,均为原创 丨转载请注明转自:https://chegva.com/1192.html | ☆★★每天进步一点点,加油!★★☆

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK