Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.80/5 (16 votes)
7 Jun 2017CPOL2 min read 32.3K   18   10
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)


Written By
Architect
France France
After working in electronics, I fall in love with software development which is a real passion for me, particulary architecture concerns for productivity.

Comments and Discussions

 
QuestionAuthors and Owners Pin
Code Artist31-May-18 2:52
professionalCode Artist31-May-18 2:52 
QuestionWill this work with MSBuild Pin
Dave Moor (the real one)13-Jun-17 9:35
Dave Moor (the real one)13-Jun-17 9:35 
AnswerRe: Will this work with MSBuild Pin
Puresharper13-Jun-17 10:59
professionalPuresharper13-Jun-17 10:59 
PraiseInteresting tool. Pin
n.podbielski8-Jun-17 9:37
n.podbielski8-Jun-17 9:37 
GeneralRe: Interesting tool. Pin
Puresharper8-Jun-17 11:09
professionalPuresharper8-Jun-17 11:09 
GeneralRe: Interesting tool. Pin
n.podbielski8-Jun-17 20:27
n.podbielski8-Jun-17 20:27 
QuestionVS2017 support? Pin
Member 20526146-Jun-17 19:57
Member 20526146-Jun-17 19:57 
AnswerRe: VS2017 support? Pin
Puresharper8-Jun-17 0:03
professionalPuresharper8-Jun-17 0:03 
PraiseGreat Tool Pin
shaib.vami3-Jun-17 7:05
shaib.vami3-Jun-17 7:05 
GeneralMessage Closed Pin
1-Jun-17 20:06
Member 132237681-Jun-17 20:06 
NewsRe: My vote of 4 Pin
Paw Jershauge1-Jun-17 21:44
Paw Jershauge1-Jun-17 21:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.