2

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?

cedd
  • 1,741
  • 1
  • 21
  • 34

1 Answers1

4

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.

Rob Mensching
  • 33,834
  • 5
  • 90
  • 130
zivkan
  • 12,793
  • 2
  • 34
  • 51
  • Thanks @zivkan, this worked perfectly. One thing to note is that this also brings all the files in to the project in Visual Studio, which in my case is quite annoying (there are a lot of files). I was never able to persuade VS to hide these, although I did try Visible="false" and False<\Visible> as in https://stackoverflow.com/questions/56569355/none-items-in-subdirectories-with-visible-false-in-nuget-package-shows-subdire and https://stackoverflow.com/questions/45613251/hide-files-in-csproj-without-excluding-them-from-build – cedd Sep 24 '20 at 16:00