win7搭建本地SonarQube环境进行c#代码分析

1、SonarQube需要正常运行,首先需要安装Java环境,我这里安装的是jdk-8u181版本,可以在下面网站找适的版本去下载安装

  https://www.oracle.com/technetwork/java/javase/downloads/index.html

  安装运行JDK以后,还需要做下面的几项配置:

  计算机属性》高级系统设置》高级》环境变量》系统变量

  win7搭建本地SonarQube环境进行c#代码分析

   系统变量添加JAVA_HOME, 内容为jdk的安装目录。例如我的安装目录为D:20170324softjdk1.8.0_181

  在系统变量Path中添加jdk安装目录下的bin目录,例如D:20170324softjdk1.8.0_181in

2、在这里根据需要下载适合的版本,我下载的是sonarqube-7.6,下载成功后解压,如图所示:

  win7搭建本地SonarQube环境进行c#代码分析

  bin目录下放的是不同环境下的启动脚本

  conf目录下放的是配置文件

  extensions目录下放的是安装的扩展

  logs目录下放的是日志文件,在启动和运行时遇到问题可以通过该目录下生成的日志进行纠错

3、sonarqube配置,我这边使用的是sonarqube内嵌数据库,所以没有配置数据库,如果需要配置,根据自己选择的数据库进行配置,配置文件为:

  win7搭建本地SonarQube环境进行c#代码分析

   该文件DATABASE配置代码如下,可以参考:

#--------------------------------------------------------------------------------------------------
# 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.
# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
#sonar.jdbc.username=
#sonar.jdbc.password=

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092

#----- DEPRECATED 
#----- MySQL >=5.6 && <8.0
# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
# 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&useSSL=false


#----- Oracle 11g/12c
# The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/.
# Only the thin client is supported, and only the versions 11.2.x or 12.2.x must be used. See
# https://jira.sonarsource.com/browse/SONAR-9758 for more details.
# If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE


#----- PostgreSQL 9.3 or greater
# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
#sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema


#----- Microsoft SQLServer 2014/2016 and SQL Azure
# A database named sonar must exist and its collation must be case-sensitive (CS) and accent-sensitive (AS)
# Use the following connection string if you want to use integrated security with Microsoft Sql Server
# Do not set sonar.jdbc.username or sonar.jdbc.password property if you are using Integrated Security
# For Integrated Security to work, you have to download the Microsoft SQL JDBC driver package from
# https://www.microsoft.com/en-us/download/details.aspx?id=55539
# and copy sqljdbc_auth.dll to your path. You have to copy the 32 bit or 64 bit version of the dll
# depending upon the architecture of your server machine.
#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true

# Use the following connection string if you want to use SQL Auth while connecting to MS Sql Server.
# Set the sonar.jdbc.username and sonar.jdbc.password appropriately.
#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar


#----- 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 pools. For example if HTTP ports are
# enabled with default sizes (50, see property sonar.web.http.maxThreads)
# then sonar.jdbc.maxActive should be 1.2 * 50 = 60.
#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

  主要是配置这三部分内容

sonar.jdbc.username=
sonar.jdbc.password=
sonar.jdbc.url=

4、启动 sonarqube,在bin文件下根据系统选择合适的启动脚本。

  win7搭建本地SonarQube环境进行c#代码分析

   运行StartSonar.bat,等执行完成,看到结果都是类似的is upwin7搭建本地SonarQube环境进行c#代码分析

,那就证明启动成功了,浏览器访问http://localhost:9000,可以正常访问证明SonarQube搭建成功了,默认的是英文。

5、登录SonarQube就可以进行配置,默认登录名和密码都是admin,进去之后自己可以修改。英文看着不舒服,安装中文包,如下图所示:

win7搭建本地SonarQube环境进行c#代码分析

 一般搜索都会匹配到适合该版本的中文包,如果没有匹配到合适的中文包,自己可以去这里下载合适的中文包,下载下来后扔到sonarqube-7.6extensionsplugins文件夹下,重新启动网页就OK了。

6、SonarQube Scanner for MSBuild安装与配置,这个是C# Framework的分析插件。

  这个插件的下载,可以在这里根据自己的需要下载,我下载的是4.3.1下的net46版本,下载成功解压之后,设置SonarQube Scanner for MSBuild的环境变量,如我的解压路径是:D:20170324mysonar-scanner-msbuild-4.3.1.1372-net46,则把该路径添加到系统变量Path中,添加方法如1中path的添加方法。

  修改SonarQube.Analysis.xml文件,这里的修改如下:

  win7搭建本地SonarQube环境进行c#代码分析

   这里的token值可以在依次点击我的账号》安全》生成令牌

  win7搭建本地SonarQube环境进行c#代码分析win7搭建本地SonarQube环境进行c#代码分析

   如果不生成令牌也可以配置输入账号和密码,如果有需要可以对下面的数据库进行配置。

  接下来比较重要的一步,找到电脑中的MSBuild.exe,把所在目录添加到path变量中,这样操作后面方便在命令行中调用MSBuild。我这边在C:Program Files (x86)MSBuild14.0Binamd64目录下,添加方法如1中path的添加方法。

7、项目分析

  cmd进入项目所在的根目录,依此执行以下三条命令。

MSBuild.SonarQube.Runner.exe begin /k:"projectKey" /n:"projectName" /v:"1.0"
MSBuild.exe /t:Rebuild
MSBuild.SonarQube.Runner.exe end 

  参数说明:

  /key(简写k):对应projectKey即项目的唯一编码,如两套源代码使用同一个projectKey那扫描的结果将混在一起,所以一个项目需要有一个单独的projectKey

  /name(简写n):对应projectName即项目的名称,为项目的一个显示的名称,建立使用完整的项目名称

  /version(简写v):对应projectVersion即项目的版本,项目在不同的时期版本也是不一样的

  三条命令分别是分析的前期准备,MSBuild编译,将报告上传给SonarQube。执行命令期间就是等待,必须等一条命令执行完成后在执行下一条命令。

  命令执行成功后在http://localhost:9000查看分析结果。

8、Windows下重启sonar:

  首先关闭SonarQube.bat窗口

  再Ctrl+Shift+Esc调出windows资源管理器

  在进程中关闭所有Java.exe进程

  然后重新运行StartSonar.bat文件

  win7搭建本地SonarQube环境进行c#代码分析

  参考文献:

  1、https://www.cnblogs.com/CoderAyu/p/9416376.html

  2、https://www.cnblogs.com/luoqin520/p/6945304.html

  3、https://www.cnblogs.com/7tiny/p/11269774.html