Recently I have been trying to generate more Nuget packages for our dotnet core projects, utilizing the
dotnet pack command. One issue I have been encountering is that the command was either referencing the required nuget packages, or the project reference DLLs, never both.
If you have Project A which has a project reference to Project B as well as including a nuget package called Package A you would expect the generated package to contain a link to both the required nuget package, and the DLL(s) for Project B, yes? This however is not how the dotnet pack command works.
This issue is widely reported on their repo (I.e. https://github.com/NuGet/Home/issues/3891 ) and unfortunately it seems the developers and the community are in a bit of a disagreement to what is “correct”. The official stance (as I understood it) is that the project references won’t be included as they should be their own packages. This however is not always practical or desired.
Plenty of workarounds have been suggested around Stack Overflow and Github including having a seperate nuspec file, using Powershell to inject things into the generated nupkg and so on…
The solution below worked for me, but of course, YMMV.
In the end I ditched having my own
.nuspec file within my project (as per some SO posts) and instead used the CSPROJ (as recommended). Below you can see the required fields for the packaging (version, naming, etc), a reference to a nuget package, and a reference to another project within the solution.
If you run dotnet pack now, it will generate an appropriately named package which will contain a nuget dependancy on
SomeNugetPackage. This can be confirmed by opening the nupkg with an archive tool (7Zip,WinRar, WinZip…) and seeing that the only DLL in the
lib folder will be the DLL of the project being packed.
The fix is as follows:
- Alter the project reference to set the
ReferenceOutputAssemblyflag to true, and
IncludeAssetsto the DLL name
<ProjectReference Include="..\ProjectB.csproj"> <ReferenceOutputAssembly>true</ReferenceOutputAssembly> <IncludeAssets>ProjectB.dll</IncludeAssets> </ProjectReference>
- Add the following line into the
- Add new target between
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage"> <ItemGroup> <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))"/> </ItemGroup> </Target>
So now you end up with something that looks like this
Now if you run dotnet pack you should see any project reference DLL under the
lib folder of the package, and if you inspect the nuspec file inside the package (or upload it to your package repo) you should see the nuget dependencies.
Hopefully this helps someone, as there is a lot of conflicting info around. Please let me know if this would cause any issues!