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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"> <metadata> <id>Jenx.Nuget.CopyToBinDemo</id> <version>1.0.0.0</version> <title>Jenx.Nuget.CopyToBinDemo</title> <authors>jenx.si</authors> <owners>jenx.si</owners> <iconUrl>https://www.jenx.si/favicon.ico</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>NuGet demo for copy-to-bin artifact.</description> <copyright>Jenx.si - 2019</copyright> </metadata> <files> <file src="Content\CopyToBinConfig.xml" target="content\ExternalConfig\CopyToBinConfig.xml" /> </files> </package> |
Creating NuGet package from nuspec file is simple:
1 |
nuget pack Jenx.NuGet.CopyToBin.nuspec |
If I install this NuGet I get:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"> <metadata> <id>Jenx.Nuget.CopyToBinDemo</id> <version>1.0.0.1</version> <title>Jenx.Nuget.CopyToBinDemo</title> <authors>jenx.si</authors> <owners>jenx.si</owners> <iconUrl>https://www.jenx.si/favicon.ico</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>NuGet demo for copy-to-bin artifact.</description> <copyright>Jenx.si - 2019</copyright> </metadata> <files> <file src="Content\CopyToBinConfig.xml" target="content\ExternalConfig\CopyToBinConfig.xml" /> <file src="Tools\install.ps1" target="tools" /> </files> </package> |
I just add install.ps1
which did the work!
1 2 3 4 |
param($installPath, $toolsPath, $package, $project) # change deployed artifact to copy-to-bin $project.ProjectItems.Item("ExternalConfig").ProjectItems.Item("CopyToBinConfig.xml").Properties.Item("CopyToOutputDirectory").Value = 1 |
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.