We are trying to conditionally exclude projects from analysis as we don’t want to analyze common projects in are part of multiple solutions. We could easily achieve conditional analysis by following article Advanced SonarQube Scanner for MSBuild configuration. Please refer Explicitly associating an MSBuild project with a SonarQube project section in above mentioned article.
Conditional analysis is working fine with msbuild.exe but not working with dotnet.exe. We think this is happening because ImportBefore targets are not getting loaded in case of dotnet.exe.
Following are the steps we have performed.
Saved targets file with content given below at C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Microsoft.Common.targets\ImportBefore
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition=" $(SQProjectKey) != '' AND $(SonarQubeExclude) == '' "> <SonarQubeExclude Condition=" $(SQProjectKey) != $(TargetSQProjectKey) " >true</SonarQubeExclude> </PropertyGroup>
Added following in csproj file.
Start analysis by running following
SonarScanner.MSBuild.exe begin /k:"Key1"
Run the build.
dotnet msbuild my.sln -property:SQProjectKey=Key1
Please note conditional analysis works as expected if we change command used in Run the build to
msbuild.exe my.sln /p:SQProjectKey=Key1
How do we solve this problem? We don’t want to use msbuild.exe .net core projects.