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!

  1. Open up your solution in Visual Studio
  2. Open your solution’s nuspec file and look for <files> tag
  3. 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:

  1. 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)
  2. 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!

3 COMMENTS

  1. 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here