一、简介
1.1 什么是Sonar
Sonar是一个用于代码质量管理的开源平台,用于管理代码的质量,是一个Web系统,展现了静态代码扫描的结果,
通过插件形式可以支持二十几种语言的代码质量检测,通过多个维度的检查了快速定位代码中潜在的或者明显的错误;
① 统一各个小组内部开发者的编码风格(编码规范)
② 消除过于复杂的代码逻辑
③ 消除重复代码与硬编码
④ 消除违反规定的设计
⑤ 提升代码注释质量
官网:https://www.sonarqube.org
文档:https://docs.sonarqube.org/latest
中文参考文档:http://www.sonar.org.cn/
1.2 Sonar工作原理
Sonar其实是一个Web系统,SonarQube对Sonar进行支持,Sonar可以在网页上展示静态代码的检测结果,检测各种代码语言的规则我们可以自定义,通过Sonar-Scanner这个工具进行代码检测;
从服务器请求数据,分析提供给分析的文件,并将结果数据以报告的形式发送回服务器,然后在服务器端进行异步分析。
1. 用户本地使用IDE的插件进行代码分析
2. 用户上传到源代码版本控制服务器
3. 持续集成,使用Sonar Scanner进行扫描
4. 将扫描结果上传到SonarQube服务器
5. SonarQube server将结果写入db
6. 用户通过web ui查看扫描结果
7. SonarQube导出结果到其他需要的服务
1.3 Sonar检测维度
Sonar可以从七个维度进行代码质量检测,我们可以根据不同维度的严重性然后根据我们的经验做出相应的代码优化,当然并不是所有维度我们都有必要代码修改;
①代码规范
Sonar可以通过PMD、CheckStyle、Findbugs等代码规则检测工具来检测我们代码是否符合代码规范;
②潜在的缺陷
Sonar可以通过PMD、CheckStyle、Findbugs等代码规则检测工具来检测我们代码是否有代码缺陷(比如空指针是否有判断、IO流是否有关闭等);
③糟糕的复杂度分布
文件、类、方法等,如果复杂度过高将难以改变,这会使得开发人员难以理解它们 且如果没有自动化的单元测试,对于程序中的任何组件的改变都将可能导致需要全面的回归测试
④重复代码
程序中包含大量复制粘贴的代码是质量低下的,sonar可以展示源码中重复严重的地方
⑤注释的检测
没有注释将使代码可读性变差,特别是当不可避免地出现人员变动时,程序的可读性将大幅下降 而过多的注释又会使得开发人员将精力过多地花费在阅读注释上,亦违背初衷
⑥单元测试
sonar可以很方便地统计并展示单元测试覆盖率
⑦糟糕的设计
通过sonar可以找出循环,展示包与包、类与类之间相互依赖关系,可以检测自定义的架构规则 通过sonar可以管理第三方的jar包,可以利用LCOM4检测单个任务规则的应用情况, 检测耦合。
二、使用配置
2.1 jhipster 生成工程配置
在工程目录下修改sonar-project.properties配置
主要要修改配置 projectKey、projectName、url
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #projectKey项目的唯一标识,不能重复、不能有空格 sonar.projectKey=financing-develop #projectKey项目名称 sonar.projectName=financing develop sonar.projectVersion=1.0 sonar.sources=src/main/ sonar.host.url=xxxxxxxxxxxxxx sonar.tests=src/test/ sonar.jacoco.reportPaths=target/jacoco/test.exec,target/jacoco/integrationTest.exec sonar.java.codeCoveragePlugin=jacoco sonar.junit.reportPaths=target/test-results/test,target/test-results/integrationTest sonar.sourceEncoding=UTF-8 sonar.exclusions=src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.* sonar.issue.ignore.multicriteria=S3437,S4502,S4684,UndocumentedApi # Rule https://sonarcloud.io/coding_rules?open=squid%3AS3437&rule_key=squid%3AS3437 is ignored, as a JPA-managed field cannot be transient sonar.issue.ignore.multicriteria.S3437.resourceKey=src/main/java/**/* sonar.issue.ignore.multicriteria.S3437.ruleKey=squid:S3437 # Rule https://sonarcloud.io/coding_rules?open=squid%3AUndocumentedApi&rule_key=squid%3AUndocumentedApi is ignored, as we want to follow "clean code" guidelines and classes, methods and arguments names should be self-explanatory sonar.issue.ignore.multicriteria.UndocumentedApi.resourceKey=src/main/java/**/* sonar.issue.ignore.multicriteria.UndocumentedApi.ruleKey=squid:UndocumentedApi # Rule https://sonarcloud.io/coding_rules?open=squid%3AS4502&rule_key=squid%3AS4502 is ignored, as for JWT tokens we are not subject to CSRF attack sonar.issue.ignore.multicriteria.S4502.resourceKey=src/main/java/**/* sonar.issue.ignore.multicriteria.S4502.ruleKey=squid:S4502 # Rule https://sonarcloud.io/coding_rules?open=squid%3AS4684&rule_key=squid%3AS4684 sonar.issue.ignore.multicriteria.S4684.resourceKey=src/main/java/**/* sonar.issue.ignore.multicriteria.S4684.ruleKey=squid:S4684 |
2.2 不是jhipster 生成的单模块工程配置
①在工程目录下修改sonar-project.properties配置
主要要修改配置 projectKey、projectName、url
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #projectKey项目的唯一标识,不能重复 sonar.projectKey=sonar-demo-develop #projectKey项目名称 sonar.projectName=sonar-demo develop sonar.projectVersion=1.0 sonar.sources=src/main/ sonar.host.url=xxxxxxxxxxxxxx sonar.tests=src/test/ #sonar.jacoco.reportPaths=target/jacoco/test.exec,target/jacoco/integrationTest.exec sonar.jacoco.reportPaths=target/jacoco/test.exec,target/jacoco.exec sonar.java.codeCoveragePlugin=jacoco #sonar.junit.reportPaths=target/test-results/test,target/test-results/integrationTest sonar.sourceEncoding=UTF-8 sonar.exclusions=src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.* |
②在maven pom 文件新增
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > < plugin > < groupId >org.codehaus.mojo</ groupId > < artifactId >properties-maven-plugin</ artifactId > < version >1.0.0</ version > < executions > < execution > < phase >initialize</ phase > < goals > < goal >read-project-properties</ goal > </ goals > < configuration > < files > < file >sonar-project.properties</ file > </ files > </ configuration > </ execution > </ executions > </ plugin > </ plugins > </ build > < profiles > < profile > < id >coverage</ id > < activation > < activeByDefault >true</ activeByDefault > </ activation > < build > < plugins > < plugin > < groupId >org.jacoco</ groupId > < artifactId >jacoco-maven-plugin</ artifactId > < executions > < execution > < id >prepare-agent</ id > < goals > < goal >prepare-agent</ goal > </ goals > </ execution > < execution > < id >report</ id > < goals > < goal >report</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > </ profile > </ profiles > |
2.5 本地idea运行命令提交
1 | mvn clean verify sonar:sonar -Dmaven.test.failure.ignore=true |
2.6 jenkins Pipeline script 配置提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | pipeline { agent any tools { maven 'mvn-3.3.9' jdk 'jdk-1.8' } stages { stage('Checkout') { steps { git branch:'20190723_dev', url: 'git@internal.git.taoqicar.com:cicd/dingtalk-robot-outgoing.git' } } stage('SonarQube analysis') { steps{ sh 'mvn clean verify sonar:sonar -Dmaven.test.failure.ignore=true' } } } post { success { sh "curl -G -d 'projectKeys=eam-develop&gitProjectName=eam&gitBranch=develop&gitToken=yS3_ZdQ_FMrx9Erhdq1G&dingDingToken=a837396680bb701220353c3f2bdc2c454642dedf9b03c97c5e5a6d1a2a756ea7' http://api.xxfcar.com:3080/dingtalk/v1-0/sonar/bug/statistics/dingding" } } } |
在IDEA安装SonarLint插件的步骤和使用方法
1、安装SonarLint插件方式


2.使用方式

3.效果
