6

I have a solution where I want to create a NuGet package from two of the projects.

If it were just one project, I'd use the .csproj file to spec the NuGet packager, but since I want two different projects to go into the finished package, I can't do that.

I created a NuSpec file that referenced the DLLs from the two projects, and then packed it with the -IncludeReferencedProjects switch in the command line. This included the referenced projects from the same solution, but not any NuGet packages that were dependencies in the projects.

I tried referencing the new NuGet package in another solution, and the solution built and run, but encountered a FileNotFoundException at runtime when it was looking for the DLL from the NuGet packages that were dependencies in the NuGet package I just created.

I realize that I could include the NuGet dependencies in the NuSpec file, but that will become outdated the moment one of my projects adds another NuGet reference.

Is it possible to set up a NuSpec file so that it uses the .csproj files from both my projects and sets up the NuGet dependencies correctly, even when the projects are updated with new NuGet dependencies?

Petter Brodin
  • 2,069
  • 4
  • 21
  • 27
  • Can you please describe the relations between your projects? Is one project references the other? Also, are there only two projects in the solution, or are there more and you need them to be packed as well? – Peas Jan 27 '15 at 20:27
  • One references the other. There are other projects in the solution, but they're not needed as part of the package. If, however, they're referenced by the projects included in the package, that's solved by using the -IncludeReferencedProjects parameter when packing the package. The problem is not the intra-solution references, but making sure that all required NuGet packages are correctly referenced. – Petter Brodin Jan 28 '15 at 06:12
  • Possible duplicate of [Creating one NuGet package from multiple projects in one solution](https://stackoverflow.com/questions/15882770/creating-one-nuget-package-from-multiple-projects-in-one-solution) – Michael Freidgeim Sep 24 '17 at 09:44

1 Answers1

6

Well, as far as I understand you have two projects, foo.csproj and bar.csproj. foo references bar.

You should use nuget pack foo.csproj -IncludeReferencedProjects (note that the pack command is applied on foo) as IncludeReferencedProjects option also adds the dependencies of the referenced projects.
This results in both foo.dll and bar.dll appearing in the lib folder of foo package. Also, both foo and bar dependencies will appear under the dependencies element.

Peas
  • 313
  • 1
  • 9
  • I have no idea why the dependencies weren't included when I tested it earlier. I guess I must have done something wrong, like packing from bar instead of foo. That, combined with the trick of combining a .csproj file with a .nuspec file (as detailed here http://stackoverflow.com/questions/14797525/differences-between-nuget-packing-a-csproj-vs-nuspec) made it possible to do exactly what I wanted. – Petter Brodin Feb 02 '15 at 12:12
  • @Peas I have the same issue but It does not produce the expected .nuspec file with dependencies listed. Here is my question on stack overflow with all the details and a sample repo to reproduce the issue: https://stackoverflow.com/questions/72984765/nuget-pack-multiple-projects-into-single-nuget-package-with-all-external-depende – VJSharp Jul 14 '22 at 18:00