When you’re packing your NuGet packages with
1 |
dotnet pack Whatever |
and the Whatever project happens to reference another project, Whatever.Dependency.csproj (with the <ProjectReference ...> tag in Whatever.csproj), the default behavior is to treat the reference as if the referenced project was ALSO a NuGet package, in which case you would need to
1 |
dotnet pack Whatever.Dependency |
too.
This might be what you want β and if so, then there’s no problem, and you can stop reading now. π
But sometimes, it’s NOT what you want! Sometimes you want to reference the project and have its resulting DLL copied and INCLUDED in your NuGet package β if that’s the case, check this out π
In the old days, you would carefully craft your nuspec to include the DLL from the referenced project in the /lib folder of the package. We could also do that today, but it would be much easier to leverage the brilliant work done by Teroneko in the Teronis.MSBuild.Packaging.ProjectBuildInPackage NuGet package.
Simply install the package into the project you are about to pack:
1 |
install-package Teronis.MSBuild.Packaging.ProjectBuildInPackage -projectname Whatever |
and then edit the csproj file (Whatever.csproj in this case) and add the PrivateAssets="all" attribute to the relevant tag:
1 |
<ProjectReference Include="..\Whatever.Dependency\Whatever.Dependency.csproj" PrivateAssets="all" /> |
and then you can
1 |
dotnet pack Whatever |
and the NuGet will contain both Whatever.dll and Whatever.Dependency.dll.
Neat! π