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

后端 未结 3 1635
半阙折子戏
半阙折子戏 2020-12-09 04:32

I\'m using the Nuget Package Explorer to create some nuget packages. I\'ve managed to do so just building a project in Release mode in VS and adding both the dll and pdb fil

相关标签:
3条回答
  • 2020-12-09 04:47

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

    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?

    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.

    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):

    <?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

    0 讨论(0)
  • 2020-12-09 04:50

    Inspired by @Tewr I've found a cumbersome but a working solution.

    Create a nuget with the following file structure:

    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:

    <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. In the end, it will delete the lib\net\$(Configuration) folder

    Enjoy (or not - I personally don't like the solution).

    0 讨论(0)
  • 2020-12-09 05:02

    Thanks @Tewr In new nuget format and sdk style csproj format, we can use some constant as $(MSBuildThisFileDirectory) to get the current file path.

    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.

    We can add the targets file to build folder and use $(MSBuildThisFileDirectory) to get the file path.

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

    See the file

    0 讨论(0)
提交回复
热议问题