美文网首页
初识Sonar——代码质量管理平台

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

作者: treeph | 来源:发表于2018-05-14 17:41 被阅读0次

    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详情

    相关文章

      网友评论

          本文标题:初识Sonar——代码质量管理平台

          本文链接:https://www.haomeiwen.com/subject/myeddftx.html