一、准备工作
二、安装篇
安装MySQL
- 执行MySQL语句12345CREATE 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环境
- 安装JDK(D:\Java)
- 配置环境变量(右键此电脑-属性-高级系统设置-环境变量)
- 如图配置(配置Path环境变量在安装SonarQubeScannerforMSBuild时说明)

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

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

- 配置Path环境变量

三、配置篇
配置SonarQube
修改配置文件(D:\SonarQube\sonarqube-6.0\conf\sonar.properties)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475# 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=rootsonar.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=5000sonar.jdbc.minEvictableIdleTimeMillis=600000sonar.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#--------------------------------------------------------------------------------------------------执行(D:\SonarQube\sonarqube-6.0\bin\windows-x86-64\StartSonar.bat),打开http://localhost:9000,如果显示SonarQube主页则安装成功
- 安装插件SonarQubeC# 、Chinese Pack等,插件JAR包放到(D:\SonarQube\sonarqube-6.0\extensions\plugins),重启服务即安装成功
配置SonarQube Scanner for MSBuild
修改配置文件(D:\SonarQube\SonarQubeScannerforMSBuild-2.1\conf\sonar-runner.properties)
12345678910111213141516171819202122232425262728#Configure here general information about the environment, such as SonarQube DB details for example#No information about specific project should appear here#----- Default SonarQube serversonar.host.url=http://127.0.0.1:9000#----- PostgreSQL#sonar.jdbc.url=jdbc:postgresql://localhost/sonar#----- MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&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 settingssonar.jdbc.username=rootsonar.jdbc.password=root#----- Default source code encodingsonar.sourceEncoding=UTF-8#----- Security (when 'sonar.forceAuthentication' is set to 'true')sonar.login=adminsonar.password=admin命令行执行sonar-runner -h验证是否安装成功
四、使用篇
代码质量检测
- 拷贝项目到目录(D:\SonarQube)
添加sonar-project.properties文件到项目目录下(D:\SonarQube\CharmCore\sonar-project.properties),内容如下
1234567891011121314151617# Required metadatasonar.projectKey=org.sonarqube:limingzhisonar.projectName=Example of Generic Test Coverage Reports Import ::limingzhisonar.projectDescription=Example of Import of UT Execution Results Report and UT/IT Coverage Report Importsonar.projectVersion=1.0# Comma-separated paths to directories with sources (required)sonar.sources=ConsoleApplicationsonar.tests=UnitTestProjectsonar.language=cs# Encoding of the source filessonar.sourceEncoding=UTF-8#sonar.genericcoverage.reportPaths=report/coverage.xml#sonar.genericcoverage.itReportPaths=report/itcoverage.xml#sonar.genericcoverage.unitTestReportPaths=report/unittest.xml到项目根目录(D:\SonarQube\CharmCore),命令行执行sonar-runner分析,当出现SUCCESS时则分析成功
- 刷新网站(http://localhost:9000)