有没有办法在 Visual Studio 2019 年发布 .NET 标准库而不生成 .nupkg?

Is there a way to publish a .NET Standard Library without producing a .nupkg in Visual Studio 2019?

我的理解是,当您发布 .NET 标准库时,预期的行为是生成 .nupkg

我维护内部 .Net 标准库以用于内部项目。典型的工作流程是发布库,然后将发布的文件复制到使用项目。这不是我应该做的事吗?我对发布 .NET 标准库时缺少选项感到有点困惑 -- 因为 .nupkg 没有选择以不同的形式发布。

想要从发布中获得的只是发布目录中(不在 nupkg 中)的必要库文件的集合。有办法吗?

My understanding is that when you publish a .Net Standard Library, the expected behavior is to produce a .nupkg. What I want to get from publish is just a collection of necessary library files in the publish directory (not inside a nupkg). Is there a way to do this?

对于普通的 .net 框架项目,如果您引用一个 nuget 包并构建它。然后您会得到一个输出文件夹(bin\debug 或 bin\release),其中包含 Project.dll 和 Package.dll。这就是你想要的。

但是对于.net standard项目,默认只会输出Project.dll。这也是预期的行为,请参阅 comment1, comment2. And that's why I suggest you need to manually edit the xx.csproj to get your expected behavior during build. That's by design that it won't copy the nuget dependencies to output, and 是可能满足您需求的解决方法。

为什么右键项目节点=>Publish in VS IDE(.net标准项目)时出现publish output is a xx.nupkg,请查看this document:.NET Standard 参考程序集的主要分发工具是 NuGet 包。 我认为这就是 Publish 的输出是 xx.nupkg 的原因,很难说但是,它可能是设计使然...

编辑:

Is there a way to publish a .Net Standard Library without producing a .nupkg in Visual Studio 2019?

如果您指的是 .net 标准项目的 Publish 选项。(在解决方案资源管理器中右键单击 .net 标准项目的项目名称=>发布)。恐怕答案是否定的。此选项是设计使然,目前不支持 publish a folder with all referenced assemblies 等其他选项。您可能需要添加 Feature Request in DC.

希望我的解释能有所帮助,如果我有任何误解,请随时纠正我:(

A typical workflow would be to publish the libraries and then copy the published files to consuming projects. Is this not the way I should be doing things?

没有。这几乎是最糟糕的做事方式。

If/as 你没有使用本地生成的 Nuget 包你应该使用 project references:

When you have a project that produces an assembly, you should reference the project and not use a file reference. The advantage of a project-to-project reference is that it creates a dependency between the projects in the build system.

包含引用一个或多个内部库的项目的每个解决方案还应包含解决方案中的库(以及这些库引用的任何内部库)。

我将 运行 通过创建一些一次性项目,但这些技术也适用于现有代码 - 您只需将它们添加到解决方案中而不是创建新的:

  • 创建 ClassLibrary1 目标 .NET Standard 2.0
  • 向解决方案添加一个新项目:ClassLibrary2 面向 .NET Standard 2.0
  • 让我们安装一个流行的 Nuget 包到 ClassLibrary2Newtonsoft.Json
  • 在解决方案资源管理器中右键单击 ClassLibrary1,单击 Add,然后单击 Reference。在项目选项卡中勾选 ClassLibrary2 并单击确定。 ClassLibrary1 现在具有对 ClassLibrary2 的项目引用并且以下内容已添加到 ClassLibrary1.csproj
  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" />
  </ItemGroup>
  • 添加 .NET 控制台应用程序项目,ConsoleApp1

  • ClassLibrary 的项目引用添加到 ConsoleApp1

  • 保存解决方案

  • 构建,带发布配置,解决方案

  • 我们需要的一切都在控制台应用程序的 bin 文件夹中,看不到 Nupkg 文件夹:

  • 如果我们创建另一个解决方案并将我们的库添加到其中,或者重用该解决方案,然后向其中添加一个 .NET Core 控制台应用程序,并让该应用程序获取对 ClassLibrary1 并构建,文件夹树中的任何地方都没有 Nupkg。

  • 但是,我们发布 .NET Core 应用程序。至少,我喜欢将我的 .NET Core 应用程序(供内部使用)发布为 'self-contained'。为此,我在解决方案资源管理器中右键单击 .NET Core 应用程序,单击 Publish,然后选择 Folder 作为目标。我点击 Create Profile 然后 Edit 并将部署模式更改为 self-contained,然后保存。然后我发布应用程序(不是 库)并且我在我的发布文件夹中获得了我需要的所有文件,包括内部库和.NET Core(不用担心,Newtonsoft DLL 也在那里,只是在屏幕截图中看不到):

我唯一一次构建 Nuget 包是:

  • 如果我明确发布内部库,或者

  • 如果我在项目文件中有<GeneratePackageOnBuild>true</GeneratePackageOnBuild>[example]

如果您要通过私有包提要分发库,那么 "Nuget way" 是一种很好且有效的方法;但由于那不是您想要做的 - 不要发布库!取而代之的是对它们的项目引用,这意味着将库添加到使用它们的解决方案中