sonar代码扫描

官方文档

  • SonarQube平台由4个组件组成:
    • SonarQube服务
    • SonarQube数据库
    • SonarQube插件
    • Sonar Scanners

注:SonarQube 5.6+需要Java 8环境,没有java环境的首先安装jdk

配置SonarQube

1.配置conf/sonar.properties

sonar.web.host=0.0.0.0
sonar.web.port=9000
sonar.search.port=9001
  1. cd /bin/macosx-universal-64/下启动服务
  • 启动:./sonar.sh start
  • 停止:./sonar.sh stop
  • 重启:./sonar.sh restart
  1. 访问http://127.0.0.1:9000,能正常访问,但是在最下面有红字提示那是因为没有连接数据库。

配置数据库

使用docker (http://www.runoob.com/docker/docker-install-mysql.html)

docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6 --max-allowed-packet=20971520 --innodb_log_file_size=209715200

  • 数据库官方支持多种,这里配的MySql:

1.新增sonar database以及sonar用户:

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;

重启mysql服务

2.在sonarqube/conf/sonar.properties添加配置

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

3.重启SonarQube服务,再次访问http://127.0.0.1:9000 正常启动,看到页面最底端的红字预警提示消失,则配置成功。

SonarQube插件

  • 每种语言都有自己的插件,默认在extensions/plugins目录
  • SonarQube插件官方有开源的跟收费的
  • 插件官方地址结尾带*的为收费版

只需要将插件放到目录下,没有任何配置。

添加汉化插件

  • 下载地址
  • 下载完后jar包添加到extensions/plugins目录

重启SonarQube服务,插件自动生效

Sonar Scanners 代码扫描

1.将/bin目录添加到环境变量

# SonarScanner
export SONAR_SCANNER_HOME=~/tools/sonar/sonar-scanner
export PATH=$PATH:$SONAR_SCANNER_HOME/bin

2.配置conf/sonar-scanner.properties

sonar.host.url=http://localhost:9000

3.到项目根目录创建sonar-project.properties文件

sonar.projectKey=py_test
sonar.projectVersion=1.0
sonar.sources=.
sonar.language=py
sonar.sourceEncoding=UTF-8

注:在扫描java项目java项目时必须配置sources,binaries

sonar.sources=app/src
sonar.java.binaries=app/build/intermediates/classes

4.启动代码扫描:sonar-scanner

错误总结

Failed to upload report - 500: An error has occurred. Please contact your administrator

查看logs/web.log详细日志

  • 错误1.
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (14049363 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.

在数据库的my.cnf文件添加max-allowed-packet=xxxx

  • 错误2.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: The size of BLOB/TEXT data inserted in one transaction is greater than 10% of redo log size. Increase the redo log size using innodb_log_file_size.

在数据库的my.cnf文件添加innodb_log_file_size=xxxx

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文重点说明sonarqube的使用和搭建,以及集成到Jenkins,从因到果进行详细的说明。gitLab+Jen...
    GeekSpring阅读 26,424评论 1赞 7
  • 由来 《Sonar code quality testing essential》一书中从七个维度定义了代码的这种...
    我为峰2014阅读 9,413评论 0赞 21
  • 一. Linux服务器搭建sonar https://www.digitalocean.com/community...
    hehetc阅读 2,252评论 4赞 3
  • Sonar简介 Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从七个维度检测代码质量 通过...
    真的有神阅读 4,185评论 0赞 0
  • 大家早上好,把清晨的第一缕送给亲爱的朋友们!今天是公历10月25日,星期五,农历九月二十七,抗美援朝纪念日,台湾光...
    旖旎i阅读 197评论 0赞 9

友情链接更多精彩内容