Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to Create a Nuget Package on Each Visual Studio Build with NuPack

4.80/5 (16 votes)
7 Jun 2017CPOL2 min read 34K  
NuPack is a nuget package used to add a build step to a project in order to pack output into a nuget package.

Introduction

Nuget is a good way to distribute libraries and even more, there exist a bunch of UI and command lines to manage nuget package. I used to execute command line to automatically produce nuget package from project or solution but it is limited to most common pattern (library packaging). I often use Nuget Package Explorer to create and update my nuget package because it allows you to manipulate package as per your needs. Unfortunately, it is very non productive to manually edit a nuget package when it is done often. That's why it can be interesting to use a nuget package like NuPack to do the job.

NuPack

NuPack is a nuget package for .NET 4.0+ that adds an additional build step to pack output of project into a nuget package. It is simple to add, use or remove because it is materialized by a single nuget package without any additional task to do.

This nuget package is an open source project and is under MIT Licence that allows you to use it without any restrictions.

Automatic

NuPack automatically creates a nuget package on each build with assembly name as id. It can work with 2 templates:

  1. Library: Pack a library assembly in lib folder and declare dependencies automatically

    Reference NuPack from nuget.org

    Image 1

    Define Assembly Attribute to Provide Clean Metadata

    Image 2

    NuPack Nuget Package Creation on Build Project

    Image 3

    NuPack Nuget Package Creation Observed with Nuget Package Explorer

    Image 4

  2. Console: Pack the console as util and provide a build step through .targets file to call console application with 5 arguments:
    1. $(SolutionPath)
    2. $(ProjectPath)
    3. $(Configuration)
    4. $(PlatformName)
    5. $(TargetPath)

Automatic Nuget Package from Console Application Observed with Nuget Package Explorer

Image 5

Manual

NuPack detects .nuspec file when it is part of project and simply respects the specification to create the expected nuget package.

Image 6

Dependencies

NuPack automatically manages dependencies by including nuget package dependencies and all resources (all files found in output path) into lib folder except files found in nuget dependencies and specific files like .pdb, .bak, .tmp, .vshost.exe.

Example: Use Cases

  • When a project references a nuget package, NuPack will include it in package as dependency.
  • If a project references another as project reference, NuPack will include output of dependency into package in lib folder.
  • If a dependency is a project reference but is a nuget producer (using NuPack ou produce by another way a .nupkg into output), NuPack will include it as nuget dependency.

References

Conclusion

There are many ways to create a package nuget. NuPack is one of them and can be useful in some scenarios. It is easy to use without any effort and can help to keep your nuget package up to date as soon as possible without deploying a white elephant.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)