且构网

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

如何使用 nuget 包资源管理器创建具有发布和调试 dll 的 nuget 包?

更新时间:2023-09-26 08:21:04

我的想法是,NuGet 打包与约定有关.

My thoughts are, NuGet packaging is a lot about conventions.

不同平台打包相同的命名空间和相同的名称没有问题(如lib/net40/mydll.dlllib/net35/mydll.dll 等),因为 NuGet 将按平台过滤注册的依赖项.

There is no problem in packaging same namespaces and same names for different platforms (as in lib/net40/mydll.dll, lib/net35/mydll.dll etc in the same package), as NuGet will filter registered dependencies by platform.

为同一平台构建多个版本似乎非常规这个讨论 偏向于为每个构建制作一个包.这并不意味着你不能这样做,但你应该先问问自己是否应该这样做.

Building several versions for the same platform seems unconventional, this discussion is biased towards making a package per build. That doesn't mean you can't do it, but you should first ask yourself if you should.

也就是说,如果您的调试和发布版本非常不同(条件编译等),这可能很有用.但是,最终用户在安装您的软件包时将如何选择发布或调试?

That said, if your debug and release builds are very different (conditional compiling etc) this might useful though. But how will end-users choose Release or Debug when installing your package?

一个想法可能是,每个构建配置一个版本.两者都可以安装进入项目.为此,请添加 将文件定位到您的包 或构建 一个 powershell 安装脚本(不支持,因为 Nuget v3) 直接在目标项目文件中添加条件引用,如果你想要一些比 MsBuild 可以做的更简单的东西给你.

An idea could be, one version per build configuration. Both can be installed into the project. To do that, either add a targets file to your package or build a powershell install script (unsupported since Nuget v3) that adds conditional references directly in the target project file, if you want something less basic than whatever MsBuild can do for you.

第一种策略的示例:创建一个 .target 文件(在您的包中,创建一个 build 文件夹,然后创建具有以下内容的 buildYourLib.targets):

Example of the first tactic: Create a .target file (in your package, create a build folder and then create buildYourLib.targets with the following contents):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <Reference Include="YourLib">
      <HintPath>..packagesYourLib.1.0.0libDebugYourLib.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <Reference Include="YourLib">
      <HintPath>..packagesYourLib.1.0.0libReleaseYourLib.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

如果您创建了 debug 和 release 文件夹(平台文件夹是可选的),构建输出将根据配置有效地改变 - 假设数据包消费者具有常规配置名称,但您始终可以扩展条件逻辑使用 $(Configuration).Contains etc 或将其放入包自述

Providing you created debug and release folders (platform folder is optional), the build output will effectively change depending on configuration - provided packet consumers have conventional configuration names, but you could always extend the condition logic a bit with $(Configuration).Contains etc or just put that in the package readme