且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 NuGet 向 .net 核心项目提供代码分析规则集

更新时间:2021-08-25 15:17:47

这个想法是不要尝试将文件部署为内容,而是将构建逻辑添加到 NuGet 包.

The idea is to not try to deploy the file as content but add build logic to the NuGet package.

确保包的结构如下:

  • 构建
  • 构建custom.ruleset
  • build{YourPackageName}.targets(例如 CustomRuleset.targets)

这种结构会导致 .targets 文件按照约定自动导入到使用项目中.

This structure causes the .targets file to be automatically imported into the consuming project by convention.

.targets 文件应包含:

The .targets file should then contain:

<Project>
  <PropertyGroup>
    <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)custom.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
</Project>

这将导致项目的规则集属性被覆盖到相对于 .targets 文件的位置.

This will cause the project's ruleset property to be overwritten to the location relative to the .targets file.

请注意,这也适用于使用新 PackageReference 样式的 NuGet 包(替换 packages.config)的 .net 框架项目,该样式在 VS 2017 中是可选的(15.2+).

Note that this also applies to .net framework projects using the new PackageReference style of NuGet packages (replacement of packages.config) which is opt-in in VS 2017 (15.2+).