SonarQube

代码质量管理

 Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
        在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。

Sonar部署

Sonar的相关下载和文档可以在下面的链接中找到:http://www.sonarqube.org/downloads/,需要注意最新版的Sonar需要至少JDK 1.8及以上版本。

  • yum install -y java-1.8.0
  • cd /usr/local/src
  • wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-7.5.zip
  • unzip sonarqube-5.6.zip
  • mv sonarqube-7.5 /usr/local/
  • ln -s /usr/local/sonarqube-7.5/ /usr/local/sonarqube

准备Sonar数据库

  • mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
  • mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';
  • mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@pw';
  • mysql> FLUSH PRIVILEGES;

配置Sonar

  • cd /usr/local/sonarqube/conf/
  • sonar.properties wrapper.conf
  • 编写配置文件,修改数据库配置
  • sonar.properties
  • sonar.jdbc.username=sonar
  • sonar.jdbc.password=sonae@pw
  • sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

启动Sonar

你可以在Sonar的配置文件来配置Sonar Web监听的IP地址和端口,默认是9000端口

  • sonar.properties
  • sonar.web.host=0.0.0.0
  • sonar.web.port=9000
  • /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start

访问Sonar
    http://IP:9000

插件管理

  • /usr/local/sonarqube/extensions/plugins

代码分析插件

    • https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
    • Https://github.com/SonarSource 各种语言测试范例,sonar-example

Sonar-scanner插件目录结构

  • [root@localhost local]# cat sonar-scanner/conf/sonar-scanner.properties
  • #Configure here general information about the environment, such as SonarQube server connection details for example
  • #No information about specific project should appear here
  • #----- Default SonarQube server
  • sonar.host.url=http://sonarqube地址:9000
  • #----- Default source code encoding
  • sonar.sourceEncoding=UTF-8
  • sonar.jdbc.username=sonar(数据库用户)
  • sonar.jdbc.password=sonar@pw(数据库密码)
  • sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp:characterEncoding=utf8

Sonar-example语言测试范例

  • [root@localhost local]# cat sonar-project.properties
  • sonar.projectKey=demo
  • sonar.projectName=demo
  • sonar.projectVersion=1.0
  • sonar.sources=./
  • sonar.language=java
  • sonar.sourceEncoding=UTF-8

Sonar-scanner检测命令

  • [root@localhost local]# /usr/local/sonar-scanner/bin/sonar-scanner

访问Sonar
    http://IP:9000 就会看到示例,检测什么样的语言,提前按照好插件,Chinese Pack是中文插件

猜你喜欢

转载自www.cnblogs.com/jokerbj/p/10322129.html