Search code examples
c#.netnuget.net-standardnuspec

Specifying files to add to a nuget package in .csproj file


I am creating a nuget package from some code, but also need to deploy some tools with the package.

In a .nuspec file, I can do this with the <files> element, and this all works well.

However when using a .nuspec file, the packageReferences from the csproj file aren't included, and I am seeing some problems when including them manually (with the <dependencies> element).

The package created also always seems to restore as a .net framework package, even though it is targetting .net, as in this question.

I am hoping that all these problems would go away if I moved to using the .csproj format for specifying the nuget package details, but having read the docs I can't find out how to do it.

Does anyone know how it is done?

If not, can anyone shed any light on created a .net framework / .net core nuget package from a .nuspec file, that restores to the correct target version and respects package dependencies?


Solution

  • It's not easy to find/discover, but NuGet's MSBuild tasks docs page has a section called "including content in a package", which tells you about the PackagePath metadata on MSBuild items, that NuGet uses to copy files into the package.

    So, in your csproj, you could have something like this:

    <ItemGroup>
      <None Include="..\MyTool\Tool.exe" PackagePath="tools" Pack="true" />
    </ItemGroup>
    

    and then your package will contain tools\Tool.exe. The Pack="true" attribute is required for None elements.

    You can use MSBuild's globbing to copy entire directories, if that's easier. Include="..\MyTool\*". My MSBuild skills are not so advanced, so I don't know how to glob ..\MyTool\**\*, which means all files in all subdirectories, while maintaining the correct directory layout in the PackagePath="???" metadata. So the best I can suggest is one glob per directory.