初识Sonar——代码质量管理平台

Sonar介绍

Sonar是一个用于代码质量管理的开放平台。

  • 通过插件机制,可以集成不同的测试工具、代码分析工具等。
  • 对其他工具的支持,Sonar支持集成在IDE里(Eclipse, idea),也支持Jenkins集成。
  • Sonar本身是Java语言,需要运行在JDK1.8及以上。
  • Sonar支持20+种语言代码扫描,包括Java、Python、C++.....

SonarQube搭建

# 数据库用户名密码
sonar.jdbc.username=sonar
sonar.jdbc.password=123456

# 我用的是mysql。如果是Oracle/PostgreSql/SQLServer,打开相应注释即可
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

# 默认端口9000,可修改
sonar.web.port=9000
  • Sonar服务启动,bin/目录下依次运行
InstallNTService.bat
StartNTService.bat
StartSonar.bat
  • Sonar Web访问
    浏览器打开http://localhost:9000,默认管理员用户密码 admin admin。

Sonar Scanner扫描

Sonar通过SonarQube Scanner对代码进行扫描和分析。

#----- Default SonarQube server
sonar.host.url=http://localhost:9000
  • sonar scanner对项目代码扫描
    1)配置
    需要在项目路径下放至配置文件sonar-project.properties,主要就是指定项目名称、版本、扫描范围。
# must be unique in a given SonarQube instance
sonar.projectKey=doc-parser
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=doc-parser
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

2)启动扫描
在项目路径下,执行sonar-scanner命令启动scanner。

Sonar Web可视化

扫描完成后,即可在http://localhost:9000上看到分析结果,接下来就是去分析问题和优化代码啦。

overview

issues详情

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容