Roslyn分析器与Jenkins

5
我们正在使用Visual Studio 2015中的SonarLintStyleCop分析器。这些在开发过程中非常好用,但我想知道是否可以在Jenkins构建期间运行它们?我看到了SonarLint.Runner,所以我认为这是可能的。
1个回答

2
我不是很熟悉Jenkins,但您可以将分析器安装为NuGet包到项目中,并让它们作为MsBuild构建的一部分生成警告和错误:

  1. 将以下2个NuGet包安装到要分析的项目中:SonarAnalyzer.CSharpStyleCop.Analyzers

Project with analyzers installed

  1. Optionally configure the severity of individual rules and treat warnings as errors if you want your build to fail in case of warnings.

  2. Using MSBuild to compile a project configured like that will result in analyzers warnings and errors surfacing the same way as compiler errors and warnings. You should be able to treat them identically in Jenkins, as well:

    "D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj" (default target) (2) ->
    (CoreCompile target) ->
      Class1.cs(13,17): warning CS0219: The variable 'a' is assigned but its value is never used [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(1,1): warning SA1652: Enable XML documentation output [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(13,23): warning SA1002: Semicolons must not be preceded by a space. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(1,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(2,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(3,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(4,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(5,1): warning SA1200: Using directive must appear within a namespace declaration [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(1,1): warning SA1652: Enable XML documentation output [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(5,77): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(17,76): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(18,74): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(28,22): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Properties\AssemblyInfo.cs(32,84): warning SA1028: Code must not contain trailing whitespace [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(13,19): warning S1854: Remove this useless assignment to local variable "a". [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
      Class1.cs(13,17): warning S1481: Remove this unused "a" local variable. [D:\Users\Damir\Temp\SO_36412838_Analyzers\SO_36412838_Analyzers\SO_36412838_Analyzers.csproj]
    
        18 Warning(s)
        0 Error(s)
    

这正是我设置的方式,但是通过命令行运行构建不会生成任何警告。 - ScottD
@ScottD:你的意思是说,你没有得到我上面展示的输出吗?你检查了使用的正确版本的MsBuild吗?它应该是14.0版本或者更高,具体取决于安装的更新。 - Damir Arh
我会检查msbuild的版本,但我只能在Visual Studio中看到警告。 - ScottD
这应该可以工作(这正是我在Jenkins中获取构建错误的方法)。你可能在配置设置中缺少 <TreatWarningsAsErrors>true</TreatWarningsAsErrors>。我认为这是必需的。 - FUR10N
经过测试,我的 MSBuild 路径指向 Windows 目录中的 MSBuild,而不是 Program Files 中的路径。运行 Program Files 目录中的 MSBuild 包括 roslyn 分析器。 - ScottD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接