Sonarqube C # code quality analysis based on WIN10 minimalism

Source: Internet
Author: User

Blog Some of the time has not been updated, these months of time, left the internship company, the university graduated from the industry, to the new company, turned the account, a little busy, recently finally a little idle, intends to re-pick up the blog, insist on writing down.

What is Sonarqube, the word goes forward?

SonarQube(formerly known as Sonar) is an excellent open source Code Analysis System Management system that supports more than 25+ programming languages and, of course, supports. Net core.

Recently, the company's projects are developed using the framework, Sonarqube name, today in the local construction sonarqube after the analysis of the project, the effect is amazing. Pulled out the system hidden in a number of bugs, work can not be, so the method of building here to share to everyone, hope to help you.

Find some information on the Internet, about the introduction of sonar in the Linux platform more, so I am the following is mainly based on the win platform, the other platform is similar.

The following are the main steps to installing sonar:

Installing the Java SDK

Sonar is a Java-based development tool, the process of installing the Java SDK is not described here, it is recommended to install the Java_home environment variables, the following is.

Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Installing Sonarqube

First download the installation package to the official website, it is worth noting that the installation package is platform-free, after downloading, select the Windows folder in the Startsonar.bat file to run.

https://www.sonarqube.org/#downloads

If the Java environment is installed properly, sonar should be able to start normally and browse after startup. The start effect is as follows:

Just loaded is English, I installed the Chinese package, how to install Chinese bag, will be described later.

Configure Sonar

We need to simply configure sonar so that it can connect to the MySQL database.

Open the MySQL database and execute the following instructions.

CREATE DATABASESonarCHARACTER SETUTF8 COLLATE utf8_general_ci;CREATE USER 'Sonar'Identified by 'Sonar';GRANT  All  onSonar.*  to 'Sonar'@'%'Identified by 'Sonar';GRANT  All  onSonar.*  to 'Sonar'@'localhost'Identified by 'Sonar'; FLUSHPrivileges;

The operation is to create a database for sonar and add the database user, the database name is sonar, the user name is sonar, the password is sonar.

Open sonar.properties to replace the contents as follows:

Sonar.jdbc.username=sonarsonar.jdbc.password=sonarsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar? Useunicode=true&characterencoding=utf8&rewritebatchedstatements=true& Useconfigs=maxperformance&usessl=false

Where Sonar.jbc.url is the connection string for the MySQL database.

Restart Sonar (Close the Run Startsonar.bat console and close all Java-related processes in Task Manager, rerun Startsonor.bat), and log in with the Administrator account (admin/admin).

After logging in, install the Chinese package, such as, after the installation needs to click Restart, after the launch, Sonar becomes Chinese.

Sonar-scanner for MSBuild installation and configuration

Download and unzip Sonarqube Scanner for MSBuild, which is the analysis plug-in for the C # framework.

https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/4.3.1.1372/ Sonar-scanner-msbuild-4.3.1.1372-net46.zip

After decompression, set Sonarqube Scanner for MSBuild environment variables, such as my decompression path is: C:\MyWorkSpace\Tools\ Sonar-scanner-msbuild-4.3.1.1372-net46, add the path to path:

Modify the SonarQube.Analysis.xml file

To modify the place is just about the Sonarqube server configuration, about the server URL, USER, password, and so on, modified as follows:

<?XML version= "1.0" encoding= "Utf-8"?><!--  This file defines properties which would is understood by the SonarQube Scanner for MSBuild, if not overridden (see Belo W) By default the SonarScanner.MSBuild.exe picks-up a file named SonarQube.Analysis.xml in the folder it is located (if It exists). It is possible to use another properties file by using The/s:filepath.xml flag the overriding strategy's property value S is the following:-A project-specific Property defined in the MSBuild *.*proj file (corresponding to A SonarQube Modul e) can override:-a property defined with the command line (/d:propertyname=value) have which can override:-a property D Efined in the SonarQube.Analysis.xml configuration file [This file] which can override:-A property defined in the Sonar Qube user Interface at project level which can override:-A property defined in the SonarQube User Interface at Global l  Evel which can ' t override anything. Note that the following properties cannot is set through an MSBuild project file or an SonarQube.Analysis.xml File:sonar.projectName, Sonar.projectkey, sonar.projectversion the following flags need to being used to set Their value:/n:[sonarqube Project Name]/k:[sonarqube project Key]/v:[sonarqube project Version] -<sonarqubeanalysispropertiesXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"xmlns= "HTTP://WWW.SONARSOURCE.COM/MSBUILD/INTEGRATION/2015/1">    < PropertyName= "Sonar.host.url">http://localhost:9000</ Property>  < PropertyName= "Sonar.login">Admin</ Property>  < PropertyName= "Sonar.password">Admin</ Property>   <!--Required versions of SonarQube prior to 5.2 -    < PropertyName= "Sonar.jdbc.url">Jdbc:mysql://localhost:3306/sonar?useunicode=true;characterencoding=utf8;rewritebatchedstatements=true; Useconfigs=maxperformance;usessl=false</ Property>  < PropertyName= "Sonar.jdbc.username">Sonar</ Property>  < PropertyName= "Sonar.jdbc.password">Sonar</ Property> </sonarqubeanalysisproperties>

Next, an important step is to find the MSBuild.exe in your computer and add it to the PATH environment variable so that you can call MSBuild later on the command line, and mine is in the installation directory of VS 2017

C:\Program Files (x86) \microsoft Visual Studio\2017\professional\msbuild\15.0\bin\amd64

C # project Analysis

cmd into the root directory where the C # project is located, execute the following three commands.

MSBuild.SonarQube.Runner.exe begin/k: "Xxh.xzc.api"/N: "Xhh.xzc.api"/V: "1.0" msbuild.exe/t: RebuildMSBuild.SonarQube.Runner.exe End

Parameter description:

/key (abbreviation k): Corresponding Projectkey is the unique code of the project, such as two sets of source code using the same Projectkey the results of the scan will be mixed together, so a project needs to have a separate projectkey

/name (abbreviated N): corresponds to projectname that is the name of the project, a display name for the project, establish the use of the full project name

/version (short v): corresponding to the projectversion is the project version, the project in different time version is not the same, if convenient, you can view the different version code in the Sonarqube server where the problem changes

The three commands are pre-preparation for the analysis, MSBuild compiles, and the report is passed on to Sonarqube.

View Analysis Results

Finally, go to http://localhost:9000/projects to view the results of the analysis, surprise not surprise?

The interface is powerful, many think that absolutely can't find the bugs are displayed, but also to see the coverage of unit testing, I believe that if the use of the tool, it will be very helpful in coding habits.

Quickly build a sonarqube to see if your code is bug!!

Reference: Https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild

Sonarqube C # code quality analysis based on WIN10 minimalism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.