代码质量检测工具 SONARQUBE7.6 配置和使用(Windows)
Java8、MySQL5.7、maven3.5(非必须)
sonarqube-7.6下载地址
https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.6.zip
sonar-scanner-cli-4.2.0.1873下载地址
https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873.zip
二、创建数据库
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 'sonar' IDENTIFIED BY 'sonar'; GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar'; GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar'; FLUSH PRIVILEGES;
三、配置sonarqube
解压下载的两个zip包到本地,进入sonarqube-7.6\bin\windows-x86-64\双击StartSonar.bat 进行启动
访问http://localhost:9000/ 登录admin/admin
配置sonarqube-7.6\conf\sonar.properties
sonar.search.port=9008 sonar.jdbc.url=jdbc:mysql://192.168.1.50:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=root sonar.jdbc.password=root sonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin
配置sonar-scanner-4.2.0.1873\conf\sonar-scanner.properties
# 这个sonarqube 不需要你在数据库创建 你只需要对应好数据库帐号密码和连接即可 sonar.jdbc.url=jdbc:mysql://192.168.1.50:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false sonar.jdbc.username=root sonar.jdbc.password=root sonar.sourceEncoding=UTF-8
配置系统环境变量
变量名:SONAR_RUNNER_HOME
变量值:D:\devTools\sonar-scanner-4.2.0.1873
path中添加 %SONAR_RUNNER_HOME%\bin
执行 sonar-scanner -v 会有以下提示
INFO: Scanner configuration file: D:\devTools\sonar-scanner-4.2.0.1873\bin\..\conf\sonar-scanner.properties INFO: Project root configuration file: NONE INFO: SonarQube Scanner 4.2.0.1873 INFO: Java 1.8.0_162 Oracle Corporation (64-bit) INFO: Windows 10 10.0 amd64
在本地maven配置文件 <profiles/>节点中添加(没有maven则忽略)
<profiles> <profile> <id>sonar</id> <properties> <sonar.jdbc.url>jdbc:mysql://192.168.1.50:3306/sonar</sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.jdbc.username>root</sonar.jdbc.username> <sonar.jdbc.password>root</sonar.jdbc.password> <sonar.host.url>http://localhost:9000</sonar.host.url> <!-- Sonar服务器访问地址 --> </properties> </profile> </profiles> <activeProfiles> <activeProfile>sonar</activeProfile> </activeProfiles>
四、执行项目解析
MAVEN项目:在项目根目录执行
mvn sonar:sonar
非MAVEN项目(或本地开发环境不全的(比如说,没有maven))
在要分析的项目根目录新建 sonar-project.properties
# 因为可能会检测多个项目 所以这个标识就是唯一标识key 你自己定义 这里我定义的是manage sonar.projectKey=designer # 项目名,我用了和key一样的名字 sonar.projectName=designer #版本 随便定义 sonar.projectVersion=1.0 # Comma-separated paths to directories with sources (required) # 根目录一般指的是pom.xml 同一个目录下 # 这个是要检测的文件路径一般是根目录比如说D盘test项目那么就是D:/test # 检测到哪个就指定到哪里,检测全部也可以。因为这个工具也可以过滤,指定的文件js或java或css等 sonar.sources=D:/devTools/EPROS_HIK_2/HIK2_3.0/EprosDesigner3.0/src/epros #这个是classes目录 也是必须填写的。 sonar.java.binaries=D:/devTools/EPROS_HIK_2/HIK2_3.0/EprosDesigner3.0/bin/epros # Language sonar.language=java # Encoding of the source files sonar.sourceEncoding=UTF-8 #如果是git项目 就加入下面配置 没有就不加 #sonar.scm.provider=git
在当前目录下打开cmd命令行
执行
sonar-scanner
看到类似下面的则表示成功了
INFO: 56 files had no CPD blocks INFO: Calculating CPD for 775 files INFO: CPD calculation finished INFO: Analysis report generated in 1933ms, dir size=12 MB INFO: Analysis report compressed in 4130ms, zip size=5 MB INFO: Analysis report uploaded in 359ms INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=designer INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AXrh6OHHiywI-QZg_dgp INFO: Analysis total time: 3:02.744 s INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 3:05.757s INFO: Final Memory: 68M/3115M INFO: ------------------------------------------------------------------------
五、插件下载
汉化插件
https://github.com/xuhuisheng/sonar-l10n-zh/releases
下载对应版本的插件 sonar-l10n-zh-plugin-1.26.jar,
pdf导出插件
下载对应版本的pdf导出插件https://gitee.com/zzulj/sonar-pdf-plugin
sonar-pdfreport-plugin-3.0.3.jar
将插件复制到sonarqube-7.6\extensions\plugins
配置 → PDF-Report ,设置用户名密码
然后在sonarqube-7.6\conf\sonar.properties中添加
sonar.pdf.password=用户名 sonar.pdf.username=密码
然后再执行解析
=========================================================================================================================
解析遇到问题
15:25:14.167 DEBUG: Getting relative path from SCM root is not supported by svn provider
15:25:14.167 DEBUG: Getting revision id is not supported by svn provider
15:25:15.995 INFO: Analysis report generated in 1699ms, dir size=12 MB
15:25:20.029 INFO: Analysis report compressed in 4033ms, zip size=5 MB
15:25:20.029 INFO: Analysis report generated in D:\devTools\EPROS_HIK_2\HIK2_3.0\EprosDesigner3.0\.scannerwork\scanner-report
15:25:20.029 DEBUG: Upload report
15:25:20.162 DEBUG: POST 500 http://localhost:9000/api/ce/submit?projectKey=designer&projectName=designer | time=130ms
15:25:20.204 INFO: ------------------------------------------------------------------------
15:25:20.204 INFO: EXECUTION FAILURE
15:25:20.204 INFO: ------------------------------------------------------------------------
15:25:20.204 INFO: Total time: 2:51.645s
15:25:20.302 INFO: Final Memory: 56M/2208M
15:25:20.302 INFO: ------------------------------------------------------------------------
15:25:20.303 ERROR: Error during SonarQube Scanner execution
Failed to upload report - An error has occurred. Please contact your administrator
查看 sonarqube-7.6\logs 下的日志
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (5869480 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
表示生成的文件太大了
数据库设置的允许的文件太小
解决方案:
https://blog.csdn.net/frankcheng5143/article/details/51755070
我这里直接修改数据库中的my.ini,添加一下配置,然后重启sonar