99

I want to create a NuGet package which adds multiple .dll files as references to my project.

I have a folder with 10 .dlls files in it.

When I install this via nuget, I want these files to be added to the project's references.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Danhol86
  • 1,352
  • 1
  • 11
  • 20
  • There is no magic tooling to automate that, and you have to learn .nuspec and pack up the assemblies that way. – Lex Li Apr 07 '17 at 14:24
  • 1
    @LexLi Wrong. See the accepted answer. – Neo Aug 15 '19 at 13:51
  • @Neo Both "NuGet Package Explorer" and "nuget spec/pack" use .nuspec implicitly. I wonder from where your comment comes from. – Lex Li Aug 15 '19 at 13:56
  • 3
    @LexLi NuGet Package Explorer is "magic tooling" that automates the process of creating a NuGet package containing DLLs, and then publishing it to NuGet. All you have to specify is which DLLs should be included, any meta data and the URL/key for publishing. This is what the OP wanted. I just did it today, and didn't even have to look at a .nuspec file once, and now have other projects referencing my package and including the requiring DLLs as desired. So I did not have to "learn .nuspec" like you suggested. – Neo Aug 15 '19 at 15:38
  • https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#advanced-extension-points-to-create-customized-package this is what helped me for same problem – Rupinder Kaur Feb 07 '22 at 12:25

3 Answers3

121

I want to create a nuget package which adds multiple .dll as references to my project.

I would like give you two solutions to achieve this:

First, Use NuGet Package Explorer

  1. Download the NuGet Package Explorer.
  2. Open the NuGet Package Explorer, select the create a new package.
  3. Add a lib folder on the content tab, and add your dlls file
  4. Save the package and install it to the project, check if it add references.

NuGet Package Explorer GUI

Second, Just as Lex Li mention, We could use .nuspec to pack up the assemblies:

  1. Download the nuget.exe.

  2. Create a new project.

  3. Open a cmd and switch path to nuget.exe

  4. Use command line: nuget spec "PathOfProject\TestDemo.csproj"

  5. Open the TestDemo.csproj.nuspec file and modify it and add the assemblies as file; below is my .nuspec file:

    <?xml version="1.0"?>
    <package>
      <metadata>
        <id>TestDemo</id>
        <version>1.0.0</version>
        <authors>Tester</authors>
        <owners>Tester</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>TestDemo</description>
        <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
        <copyright>Copyright 2017</copyright>
        <tags>Tag1 Tag2</tags>
      </metadata>
      <files>
        <file src="MultipleDll\*.*" target="lib\net461" />
      </files>
    </package>
    
  6. Use pack command: nuget pack TestDemo.csproj.nuspec

  7. Open the TestDemo package by NuGet Package Explorer.

NuGet Package Explorer - built package output

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 4
    I am trying to make my first NuGet package and I learned about NuGet Package Explorer from you. Super easy. One question: Do I need to put the DLLs inside a "native" subfolder of the lib folder? (your second screenshot suggest such a subfolder) ![screenshot](https://imgur.com/a/Oxu1yFT). – Mike Jun 10 '18 at 08:40
  • @Mike If the ClassLibrary in this example has dependencies for instance to 4.6.1 you need to add the lib and add them dlls to the lib. Else you'll get dependency issues when installing the package later. – Dynamic_Pace Jun 15 '18 at 12:33
  • I'd suggest completing the installation steps mentioned on the [NuGet CLI reference page](https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference) to efficiently use nuget.exe in your environment. It's basically adding the path to the folder containing nuget.exe in the Path environment variable to be able to use it from anywhere on the system. – Alexis Leclerc Mar 21 '19 at 16:29
45

I think the best way to create NuGet packages is use nuget.exe.

  1. First, download and install nuget.exe (https://www.nuget.org/downloads)
  2. Then go to your project folder, press shift + right click to display command prompt
  3. In the command prompt, enter

    nuget spec
    
  4. You will now have a .nuspec file. Open it in an editor and add Id, author, etc.

  5. The most important part is the files tag after closed metadata tag.

    You can define all dlls here like this:

    <files>
       <file src="bin\Release\YourDll.dll" target="lib"></file>
    </files>
    
  6. Finally, you can create .nupkg file with this command:

    nuget pack 'Your_nuspec_file_name'
    

This video on How to Create Nuget Packages has a very useful and clear tutorial.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
masehhat
  • 650
  • 6
  • 10
9

If you want to do it through Visual Studio or dotnet, then you can edit your csproj file, add an ItemGroup to include the dlls as below: This will pack the other dlls along with your current project dll in the nuget package.

<ItemGroup>
    <Content Include="<path to other dll>">
        <Pack>true</Pack>
        <PackagePath>lib\$(TargetFramework)</PackagePath>
    </Content>
</ItemGroup>
Akhil Dabral
  • 760
  • 1
  • 10
  • 11