.net continuous integration sonarqube installation and basic configuration of articles sonarqube

Series catalog

Sonarqube download and install

Sonarqube download address is: https://www.sonarqube.org/downloads/download version has two, one is long-term support version, the other is the latest version, installed here is the latest version, the current version is 7.3, download time clicking striking blue button (at this time download the community version), here are three buttons without background download link, corresponding to a Developer Edition, enterprise Edition and datacenter Edition, these versions are not the free version, you need to get Licence key can be used. currently infancy using the community Edition on the Ok.

Note Sonarqube is based on java language development, and therefore must be installed before running Jre

Sonarqube support for Windows, mac and linux, but does not distinguish platform installation package, which is three platforms download package is the same, just different ways to start.

Download the complete whole unpack the archive to download, go to the bin directory, you can see this directory there are several folders, the name of the folder is easy to see that they correspond to windows, mac, linux platform startup directory, because the we are running under windows platform and thus into the windows-x86-64 directory (of course, if your computer is a 32-bit system, then enter the directory windows-x86-32) this directory has many script files, we double-click on StartSonar.batthe batch sonarqube, start the next processing file to run windows need half a minute, please be patient, and so on. when you see the last sentence of console SonarQube is upexplanation sonarqube has been successfully started. at this point in the browser address bar localhost:9000to get into management sonarqube web page .

avatar

This page is an introductory page on Sonarqube, and from here you can see very clear analysis Sonarqube language support, click the Read Documentationbutton to enter Sonarqube help documentation page.

Sonarqube basic configuration

上一节我们已经成功启动Sonarqube,然而仅仅把Sonarqube启动起来并没有什么作用,我们还需要进行数据库配置和扫描器(Sonarqube对特定语言的扫描工具称为扫描器)配置才能把数据接入Sonarqube管理平台.

数据库配置

Sonarqube支持内嵌数据库,mysql,oracle 11g/12c,sql server 2014/2016,postgresql 9.3+,由于oracle需要提供单独的驱动比较麻烦,这里就以mysql为例讲解.

我们进入Sonarqube目录下的conf目录,下面有一个sonar.properties文件,我们用记录本打开它,我们进入mysql栏把sonar.jdbc.url注释取消掉,并加上以下代码

sonar.jdbc.username=root
sonar.jdbc.password=
sonar.sorceEncoding=UTF-8
sonar.login=root
sonar.password=

Sonarqube默认的数据库名为sonar,需要我们手动创建,我们在自己常用的mysql管理工具里新建即可.

以上是关于sonarqube mysql连接字符串的配置.用户名和密码读者根据实际情况填写

以上笔者使用的是Xampp带的mysql的默认配置,用户名为root,密码为空,注意这里的password不能写为"",而是什么都不写留空,当然如果有密码就填写实际密码.

Sonarqube 7.3仅支持mysql 5.6及以上8.0以下版本,也就是说不支持8.0版本,需要特别注意.

Sonarqube也支持内嵌数据库,然而内嵌数据库不便于管理和迁移,强烈不建议在生产环境使用内嵌数据库.

我们重新启动Sonarqube,然后进入web管理界面,此时我们打开mysql管理工具,可以看到sonarqube在sonarqube库下创建了很多表.

安装配置Msbuild Scanner

上一节我们说过,Sonarqube需要使用Scanner来扫描代码数据以供Sonarqube管理平台使用,这里我们下载 msbuild scanner扫描工具对c#代码进行扫描.

我们把Sonarqube web管理界面往下拉到最底,会看到一拍图标

avatar

我们点击第一个图标,也就是Visual Studio图标,会进入一个页面

avatar
图示部分为下载链接,我们选择.net framework 4.6+

注意,这里下载链接的名称虽然是for .net framework 4.6+,实际上仍然支持.net 4.0及以上版本,并不是只支持.net 4.6以上版本.

下载完成后,我们把压缩包解压到一个目录,并把目录地址添加到系统环境变量里.

同样,为了方便使用,我们也需要把msbuild.exe添加到环境变量中,如果你的系统中安装的visual Studio开发工具,则Visual studio中会自动包含一个msbuild.exe我们找到它的路径添加到系统环境变量path中.

配置SonarQube.Analysis.xml文件

由于Scanner要通过rest api方式向服务器提交数据,这里需要对Sonarqube web服务器地址和用户名进行配置才能正常提交数据,我们打开刚解压的目录,找到SonarQube.Analysis.xml文件,添加以下三行配置即可

<Property Name="sonar.host.url">http://localhost:9000</Property>

  <Property Name="sonar.login">admin</Property>
  <Property Name="sonar.password">admin</Property>

以上配置很容易理解,url为Sonarqube服务器地址,下面为登陆名和密码(Sonarqube默认带一个管理员账号,用户名为admin密码也是admin,后面我们会讲在生产环境中如何更改admin密码)

再次回到刚才的下载页面,定位到usage栏,可以看到有一个简短的示例代码如下

SonarScanner.MSBuild.exe begin /k:"project-key"
MSBuild.exe /t:Rebuild
SonarScanner.MSBuild.exe end

我们依照这段代码的结构,对本地的一个项目进行编译(可以是任意c#项目),进入项目sln或者csproj所在的文件夹下依次执行以下代码

SonarScanner.MSBuild.exe begin /k:"mygetdata" /v:"1.0"
MSBuild.exe /t:Rebuild
SonarScanner.MSBuild.exe end

And the second is the implementation of msbuild, the end of the third paragraph is a sign, there is no need to elaborate on the details about msbuild to build can view Microsoft's official website, you can also refer to other sections of this tutorial. Here is a brief introduction of the first paragraph under / k and the / v parameter, / k is the abbreviation of the key, sonarqube each project must have a unique key, key can set their own rules, but the need to facilitate the management, / v for the version, if two identical constructed key, verison different, sonarqube will generate a contrast to visually see the difference between the two constructed so easy to solve the situation administrator to view the problem.

After executing the above command, we re-enter Sonarqube home, this time we click on the Login button to log in, 只有登陆以后才可以看到与项目相关的内容click on the user name such as admin, password is admin (as I said before Sonarqube default account will get a user name and password are admin of, later we'll talk about how to change the password and how to assign the account to a different developer)

After landing, you will see a screen as shown below in FIG.

avatar

From the figure we can see the key we just created is mygetdatabuilt, you can briefly see a serious bug in this project from the figure, general bug, the code is not standardized, the code unit test coverage, code duplication of code and finally an analysis time.

We click on mygetdatathe title, it will go in more detailed information about the task of building

avatar

You can see more detailed information, switching the navigation bar above you can see more detailed information on a particular dimension.

The large number icons are clickable links, such as display of 10 pages bugs we click on 10that number you can enter the 10 bug-free code details

avatar

Guess you like

Origin www.cnblogs.com/tylerzhou/p/11229548.html