Yesterday, I was assigned to fix a nuget package solution that was not packing all the needed files. I had a bad time searching online for answers, and had to dig in by myself. After I discovered how to include the files that I needed, I decided to create a simple tutorial that has it all!
This tutorial will show you how to include your non-compilable items into a nuget package (nupkg). This steps apply to any type of file.
DISCLAIMER: You must have a nuspec file and a targets file. If you don’t have one yet, download one from somewhere and change the metadata accordingly.
Let’s start!
- Open up your solution in Visual Studio
- Open your solution’s nuspec file and look for <files> tag
- You need to reference all folders and all files that you want to include in your nuget. Example below:
<files> <file src="lib\net452\your_compiled_assembly.dll" target="lib\net452" /> <file src="Your_Folder\*.pp" target="content\Your_Folder"/> <file src="Your_Folder\Your_Sub_Folder\*.*" target="content" /> <file src="*.xdt" target="content" /> </files>
4. After you added all files into nuspec config, now you can open your targets file. Don’t forget to import targets into your csproj file like the example below
<Import Project="your_targets_file.targets" Condition="Exists('your_targets_file.targets')" />
5. Now to targets file, you must index ALL files that you want to add to your nuget. This step is very important
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <TargetName="CreatePackage"> <PropertyGroup> <Configuration>Release</Configuration> <PackageSource>bin$(Configuration)\Package</PackageSource> <NuSpecPath>$(MSBuildProjectName).nuspec</NuSpecPath> </PropertyGroup> <RemoveDirDirectories="$(PackageSource)"/> <MSBuildProjects="$(MSBuildProjectFullPath)" Targets="Rebuild"Properties=" TargetFrameworkVersion=v4.0; OutputPath=bin$(Configuration)\net40;Configuration=$(Configuration)" BuildInParallel="$(BuildInParallel)"/> <Copy SourceFiles="bin$(Configuration)\net40$(AssemblyName).dll" DestinationFolder="$(PackageSource)\lib\net40"/> <Copy SourceFiles="$(NuSpecPath)" DestinationFolder="$(PackageSource)"/> <Copy SourceFiles="Your_Folder\Your_Class_File.cs.pp" DestinationFolder="$(PackageSource)\Your_Folder"/> <Copy SourceFiles="Your_Folder\Yet_Another_Class.cs.pp" DestinationFolder="$(PackageSource)\Your_Folder"/> <Copy SourceFiles="Your_Folder\Your_Sub_Folder\A_Class.cs.pp" DestinationFolder="$(PackageSource)\Your_Folder\Your_Sub_Folder"/> <Copy SourceFiles="Your_Images_Folder\Your_Image.jpeg" DestinationFolder="$(PackageSource)\Your_Images_Folder"/> <Copy SourceFiles="Your_root_file.xml" DestinationFolder="$(PackageSource)"/> </Target> </Project>
6. After you added all your source files into your targets file, you are ready to pack it!!
File extensions for Transformations and generated classes:
- A pp file extension is a class that’s generated when a nuget is installed in any solution. Usually, the class namespace is replaced by “$rootnamespace$” (without quotes) and the extension is added (yourclass.cs.pp)
- A xdt file extension is a XML Transformation procedure.
Example of a *.pp class ready to be packed into a nuget:
namespace $rootnamespace$.App_Start { using System; using System.Collections.Generic; using System.Configuration; using System.Linq; ... }
That’s all folks!
Very nice
Hi,
I need the file path of the content file in the library. Can you explain it in simpler terms?
Hi Farshan!
You need to add the paths related to the solution file.
Imagine this:
dir\anotherDir\Projects\MySolution.sln
dir\anotherDir\Projects\Images\MyImage.jpeg
dir\Extra\MyFileToInclude.txt
You don’t need to use full paths. You have to move back and forward to get into the folder with the files you need.
If I didn’t answer to your question, I ask you to explain your problem in a more detailed way 🙂
Regards
can you please post your complete project solution
I have been asked to create a nuget package with certain exe and dll and other image files. Want all of these to be copied into certain folders on client app when this nuget pkg is installed. This tutorial unfortunately assumes a lot of dotnet knowledge and skips on a lot of important steps that are crucial to newbs like me. I mean, for one … there is no such thing as a nuspec file in my solution. Under what hierarchy do I include the tags? Where is this targets file? What are its contents? If you are writing a tutorial, never assume that your readers already know as much as you. At least provide pointers to other articles/resources that are credible to read as pre-requisites.
what are the next nuget command?
Hi Gordan,
What do you mean by the next nuget command?
Thanks
GB