质量管理工具sonarQube环境搭建教程

1. 构建持续的质量管理平台的生态环境

  • 源码版本控制工具:SVN、Git等
  • JDK:JDK1.8地址
  • 持续集成工具:Jenkins等
  • 质量管理工具:SonarQube等

2. SonarQube的工具链

  • 工程(Project):待分析的源码工程;
  • 一个数据库(SonarQube Database):存放配置信息和分析结果信息;以Mysql为例;
  • 一个WEB服务器(SonarQube Server):发布应用,在线浏览、配置分析;
  • 一个客户端(SonarQube Scanner):执行源代码分析。
    • 原名为 sonar runner,改为 sonar scanner

3. 本文主要介绍 SonarQube 在mac机上的搭建流程

3.1 搭建 Mysql 数据库

  • 3.1.1 Mysql 下载地址

  • 3.1.2 点击 mysql 的 dmg 文件,安装mysql,安装完成获取提示,注意记录账号 root;密码:LhEfS:lEe0c(密码随机生成):

    2017-09-18T13:50:51.563897Z 1 [Note] A temporary password is generated for root@localhost: LhEfS:lEe0c)
    
    If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.
    
  • 3.1.3 验证mysql安装结果
    终端输入 mysql --version 命令行,显示以下内容则表示安装成功

    mysql --version
    mysql  Ver 14.14 Distrib 5.7.19, for macos10.12 (x86_64) using  EditLine wrapper
    
    • 验证失败处理方法

      如果没有成功,执行以下命令行:

       cd /usr/local/bin/
       sudo ln -fs /usr/local/mysql/bin/mysql mysql
      
  • 3.1.4 终端登录 mysql 数据库
    终端输入 mysql -u root -p 之后输入密码(LhEfS:lEe0c),显示以下内容表示登录成功:

    mysql -u root -p
    Enter password: *******
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 36
    Server version: 5.7.19 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    
    • 登录异常处理措施:

      Access denied for user 'root'@'localhost' (using password: YES)
      
      • 停止MySQL的服务,打开系统的偏好设置,找到MySQL 进去后,点击Stop MySQL Server即可。

        停止Mysql.png
      • 开启两个终端,在第一个终端输入 sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables 输入当前用户的密码,如下所示

        sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
        Password:******
        2017-09-20T03:47:18.6NZ mysqld_safe Logging to '/usr/local/mysql/data/Kim.local.err'.
        2017-09-20T03:47:18.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
        
      • 然后在第二个终端输入 sudo /usr/local/mysql/bin/mysql -u -root ,然后输入当前用户的密码后,出现以下的界面

        sudo mysql -u root
        Welcome to the MySQL monitor.  Commands end with ; or \g.
        Your MySQL connection id is 36
        Server version: 5.7.19 MySQL Community Server (GPL)
        
        Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
        
        Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
        
        Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
        
        mysql> 
        
      • 然后在第二个终端继续输入命令 UPDATE mysql.user SET authentication_string=PASSWORD('新密码') WHERE User='root'; 回车,出现以下内容,说明修改成功。

        mysql> UPDATE mysql.user SET authentication_string=PASSWORD('newPassword') WHERE User='root';
        Query OK, 0 rows affected, 1 warning (0.01 sec)
        Rows matched: 1 Changed: 0  Warnings: 1
        
        mysql>
        
      • 然后在第二个终端继续输入命令 FLUSH PRIVILEGES; 回车,出现以下内容。

        mysql> FLUSH PRIVILEGES; 
        Query OK, 0 rows affected (0.00 sec)
        
        mysql>
        
      • 然后在第二个终端继续输入命令 \q ,退出,关闭第一个终端,回到系统的偏好设置,重新开启MySQL即可。

        mysql> \q
        Bye
        

3.2 创建sonar user 和 database

  • 3.2.1 打开一个终端,输入命令

    mysql -u root -p
    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;
    
    • 异常处理

      ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

      解决方法:SET PASSWORD=PASSWORD('新密码’); 注意该新密码指的是root用户; 参考 http://blog.sina.com.cn/s/blog_7d553bb50102w9rb.html

3.3 下载SonarQube服务

  • 3.3.1 JAVA JDK下载地址 ,安装完成,终端输入命令 java -version

    java -version
    java version "1.8.0_40"
    Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
    Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
    
  • 3.3.2 下载地址 版本SonarQube 5.6.6 (LTS *) 建议下载LTS最新版本,不建议下载非LTS版作为生产使用。

  • 3.3.3 打开终端,执行命令

    unzip sonarqube-5.6.zip
    mv sonarqube-5.6 /usr/local
    
  • 3.3.4 修改 sonar.properties

    文件路径 :

    /usr/local/sonarqube-5.6/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
    
  • 3.3.5 sonar命令行

    • 启动sonar

      sudo /usr/local/sonarqube-5.6/bin/linux-x86-64/sonar.sh start
      
    • 关闭 sonar:

      sudo /usr/local/sonarqube-5.6/bin/linux-x86-64/sonar.sh stop
      
    • 重启 sonar:

      sudo /usr/local/sonarqube-5.6/bin/linux-x86-64/sonar.sh restart
      
  • 3.3.6 访问 http://localhost:9000

    • 默认账号:admin 默认密码:admin

      • 异常处理,如果sonar配置MySQL失败,Web页面会提示,sonar会使用默认的数据库。
    配置MySQL失败的提示.png

3.4 配置 Sonar Scanner 分析器

  • 3.4.1 打开终端,执行 brew install sonar-scanner

    brew install sonar-scanner
    
  • 3.4.2 验证 sonar-scanner 安装结果

    sonar-scanner --version
    INFO: Project root configuration file: NONE
    INFO: SonarQube Scanner 3.0.3.778
    INFO: Java 1.8.0_40 Oracle Corporation (64-bit)
    INFO: Mac OS X 10.12.6 x86_64
    

3.5 配置必须的插件

  • 3.5.1 汉化插件流程

    插件安装流程.png

4 SonarQube的安装结束。

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

推荐阅读更多精彩内容