从 dls 创建 nuget 包

我想创建一个 NuGet 包,它添加多个. dll 文件作为对我的项目的引用。

我有一个文件夹,里面有10.dlls 文件。

当我通过 nuget 安装这些文件时,我希望这些文件被添加到项目的引用中。

108321 次浏览

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

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.

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>

The best I found for nuget packages is the dotnet CLI if you are working on .NET projects (SDK and non SDK styles). Here is a video which explains easily how to create a nuget package using dotnet CLI: https://www.youtube.com/watch?v=SAVDcgP3lyM