简体   繁体   English

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

[英]How to create a nuget package with both release and debug dll's using nuget package explorer?

I'm using the Nuget Package Explorer to create some nuget packages.我正在使用 Nuget 包资源管理器来创建一些 nuget 包。 I've managed to do so just building a project in Release mode in VS and adding both the dll and pdb files to the package.我已经设法做到了,只是在 VS 中以 Release 模式构建一个项目并将 dll 和 pdb 文件添加到包中。

So far so good, but when I add the package to another project and try to step into the code while debugging, it will step over it instead.到目前为止一切顺利,但是当我将包添加到另一个项目并尝试在调试时单步执行代码时,它会跳过它。

I understand that I need to build and add the Debug dll and pdb to my package if I want to step into the code while debugging.我知道如果我想在调试时单步执行代码,我需要构建 Debug dll 和 pdb 并将其添加到我的包中。 I'm not sure though how to add these to the package I've already create, which already contains the Release dll and pdb file, which are named the same.我不确定如何将这些添加到我已经创建的包中,该包已经包含名称相同的 Release dll 和 pdb 文件。

Any thoughts?有什么想法吗?

My thoughts are, NuGet packaging is a lot about conventions.我的想法是,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.不同的平台打包相同的命名空间和相同的名称没有问题(如在同一包中的lib/net40/mydll.dlllib/net35/mydll.dll等),因为 NuGet 将按平台过滤注册的依赖项。

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?但是,在安装您的软件包时,最终用户将如何选择 Release 或 Debug 呢?

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.为此,如果您想要一些比 MsBuild 可以为您做的更简单的事情,请向您的包中添加一个目标文件或构建 一个 powershell 安装脚本 (自Nuget v3起不受支持),该 脚本 直接在目标项目文件中添加条件引用。

Example of the first tactic: Create a .target file (in your package, create a build folder and then create build\\YourLib.targets with the following contents):第一种策略的示例:创建一个 .target 文件(在您的包中,创建一个build文件夹,然后使用以下内容创建build\\YourLib.targets ):

<?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>..\packages\YourLib.1.0.0\lib\Debug\YourLib.dll</HintPath>
    </Reference>
  </ItemGroup>

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

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如果您创建了 debug 和 release 文件夹(platform 文件夹是可选的),构建输出将根据配置有效地改变 - 如果数据包使用者具有传统的配置名称,但您始终可以使用$(Configuration).Contains 等稍微扩展条件逻辑或者只是把它放在包自述文件中

Inspired by @Tewr I've found a cumbersome but a working solution.受到@Tewr 的启发,我找到了一个繁琐但有效的解决方案。

Create a nuget with the following file structure:使用以下文件结构创建一个 nuget:

lib\net\$(Configuration)\YourLib.1.0.0.dll    <---- put here some dummy file  named YourLib.1.0.0.dll
tools\release\YourLib.1.0.0.dll  <--- put here the release version
tools\debug\YourLib.1.0.0.dll  <--- put here the debug version
build\YourLib.targets  

The targets file content:目标文件内容:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CopyReferences" BeforeTargets="Build" Condition="Exists('..\packages\YourLib.1.0.0\lib\net\%24(Configuration)')">     
    <Exec Command="mkdir ..\packages\YourLib.1.0.0\lib\net\Release" />
    <Exec Command="mkdir ..\packages\YourLib.1.0.0\lib\net\Debug" />
    <Exec Command='copy "..\packages\YourLib.1.0.0\tools\Release\YourLib.1.0.0.dll" "..\packages\YourLib.1.0.0\lib\net\Release"' />
    <Exec Command='copy "..\packages\YourLib.1.0.0\tools\Debug\YourLib.1.0.0.dll" "..\packages\YourLib.1.0.0\lib\net\Debug"' />
    <Exec Command='rmdir /S /Q "..\packages\YourLib.1.0.0\lib\net\%24(Configuration)"' />
</Target>

The dlls in lib folder will be automatically added as references creating the following in the project file: lib文件夹中的 dll 将自动添加为引用,在项目文件中创建以下内容:

<Reference Include="YourLib>   
    <HintPath>..\packages\YourLib.1.0.0\lib\net\$(Configuration)\YourLib.1.0.0.dll</HintPath>
    <Private>True</Private>
</Reference>

Once you build the project for the first time, the target will copy the release and debug version from tools\\release and tools\\debug folders to lib\\net\\release and lib\\net\\debug folders.首次构建项目后,目标会将发布和调试版本从tools\\releasetools\\debug文件夹复制到lib\\net\\releaselib\\net\\debug文件夹。 In the end, it will delete the lib\\net\\$(Configuration) folder最后会删除lib\\net\\$(Configuration)文件夹

Enjoy (or not - I personally don't like the solution).享受(或不享受 - 我个人不喜欢该解决方案)。

Thanks @Tewr In new nuget format and sdk style csproj format, we can use some constant as $(MSBuildThisFileDirectory) to get the current file path.谢谢@Tewr 在新的nuget格式和 sdk 样式的 csproj 格式中,我们可以使用一些常量作为$(MSBuildThisFileDirectory)来获取当前文件路径。

The code that uses the version will increase maintenance difficulty.使用版本的代码会增加维护难度。 The sdk style csproj format use the new package format that will do not output the package file to package folder. sdk 风格的 csproj 格式使用新的包格式,不会将包文件输出到包文件夹。

We can add the targets file to build folder and use $(MSBuildThisFileDirectory) to get the file path.我们可以将目标文件添加到构建文件夹并使用$(MSBuildThisFileDirectory)获取文件路径。

<ItemGroup Condition="'$(Configuration)' == 'DEBUG'">
    <Reference Include="YourLib">
        <HintPath>$(MSBuildThisFileDirectory)..\lib\debug\YourLib.dll</HintPath>
    </Reference>
</ItemGroup>

See the file查看文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM