【问题标题】:Roslyn Analyzers with JenkinsJenkins 的 Roslyn 分析器
【发布时间】:2016-07-24 14:18:02
【问题描述】:

我们在 Visual Studio 2015 中使用 SonarLintStyleCop 分析器。它们在开发过程中运行良好,但想知道是否可以在 Jenkins 构建期间运行它们?

我见过 SonarLint.Runner,所以我认为这是可能的。

【问题讨论】:

    标签: c# jenkins visual-studio-2015 roslyn-code-analysis


    【解决方案1】:

    我对 Jenkins 不太熟悉,但您始终可以将分析器作为 NuGet 包安装到项目中,并让它们在 MsBuild 构建过程中生成警告和错误:

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

    1. 如果您希望构建在出现警告时失败,则可选择配置各个规则的严重性并将警告视为错误。

    2. 使用 MSBuild 编译这样配置的项目将导致分析器警告和错误以与编译器错误和警告相同的方式出现。您也应该能够在 Jenkins 中以同样的方式对待它们:

      "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:你是说,你没有得到我上面显示的输出?您检查是否使用了正确版本的 MsBuild?它应该是 14.0 版本,具体取决于安装的更新。
    • 我会检查 msbuild 版本,但我只在 Visual Studio 中收到警告。
    • 这应该可以工作(这正是我在 Jenkins 中获取构建错误所做的工作)。您可能在配置设置中缺少<TreatWarningsAsErrors>true</TreatWarningsAsErrors> 这个。我认为这是必需的
    • 经过测试,我的 MSBuild 路径指向 Windows 目录中的 MSBuild,而不是 Program Files 中的路径。在程序文件目录中运行一个确实包括 roslyn 分析器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多