DevelopmentDevOps

NuGet package with “Copy Always” content file

 

Introduction

NuGet is awesome package manager for Microsoft development platform. It’s everywhere in the .NET ecosystem. With NuGet, package dependency management is really easy task!

Problem

Creating standard NuGet package is very simple. I want to create NuGet package to deploy file and mark this file as “Copy Always” in C# project. This scenario is very common in cases where some file must be shipped with host application, i.e. configuration files. Thus, these files must be deployed to output folder at build time!

Source Code

All source code for this prototype is here: https://github.com/josipx/Jenx.NuGet.CopyToBin.Demo

Let’s start

1) Creating NuGet package containing “content” artifact (ConfigToBinConfig.xml)

Code: https://github.com/josipx/Jenx.NuGet.CopyToBin.Demo/tree/master/Jenx.NuGet.CopyToBin

Creating NuGet package from nuspec file is simple:

If I install this NuGet I get:

NuGet content artifact with “Do not copy” flag.

Ok, next phase is to add PowerShell script which will enable copy to output functionality.

2) Creating NuGet package containing “content” artifact with copy-to-output flag.

Code: https://github.com/josipx/Jenx.NuGet.CopyToBin.Demo/tree/master/Jenx.NuGet.CopyToBin2

NuGet content artifact with “Copy Always” flag!

I just add install.ps1 which did the work!

Conclusion

NuGet is very powerful package management tool for .NET ecosystem. Underlying PowerShell scripting provides very flexible and powerful handling of various scenarios.

In this post, I show how to include file in C# project and mark it as “Copy Always” in order to get it deployed to the output folder at build time.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.