SonarQube安装、配置与使用

一、准备工作

二、安装篇

安装MySQL

  1. 执行MySQL语句
    1
    2
    3
    4
    5
    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;

搭建JAVA环境

  1. 安装JDK(D:\Java)
  2. 配置环境变量(右键此电脑-属性-高级系统设置-环境变量)
  3. 如图配置(配置Path环境变量在安装SonarQubeScannerforMSBuild时说明)
    Alt text

安装SonarQube(http://localhost:9000)

  1. 解压SonarQube到目录(D:\SonarQube\sonarqube-6.0)
  2. 配置环境变量(右键此电脑-属性-高级系统设置-环境变量)
  3. 如图配置(配置Path环境变量在安装SonarQubeScannerforMSBuild时说明)
    Alt text

安装SonarQube Scanner for MSBuild

  1. 解压SonarQube到目录(D:\SonarQube\SonarQubeScannerforMSBuild-2.1)
  2. 配置环境变量(右键此电脑-属性-高级系统设置-环境变量)
  3. 如图配置(配置Path环境变量在安装SonarQubeScannerforMSBuild时说明)
    Alt text
  4. 配置Path环境变量
    Alt text

三、配置篇

配置SonarQube

  1. 修改配置文件(D:\SonarQube\sonarqube-6.0\conf\sonar.properties)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    # Property values can:
    # - reference an environment variable, for example sonar.jdbc.url= ${env:SONAR_JDBC_URL}
    # - be encrypted. See http://redirect.sonarsource.com/doc/settings-encryption.html
    #--------------------------------------------------------------------------------------------------
    # DATABASE
    #
    # IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for
    # production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
    # User credentials.
    # Permissions to create tables, indices and triggers must be granted to JDBC user.
    # The schema must be created first.
    sonar.jdbc.username=root
    sonar.jdbc.password=root
    #----- MySQL 5.x
    # Only InnoDB storage engine is supported (not myISAM).
    # Only the bundled driver is supported. It can not be changed.
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
    #----- Connection pool settings
    # The maximum number of active connections that can be allocated
    # at the same time, or negative for no limit.
    # The recommended value is 1.2 * max sizes of HTTP/HTTPS pools. For example if both HTTP and
    # HTTPS ports are enabled with default sizes (50, see properties sonar.web.http.maxThreads
    # and sonar.web.https.maxThreads) then sonar.jdbc.maxActive should be 1.2 * (50 + 50) = 120.
    sonar.jdbc.maxActive=60
    # The maximum number of connections that can remain idle in the
    # pool, without extra ones being released, or negative for no limit.
    sonar.jdbc.maxIdle=5
    # The minimum number of connections that can remain idle in the pool,
    # without extra ones being created, or zero to create none.
    sonar.jdbc.minIdle=2
    # The maximum number of milliseconds that the pool will wait (when there
    # are no available connections) for a connection to be returned before
    # throwing an exception, or <= 0 to wait indefinitely.
    sonar.jdbc.maxWait=5000
    sonar.jdbc.minEvictableIdleTimeMillis=600000
    sonar.jdbc.timeBetweenEvictionRunsMillis=30000
    #--------------------------------------------------------------------------------------------------
    # WEB SERVER
    # Web server is executed in a dedicated Java process. By default heap size is 768Mb.
    # Use the following property to customize JVM options.
    # Recommendations:
    #
    # The HotSpot Server VM is recommended. The property -server should be added if server mode
    # is not enabled by default on your environment: http://docs.oracle.com/javase/7/docs/technotes/guides/vm/server-class.html
    #
    #sonar.web.javaOpts=-Xmx768m -Xms256m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError -Djava.net.preferIPv4Stack=true
    # Same as previous property, but allows to not repeat all other settings like -Xmx
    #sonar.web.javaAdditionalOpts=
    # Binding IP address. For servers with more than one IP address, this property specifies which
    # address will be used for listening on the specified ports.
    # By default, ports will be used on all IP addresses associated with the server.
    sonar.web.host=127.0.0.1
    # TCP port for incoming HTTP connections. Disabled when value is -1.
    sonar.web.port=9000
    #--------------------------------------------------------------------------------------------------
  2. 执行(D:\SonarQube\sonarqube-6.0\bin\windows-x86-64\StartSonar.bat),打开http://localhost:9000,如果显示SonarQube主页则安装成功

  3. 安装插件SonarQubeC# 、Chinese Pack等,插件JAR包放到(D:\SonarQube\sonarqube-6.0\extensions\plugins),重启服务即安装成功

配置SonarQube Scanner for MSBuild

  1. 修改配置文件(D:\SonarQube\SonarQubeScannerforMSBuild-2.1\conf\sonar-runner.properties)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    #Configure here general information about the environment, such as SonarQube DB details for example
    #No information about specific project should appear here
    #----- Default SonarQube server
    sonar.host.url=http://127.0.0.1:9000
    #----- PostgreSQL
    #sonar.jdbc.url=jdbc:postgresql://localhost/sonar
    #----- MySQL
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
    #----- Oracle
    #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
    #----- Microsoft SQLServer
    #sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
    #----- Global database settings
    sonar.jdbc.username=root
    sonar.jdbc.password=root
    #----- Default source code encoding
    sonar.sourceEncoding=UTF-8
    #----- Security (when 'sonar.forceAuthentication' is set to 'true')
    sonar.login=admin
    sonar.password=admin
  2. 命令行执行sonar-runner -h验证是否安装成功

四、使用篇

代码质量检测

  1. 拷贝项目到目录(D:\SonarQube)
  2. 添加sonar-project.properties文件到项目目录下(D:\SonarQube\CharmCore\sonar-project.properties),内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # Required metadata
    sonar.projectKey=org.sonarqube:limingzhi
    sonar.projectName=Example of Generic Test Coverage Reports Import ::limingzhi
    sonar.projectDescription=Example of Import of UT Execution Results Report and UT/IT Coverage Report Import
    sonar.projectVersion=1.0
    # Comma-separated paths to directories with sources (required)
    sonar.sources=ConsoleApplication
    sonar.tests=UnitTestProject
    sonar.language=cs
    # Encoding of the source files
    sonar.sourceEncoding=UTF-8
    #sonar.genericcoverage.reportPaths=report/coverage.xml
    #sonar.genericcoverage.itReportPaths=report/itcoverage.xml
    #sonar.genericcoverage.unitTestReportPaths=report/unittest.xml
  3. 到项目根目录(D:\SonarQube\CharmCore),命令行执行sonar-runner分析,当出现SUCCESS时则分析成功

  4. 刷新网站(http://localhost:9000)
Mickey wechat
微信公众号