0

我创建了一个类库并将其上传到 NuGet.org。类库使用 C# 源代码生成器来生成重复代码(例如使用值元组的方法)。

类库在 NuGet.org 上过着幸福的生活,但是不知何故,源生成器项目也被打包和上传,这不是我想要的......

如何防止源生成器项目上传到 NuGet?


.csproj类库的:

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
      <TargetFramework>net5.0</TargetFramework>
   </PropertyGroup>

   <PropertyGroup>
      <!-- Version, Authors, Description and other NuGet package info abbreviated here. -->
   </PropertyGroup>

   <ItemGroup>
      <ProjectReference
         Include="..\MyLib.SourceGenerators\MyLib.SourceGenerators.csproj"
         OutputItemType="Analyzer"
         ReferenceOutputAssembly="false"/>
   </ItemGroup>
</Project>

.csproj源生成器的:

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
      <TargetFramework>netstandard2.0</TargetFramework>
      <LangVersion>9.0</LangVersion>
   </PropertyGroup>

   <ItemGroup>
      <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0"/>
      <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
         <PrivateAssets>all</PrivateAssets>
         <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
   </ItemGroup>
</Project>
4

1 回答 1

0

添加<IsPackable>false</IsPackable>.csproj源生成器项目似乎可以防止生成 NuGet 包,类似于单元测试项目。

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
      <TargetFramework>netstandard2.0</TargetFramework>
      <LangVersion>9.0</LangVersion>
      <IsPackable>false</IsPackable> <!-- HERE -->
   </PropertyGroup>

   <ItemGroup>
      <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0"/>
      <!-- ... -->
于 2021-08-10T12:04:06.727 回答