{"id":9981,"date":"2015-09-28T15:32:31","date_gmt":"2015-09-28T15:32:31","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2015\/09\/28\/quickstart-analyzing-net-projects-with-sonarqube-msbuild-or-visual-studio-online-and-third-party-analyzers-stylecop-resharper\/"},"modified":"2022-08-02T01:39:39","modified_gmt":"2022-08-02T09:39:39","slug":"quickstart-analyzing-net-projects-with-sonarqube-msbuild-or-visual-studio-online-and-third-party-analyzers-stylecop-resharper","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/quickstart-analyzing-net-projects-with-sonarqube-msbuild-or-visual-studio-online-and-third-party-analyzers-stylecop-resharper\/","title":{"rendered":"Quickstart: Analyzing .NET projects with SonarQube, MSBuild or Visual Studio Online, and third-party analyzers (StyleCop, ReSharper)"},"content":{"rendered":"<p>This post provides a quick-start guide to using SonarQube to analyze .NET managed code. It covers installing SonarQube locally, running your first analysis using <em>MSBuild<\/em>, and using some popular third-party analyzers. It also describes how to use the new Visual Studio Online (VSO) and Team Foundation Server (TFS) Build tasks to perform analysis as part of a VSO or TFS build.<\/p>\n<h3>Introduction<\/h3>\n<p><em><a href=\"http:\/\/www.sonarqube.org\/\" target=\"_blank\" rel=\"noopener\">SonarQube<\/a><\/em> is an open source product, produced by <a href=\"http:\/\/www.sonarsource.com\/\" target=\"_blank\" rel=\"noopener\">SonarSource SA<\/a>, which consists in a set of static analyzers (for many languages), a data mart, and a portal that enables you to manage your technical debt. SonarSource and the community provide additional analyzers (free or commercial) that can be added to a SonarQube installation as plug-ins. SonarSource and Microsoft have been working to integrate SonarQube with MSBuild and TFS for some time and, since August 2015, there is a wide range of possibilities for analyzing the technical debt in your .NET projects.<\/p>\n<p>This post aims to help you discover the possibilities for analyzing your technical debt using the <a href=\"https:\/\/github.com\/SonarSource\/sonar-msbuild-runner\/releases\" target=\"_blank\" rel=\"noopener\">MSBuild.SonarQube.Runner 1.0.1<\/a>**, which is at the core of the integration of SonarQube with MSBuild.<\/p>\n<p>In this post, I\u2019ll show you how you can:<\/p>\n<p>\u00b7 <a href=\"#_Installing_a_SonarQube\">Install a SonarQube server locally on Windows<\/a>. It\u2019s quite easy to set up SonarQube on a Windows computer for trial or demo purposes, though I\u2019ll also provide pointers to setting up a production environment.<\/p>\n<p>\u00b7 <a href=\"#_Executing_the_MSBuild.SonarQube.Run\">Analyze a project locally with <em>MSBuild<\/em><\/a>. In this section, I\u2019ll show how you use the SonarQube.MSBuild.Runner in your builds<\/p>\n<p>\u00b7 <a href=\"#_Executing_3rd_party\">Incorporate other code analyzers that run locally<\/a>. For example, you can get extra coverage for technical debt by using analyzers such as <em>StyleCop *and<\/em> ReSharper.*<\/p>\n<p>\u00b7 <a href=\"#_Running_the_analysis\">Incorporate other code analyzers in VSO or TFS<\/a>. In a previous post, I showed how you can analyze your .NET projects with the SonarQube Build vNext analysis tasks in VSO or TFS. Here, I\u2019ll show you how you can, in addition, run third-party analyzers with Build vNext.<\/p>\n<h3><a name=\"_Installing_a_SonarQube\"><\/a>Installing a SonarQube server on a local Windows computer<\/h3>\n<p>In the following sections of this post, I\u2019ll be using SonarQube server installed on my local computer. To do this I installed Java, downloaded SonarQube from the <a href=\"http:\/\/www.sonarqube.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">SonarSource portal<\/a>, and unzipped it. I\u2019m using the default embedded database (H2), which is not designed to be used for production, and I\u2019m running SonarQube as a Windows service. To set it up, I followed <a href=\"https:\/\/github.com\/SonarSource\/sonar-.net-documentation\/blob\/master\/doc\/additional-configurations.md\" target=\"_blank\" rel=\"noopener\">these instructions on GitHub<\/a>.<\/p>\n<p>Why would you want to use the default, non-production installation? This might be because, as a consultant, you are working with your customer\u2019s code on your own computer, or on one of their computers, but you don\u2019t want to set up an integrated build. Alternatively, it might be that you want to start playing with SonarQube analysis for .NET and learn about it, before using it in production<\/p>\n<p>If you want to do something more serious, such as for production use, you can install another database such as SQL Server. The Visual Studio ALM Rangers provide <a href=\"https:\/\/github.com\/SonarSource\/sonar-.net-documentation\/blob\/master\/doc\/additional-configurations.md#configure-sonarqube-to-use-microsoft-sql-database\" target=\"_blank\" rel=\"noopener\">guidance on how to do that<\/a>. This guidance was converted to a GitHub repository (<a href=\"https:\/\/github.com\/SonarSource\/sonar-.net-documentation\/blob\/master\/doc\/SUMMARY.md\" target=\"_blank\" rel=\"noopener\">sonar-.net-documentation<\/a>), and is also available as a <a href=\"https:\/\/github.com\/SonarSource\/sonar-.net-documentation\/releases\/download\/1.1.0\/SonarQube-Setup-Guide-For-Net-Users-V-1-1-0.pdf\" target=\"_blank\" rel=\"noopener\">PDF file<\/a>.<\/p>\n<p>Next, I signed in as an administrator to the SonarQube server (the default username and password is <strong>admin<\/strong> \/ <strong>admin<\/strong>), went to the Update Center (<strong>Settings<\/strong> | <strong>System<\/strong> | <strong>Update Center<\/strong>), and installed the SonarQube <strong>C# Plug-in<\/strong>. This plug-in tells SonarQube how to analyze C# code. The SonarQube Update Center downloads the plug-in from the Internet and installs it in my SonarQube server.<\/p>\n<p>SonarQube must be restarted after installing or updating a plugin. As I\u2019m running SonarQube as a Windows service, I did this by selecting <strong>Restart<\/strong> in the <strong>Local Services<\/strong> console.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image001\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/6406.clip_image001_thumb_0FB92878.png\" alt=\"clip_image001\" width=\"676\" height=\"474\" border=\"0\" \/><\/p>\n<h4>Installing the MSBuild.SonarQube.Runner scanner<\/h4>\n<p>I downloaded the <a href=\"https:\/\/github.com\/SonarSource\/sonar-msbuild-runner\/releases\/\" target=\"_blank\" rel=\"noopener\">MSBuild.SonarQube.Runner 1.0.1<\/a> scanner, which I unzipped on my computer into the folder C:SqMSBuild.SonarQube.Runner-1.0.1. Remember, my configuration here is a single computer that hosts Visual Studio, the SonarQube Server, and the MSBuild.SonarQube.Runner 1.0.1.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/7534.clip_image002_thumb_7A5E03C0.png\" alt=\"clip_image002\" width=\"355\" height=\"170\" border=\"0\" \/><\/p>\n<h3><a name=\"_Executing_the_MSBuild.SonarQube.Run\"><\/a>Analyzing a project locally with the MSBuild.SonarQube.Runner<\/h3>\n<p>The first experiment I\u2019m going to carry out is to run the MSBuild.SonarQube.Runner locally. Visual Studio 2015 Community is installed on my computer. What I need to do is:<\/p>\n<ol>\n<li>\n<p>Open a **Developer Command Prompt for VS2015 **from the Start menu.<\/p>\n<\/li>\n<li>\n<p>Navigate to the folder containing the project I want to analyze.<\/p>\n<\/li>\n<li>\n<p>Run the following commands:<\/p>\n<\/li>\n<\/ol>\n<p><span style=\"font-family: Consolas\">path=%path%;C:SqMSBuild.SonarQube.Runner-1.0.1<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner begin \/n:Backlogmaps \/v:1.0 \/k:blm<\/span><\/p>\n<p><span style=\"font-family: Consolas\">Msbuild<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner end<\/span><\/p>\n<p>The first of the commands in step 3 just sets the path to include the folder where I unzipped the MSBuild.SonarQube.Runner (and only for the duration of the command window). Note that, here, I\u2019ve chosen to set the path in the command prompt. I could have changed the environment variables permanently in the <strong>Advanced<\/strong> tab of the <strong>System Properties<\/strong> dialog (<strong>Control Panel<\/strong> | <strong>System<\/strong> | <strong>Advanced system settings<\/strong>).<\/p>\n<p>The second command downloads the quality profile to be used for my project, as well as some binaries related to the .NET analysis, from the <em>SonarQube<\/em> server installed locally on my computer (I have not changed any configuration settings).<\/p>\n<p>The third command simply runs MSBuild on my folder (I have only one solution).<\/p>\n<p>The fourth command completes the SonarQube analysis, and sends the results to the SonarQube server.<\/p>\n<p>Here are the last few lines of the console output:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image003\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/8176.clip_image003_thumb_79F4617C.png\" alt=\"clip_image003\" width=\"676\" height=\"135\" border=\"0\" \/><\/p>\n<p>Now I open the URL <strong>http:\/\/localhost:9000\/dashboard\/index\/backlogmaps<\/strong> (as indicated in the analysis results shown above) in my browser, and I get a first view of my project.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image004\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/7776.clip_image004_thumb_4007AF41.png\" alt=\"clip_image004\" width=\"700\" height=\"969\" border=\"0\" \/><\/p>\n<h4>Passing additional settings; for example, to change the analysis verbosity<\/h4>\n<p>The output produced by version 1.0 of the MSBuild.SonarQube.Runner was very verbose, so in version 1.0.1 we reduced the level of detail produced in normal operation. If you want to enable verbose log files, you can do it by settings the <strong>sonar.verbose<\/strong> property to <strong>true<\/strong>. Here is how you do it compared to the commands you saw earlier:<\/p>\n<p><span style=\"font-family: Consolas\">path=%path%;C:SqMSBuild.SonarQube.Runner-1.0.1<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner begin <b>\/d:sonar.verbose=true<\/b> \/v:1.0 \/k:blm \/n:Backlogmaps<\/span><\/p>\n<p><span style=\"font-family: Consolas\">Msbuild<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner end<\/span><\/p>\n<p>As a result, the analysis displays a lot more information \u2013 you can see the <strong>INFO<\/strong> and <strong>DEBUG<\/strong> lines in this screenshot.<img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image005\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/4111.clip_image005_thumb_6D1F2CC0.png\" alt=\"clip_image005\" width=\"704\" height=\"310\" border=\"0\" \/><\/p>\n<p>Note that the <strong>\/d:<\/strong> option is used to set any variable that SonarQube or its plug-ins understands. I\u2019ll be using it later in this post when performing analysis using third-party plug-ins.<\/p>\n<h3><a name=\"_Executing_3rd_party\"><\/a>Executing third-party analyzers locally<\/h3>\n<p>Since we released the MSBuild.SonarQube.Runner, we received several questions related to running third-party analyzers such as <em>StyleCop<\/em>, <em>ReSharper<\/em>, or <em>NDepend<\/em>. The NDepend plug-in is not officially available yet, but I\u2019ll explain in this section how you can run StyleCop and ReSharper analysis and send the results to SonarQube.<\/p>\n<h4>StyleCop<\/h4>\n<p>If you want to add StyleCop to your analysis, you\u2019ll need to:<\/p>\n<p>\u00b7 <a href=\"#_Installing_StyleCop\">Install StyleCop on your build computer<\/a><\/p>\n<p>\u00b7 <a href=\"#_Installing_the_SonarQube\">Install the SonarQube StyleCop plug-in<\/a><\/p>\n<p>\u00b7 <a href=\"#_Restarting_the_SonarQube\">Restart the SonarQube server<\/a><\/p>\n<p>\u00b7 <a href=\"#_Adding_the_StyleCop\">Add StyleCop rules to the quality profile<\/a><\/p>\n<p>\u00b7 <a href=\"#_Re-running_the_analysis\">Re-run the analysis<\/a><\/p>\n<h5><a name=\"_Installing_StyleCop\"><\/a>Installing StyleCop<\/h5>\n<p>It\u2019s really easy to install StyleCop:<\/p>\n<ol>\n<li>\n<p>Download the StyleCop MSI from <a href=\"https:\/\/stylecop.codeplex.com\/\">https:\/\/stylecop.codeplex.com\/<\/a>.<\/p>\n<\/li>\n<li>\n<p>Run the MSI to install StyleCop on your build computer.<\/p>\n<\/li>\n<\/ol>\n<p>Currently SonarQube supports StyleCop 4.7. StyleCop installed as a NuGet package is not yet supported, though we have raised a SonarQube <a href=\"https:\/\/jira.sonarsource.com\/browse\/SONARMSBRU-141\" target=\"_blank\" rel=\"noopener\">suggestion<\/a> for this.<\/p>\n<h5><a name=\"_Installing_the_SonarQube\"><\/a>Installing the SonarQube StyleCop plug-in<\/h5>\n<p>To install the SonarQube StyleCop plug-in, I chose the <strong>Login<\/strong> link at the top right of the SonarQube server web page, and signed in as the SonarQube server administrator (<strong>admin<\/strong> \/ <strong>admin<\/strong> by default). Then I navigated to <strong>Settings<\/strong> | <strong>System<\/strong> | <strong>Update Center<\/strong>, and opened the <strong>Available Plug-ins **tab. In the **External Analyzers<\/strong> section, I located \u201cStyle Cop\u201d and installed it. The link automatically downloads the plug-in from the Internet and installs it in my SonarQube server.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image006\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/4532.clip_image006_thumb_6EEF8287.png\" alt=\"clip_image006\" width=\"704\" height=\"328\" border=\"0\" \/><\/p>\n<h5><a name=\"_Restarting_the_SonarQube\"><\/a>Restarting the SonarQube server<\/h5>\n<p>Next, I restarted the SonarQube server from the **Local Services **window, as shown earlier.<\/p>\n<h5><a name=\"_Adding_the_StyleCop\"><\/a>Adding the StyleCop rules to the quality profile<\/h5>\n<p>SonarQube has the notion of <em>Quality Profiles<\/em>. A quality profile is essentially a mechanism for grouping a set of rules. Every SonarQube project has an associated quality profile per analyzed language that determines the rules that are run against that project for this language.<\/p>\n<p>After installing the StyleCop plug-in and restarting the server, SonarQube will be aware of StyleCop rules. However, these won\u2019t yet be activated in the quality profile associated with your project \u2013 you must activate them yourself. I\u2019m going to create my own quality profile named <strong>My Way<\/strong> by cloning the default <strong>Sonar Way<\/strong> profile, and add rules for the StyleCop plug-ins I\u2019ve installed.<\/p>\n<ol>\n<li>\n<p>Sign into the SonarQube server as an administrator (you must be an administrator to update the quality profile).<\/p>\n<\/li>\n<li>\n<p>Choose <strong>Quality Profiles **and select the default **Sonar Way<\/strong> profile.<\/p>\n<\/li>\n<li>\n<p>Choose <strong>Copy<\/strong>, and create a new quality profile named <strong>My Way<\/strong>.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image007\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/3808.clip_image007_thumb_50A4CB91.png\" alt=\"clip_image007\" width=\"701\" height=\"421\" border=\"0\" \/><\/p>\n<ol>\n<li>\n<p>Choose **Set as Default **in the \u201cMy Way\u201d entry to set it as the default for C# projects (I could have associated it with only my project instead).<\/p>\n<\/li>\n<li>\n<p>Choose <strong>Rules **in the top navigation toolbar and then set the **Repository<\/strong> textbox in the left-hand column<\/p>\n<\/li>\n<li>\n<p>Select **StyleCop **in the Repository list to see the list of rules.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image008\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/4034.clip_image008_thumb_7BEBF349.png\" alt=\"clip_image008\" width=\"704\" height=\"441\" border=\"0\" \/><\/p>\n<ol>\n<li>\n<p>Select all the StyleCop rules (when using a mouse you can click on the first rule, scroll to the end, and <em>Shift<\/em>-click the last one).<\/p>\n<\/li>\n<li>\n<p>Choose <strong>Bulk Change<\/strong> and then choose <strong>Activate In<\/strong>. From the drop-down list, choose the **My Way **profile.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image009\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/7607.clip_image009_thumb_5B674448.png\" alt=\"clip_image009\" width=\"704\" height=\"221\" border=\"0\" \/><\/p>\n<p>After a few seconds, SonarQube tells you that rules were added to the **My Way **quality profile.<\/p>\n<h5><a name=\"_Re-running_the_analysis\"><\/a>Re-running the analysis<\/h5>\n<p>Just as before, I run the commands:<\/p>\n<p><span style=\"font-family: Consolas\">path=%path%;C:SqMSBuild.SonarQube.Runner-1.0.1<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner begin \/n:Backlogmaps \/v:1.0 \/k:blm<\/span><\/p>\n<p><span style=\"font-family: Consolas\">Msbuild<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner end<\/span><\/p>\n<p>The result, however, is very different. Now I get 4,420 additional issues (I was a little ruthless in activating all the StyleCop rules J).<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image010\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/2437.clip_image010_thumb_3F59160E.png\" alt=\"clip_image010\" width=\"704\" height=\"679\" border=\"0\" \/><\/p>\n<h5>StyleCop: conclusion<\/h5>\n<p>Adding StyleCop to your analysis is quite easy. You just install StyleCop on your build\/analysis computer, install the StyleCop plug-in, activate the StyleCop rules in the quality profile for your project, and re-run your analysis.<\/p>\n<p>Note that the fact that you need to install StyleCop on the build computer means that you won\u2019t be able to run it with a <strong>Hosted<\/strong> build agent in Visual Studio Online. You will need to set up a private build agent. We\u2019ll do that in the last section of this post. But, before then, let\u2019s see how to perform an analysis with the ReSharper command line tool.<\/p>\n<h4>ReSharper<\/h4>\n<p>To perform an analysis with ReSharper, you carry out a very similar set of steps as you did for StyleCop. The main difference is that, in this case, you must add parameters for the analysis. The basic steps are:<\/p>\n<p>\u00b7 <a href=\"#_Installing_ReSharper\">Install ReSharper on your build computer<\/a><\/p>\n<p>\u00b7 <a href=\"#_Installing_the_SonarQube_1\">Install the SonarQube ReSharper plug-in<\/a><\/p>\n<p>\u00b7 <a href=\"#_Restarting_the_SonarQube_1\">Restart the SonarQube server<\/a><\/p>\n<p>\u00b7 <a href=\"#_Adding_the_ReSharper\">Add ReSharper rules to the quality profile<\/a><\/p>\n<p>\u00b7 <a href=\"#_Re-running_the_analysis_1\">Re-run the analysis<\/a><\/p>\n<h5><a name=\"_Installing_ReSharper\"><\/a>Installing ReSharper<\/h5>\n<p>Download the <a href=\"http:\/\/www.jetbrains.com\/resharper\/features\/command-line.html\">JetBrains ReSharper Command Line Tools<\/a> and install it on your local computer. This is a zip file, which I unzipped into the folder:<\/p>\n<p>C:Program Files (x86)JetBrains.ReSharper.CommandLineTools.9.1.20150724.155556<\/p>\n<h5><a name=\"_Installing_the_SonarQube_1\"><\/a>Installing the SonarQube ReSharper plug-in<\/h5>\n<p>Using the <strong>Update Center<\/strong> on my SonarQube server (<strong>Settings<\/strong> | <strong>System<\/strong> | <strong>Update Center<\/strong>) as an administrator, I added the ReSharper plug-in from the **Available Plug-ins **tab. This automatically downloads the plug-in from the Internet and installs it in my SonarQube server.<\/p>\n<h5><a name=\"_Restarting_the_SonarQube_1\"><\/a>Restarting the SonarQube server<\/h5>\n<p>Next, I restarted the SonarQube server from the **Local Services **window, as shown earlier.<\/p>\n<h5><a name=\"_Adding_the_ReSharper\"><\/a>Adding the ReSharper rules to the quality profile<\/h5>\n<p>After signing in to the SonarQube server as an administrator, I go to the <strong>Rules<\/strong> tab, select the <strong>ReSharper<\/strong> rules repository, select all the rules I want to include, and \u201cbulk add\u201d these rules to the quality profile. See the earlier <a href=\"#_Adding_the_StyleCop\">section about StyleCop<\/a> for more details.<\/p>\n<h5><a name=\"_Re-running_the_analysis_1\"><\/a>Re-running the analysis<\/h5>\n<p>For the analysis itself, things are different. I need to run the ReSharper command line tool after the MsBuild step:<\/p>\n<p><span style=\"font-family: Consolas\">path=%path%;C:SqMSBuild.SonarQube.Runner-1.0.1<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner begin \/n:Backlogmaps \/v:1.0 \/k:blm \/<b>d:sonar.resharper.solutionFile=%UserProfile%SourceReposBacklogmapsStoryMaps.sln<\/b> <b>\/d:sonar.resharper.cs.reportPath=c:tempReSharperResult.xml<\/b><\/span><\/p>\n<p><span style=\"font-family: Consolas\">Msbuild<\/span><\/p>\n<p><span style=\"font-family: Consolas\">&#8220;C:Program Files (x86)JetBrains.ReSharper.CommandLineTools.9.1.20150724.155556inspectcode.exe&#8221; StoryMaps.sln \/o=C:tempReSharperResult.xml<\/span><\/p>\n<p><span style=\"font-family: Consolas\">MSBuild.SonarQube.Runner end<\/span><\/p>\n<p>Note that I run ReSharper only in the fourth line, after the build, and provide the path and name of a result file. However, I still need to tell the MSBuild.SonarQube.Runner about the ReSharper report path before the build starts. The last step (calling MSBuild.SonarQube.Runner with the <strong>end<\/strong> verb) does not accept parameters. This is because some parameters must be known from beginning in order to send a request to the server containing the appropriate information.<\/p>\n<p>When we designed the MSBuild.SonarQube.Runner, we thought it would be confusing if you had to figure out which details are required in the begin step and which are required in the end step.<\/p>\n<p>Now, in the SonarQube project dashboard, I can see that the ReSharper rules have added more issues.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image011\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/5531.clip_image011_thumb_57EB440F.png\" alt=\"clip_image011\" width=\"704\" height=\"595\" border=\"0\" \/><\/p>\n<h5>ReSharper: conclusion<\/h5>\n<p>Adding ReSharper to your analysis is a little more complicated than when using StyleCop because it requires parameters. You install ReSharper on your build\/analysis computer, install the ReSharper plug-in, activate the ReSharper rules in the quality profile for your project, update the build to include the required parameters by using the **\/d: **parameter option on the command line of the MSBuild.SonarQube.Runner, and then re-run your analysis.<\/p>\n<p>Note that, as with the StyleCop example, the fact that you need to install ReSharper on the build computer means that you won\u2019t be able to run it with a <strong>Hosted<\/strong> build in Visual Studio Online. You will need to set up a private build agent, as shown in the next section of this post.<\/p>\n<h3><a name=\"_Running_the_analysis\"><\/a>Executing third-party analyzers from VSO or TFS<\/h3>\n<p>Now that you understand how the MSBuild.SonarQube.Runner works, I\u2019ll use it with Visual Studio Online (VSO) by adding the commands you saw in earlier examples to an example of a nightly build that runs in VSO. For this, I\u2019m going to use the <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2015\/08\/24\/build-tasks-for-sonarqube-analysis.aspx\">Build tasks for SonarQube analysis<\/a>, which we released three weeks ago. However, as I already mentioned, we cannot use the <strong>Hosted<\/strong> build agent if we want to run StyleCop or ReSharper because they will not be installed on the hosted agent computer. Therefore, I\u2019ll need to use a private build agent.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image012\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/8105.clip_image012_thumb_6E40E954.png\" alt=\"clip_image012\" width=\"518\" height=\"247\" border=\"0\" \/><\/p>\n<h4>Setting up a private build agent on my computer<\/h4>\n<p>As I am a Build Administrator and Agent Pool Administrator in my VSO account, I was able to install the agent on my Windows computer by following <a href=\"https:\/\/msdn.microsoft.com\/Library\/vs\/alm\/Build\/agents\/windows\">these instructions<\/a>. Alternatively, use the \u201cget started\u201d link in the <strong>BUILD<\/strong> tab in Visual Studio Online and choose the <strong>Deploy Windows agents<\/strong> link in the page that opens.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image013\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/8117.clip_image013_thumb_57156ED6.png\" alt=\"clip_image013\" width=\"604\" height=\"183\" border=\"0\" \/><\/p>\n<p>I followed the instructions to deploy the agent manually on my local computer because I did not want to install TFS on this computer.<\/p>\n<p>This was simple! I created an agent pool named <strong>JmprieurLaptop<\/strong>, and used the name <strong>Agent-JMPRIEUR-PC **for my agent. Then I added a capability to my agent (using the **USER CAPABILITIES<\/strong> page) to indicate that this agent knows about ReSharper. This allows the build system to select the appropriate agent at runtime.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image014\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/0045.clip_image014_thumb_640F4EE7.png\" alt=\"clip_image014\" width=\"704\" height=\"409\" border=\"0\" \/><\/p>\n<h4>Creating a build definition in Visual Studio Online<\/h4>\n<p>Next, I created a new build definition that includes a <strong>Visual Studio Build<\/strong> task and a <strong>Visual Studio Test<\/strong> task. Then I then inserted a **SonarQube for MSBuild &#8211; Begin Analysis **task as the first step and a **SonarQube for MSBuild &#8211; End Analysis **task as the last step.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image015\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/6740.clip_image015_thumb_6FA79CD7.png\" alt=\"clip_image015\" width=\"688\" height=\"463\" border=\"0\" \/><\/p>\n<p>I need a service endpoint for the connection to SonarQube, so I went to the VSO Control Panel and created one named <strong>SonarQube on Jm\u2019s machine<\/strong>. I used <strong>anonymous<\/strong> \/ <strong>anonymous<\/strong> as a user-name and password. These parameters cannot be empty, but the actual values don\u2019t matter because I have not secured my SonarQube installation.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image016\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/7455.clip_image016_thumb_1126CC67.png\" alt=\"clip_image016\" width=\"576\" height=\"250\" border=\"0\" \/><\/p>\n<p>Now I can use this endpoint in the parameters of the **SonarQube for MSBuild &#8211; Begin Analysis **step, where I also provided the same parameters as I used in MSBuild. Note that I did not need to provide any database parameter because I\u2019m using a simple local installation of SonarQube.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image017\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/8080.clip_image017_thumb_2305F0E5.png\" alt=\"clip_image017\" width=\"704\" height=\"552\" border=\"0\" \/><\/p>\n<h4>Adding a task to configure and invoke ReSharper<\/h4>\n<p>To invoke ReSharper during the build, I need to do two things:<\/p>\n<p>\u00b7 Add parameters to the **SonarQube for MSBuild &#8211; Begin Analysis **step to configure the build to use ReSharper.<\/p>\n<p>\u00b7 Add a <strong>Command Line<\/strong> task step after the <strong>Visual Studio Test<\/strong> step to invoke the ReSharper <strong>InspectCode<\/strong> executable.<\/p>\n<p>For the first of these, I can use the environment variables available in the build steps. You saw some of these environment variables in the <strong>USER CAPABILITIES<\/strong> list when I configured the build agent. The solution file will be <strong>$(Agent.BuildDirectory)$(Build.Repository.Name)StoryMaps.sln<\/strong>. For the output report, I can continue to use <strong>C:tempReSharperResult.xml<\/strong>.<\/p>\n<p>So, I inserted a <strong>Command Line<\/strong> task step using the parameters:<\/p>\n<p>\u00b7 <strong>Tool<\/strong>: $(ReSharper)InspectCode. <em>Notice that I\u2019m using the <strong>ReSharper<\/strong> user capability that I added to the agent when I installed it<\/em>.<\/p>\n<p>\u00b7 <strong>Arguments<\/strong>: $(Agent.BuildDirectory)$(Build.Repository.Name)StoryMaps.sln \/o=C:tempReSharperResult.xml<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image018\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/1411.clip_image018_thumb_06F7C2AB.png\" alt=\"clip_image018\" width=\"704\" height=\"406\" border=\"0\" \/><\/p>\n<p>Now, for the second change required to enable ReSharper, I need to pass the corresponding arguments (the solution file path and the output report path) to the MSBuild.SonarQube.Runner. For this, I used the **Sonar-Runner arguments **parameter. I pass as arguments the values:<\/p>\n<p><span style=\"font-family: Consolas\">\/d:sonar.resharper.solutionFile=$(Agent.BuildDirectory)$(Build.Repository.Name)StoryMaps.sln<\/span><\/p>\n<p>and<\/p>\n<p><span style=\"font-family: Consolas\">\/d:sonar.resharper.cs.reportPath=c:tempReSharperResult.xml<\/span><\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image019\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/4162.clip_image019_thumb_1D4D67F0.png\" alt=\"clip_image019\" width=\"704\" height=\"475\" border=\"0\" \/><\/p>\n<h4>Specifying the appropriate build agent to be used for the build<\/h4>\n<p>Finally, I used the <strong>General<\/strong> tab of my build definition to edit the list of <strong>Demands<\/strong> for the build agent so that it will use only the agent that has a user capability named <strong>ReSharper<\/strong> \u2013 recall that I added this to the list of user capabilities for the agent when I installed and configured it earlier.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image020\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/3683.clip_image020_thumb_6703F1F3.png\" alt=\"clip_image020\" width=\"585\" height=\"452\" border=\"0\" \/><\/p>\n<h4>Queuing the build manually<\/h4>\n<p>Now I queued the build manually, and it worked perfectly. Notice that I now get a simple summary of the SonarQube analysis in the **Miscellaneous details **section of the build result.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px\" title=\"clip_image021\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/09\/7713.clip_image021_thumb_69CEC848.png\" alt=\"clip_image021\" width=\"691\" height=\"499\" border=\"0\" \/><\/p>\n<h3>Conclusion<\/h3>\n<p>In this post, you\u2019ve seen how you can:<\/p>\n<p>\u00b7 Install a SonarQube server locally on Windows. It\u2019s quite easy to set up SonarQube on a Windows computer for trial or demo purposes, though I\u2019ve also provide pointers to setting up a production environment.<\/p>\n<p>\u00b7 Analyze a project locally with <em>MSBuild<\/em>. In this section, I showed how you use the SonarQube.MSBuild.Runner in your builds.<\/p>\n<p>\u00b7 Incorporate other code analyzers that run locally. For example, you can get extra coverage for technical debt by using analyzers such as <em>StyleCop and ReSharper.<\/em><\/p>\n<p>\u00b7 Incorporate other code analyzers in VSO or TFS. You can analyze projects with third-party analyzers using the SonarQube Build vNext analysis tasks in VSO or TFS.<\/p>\n<p>We look forward to hearing from you. Please send us your feedback by asking some questions on <a href=\"http:\/\/stackoverflow.com\/questions\/tagged\/sonarqube\" target=\"_blank\" rel=\"noopener\">StackOverflow<\/a> or directly by reporting any bugs you find on the <a href=\"https:\/\/groups.google.com\/forum\/#!forum\/sonarqube\" target=\"_blank\" rel=\"noopener\">SonarQube Google Group<\/a>. You can also submit suggestions for new features, for example, on <a href=\"http:\/\/visualstudio.uservoice.com\/\" target=\"_blank\" rel=\"noopener\">User Voice<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post provides a quick-start guide to using SonarQube to analyze .NET managed code. It covers installing SonarQube locally, running your first analysis using MSBuild, and using some popular third-party analyzers. It also describes how to use the new Visual Studio Online (VSO) and Team Foundation Server (TFS) Build tasks to perform analysis as part [&hellip;]<\/p>\n","protected":false},"author":112,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[253,229,1,249],"tags":[],"class_list":["post-9981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-devops-server","category-community","category-devops","category-open-source"],"acf":[],"blog_post_summary":"<p>This post provides a quick-start guide to using SonarQube to analyze .NET managed code. It covers installing SonarQube locally, running your first analysis using MSBuild, and using some popular third-party analyzers. It also describes how to use the new Visual Studio Online (VSO) and Team Foundation Server (TFS) Build tasks to perform analysis as part [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=9981"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9981\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=9981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=9981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=9981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}