DEV Community

Include both Nuget Package References *and* project reference DLL using “dotnet pack” 📦

Rich on September 05, 2019

Original: http://yer.ac/blog/2019/09/05/dotnet-pack-project-reference-and-nuget-dependency/ Recently I have been trying to generate more Nuget pac...
Collapse
 
aklaus profile image
Alex Klaus

Wanted to cross-reference a similar implementation on SO - stackoverflow.com/a/59893520/968003.

BTW, in order to bring XML-docs add BuildOnlySettings to "Target DependsOnTargets". It also will add PDBs to *.snupkg if you generate one.

So the final snippet looks like:

<PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target DependsOnTargets="BuildOnlySettings;ResolveReferences" Name="CopyProjectReferencesToPackage">
    <ItemGroup>
        <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
    </ItemGroup>
</Target>
Collapse
 
alexeyzimarev profile image
Alexey Zimarev

Thanks! Solved my issue today :)

I managed to get it working without those lines:

  <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
  <IncludeAssets>ProjectB.dll</IncludeAssets>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
maxkoshevoi profile image
Maksym Koshovyi

Could you please share your solution?

Collapse
 
ibrahimnamdar profile image
Ibrahim Namdar

I solved it by setting PrivateAssets="all"

<ProjectReference Include="..\ProjectB.csproj" PrivateAssets="all"/>
Otherwise ProjectB was seen as a package dependency not a project dependency.

Collapse
 
ashish4github profile image
ashish4github

This worked great for the project dll to be included but did not add the packages references for the packages referenced form ProjectB.dll

Collapse
 
aleksanderis profile image
Aleksandras

Thanks, it looks like promising solution!
However I found one not fully solved issue with it.. It copies all needed files correctly and it works in general, but it still adds a package reference to not existing package inside generated *.nuspec file.
Trying to restore "master" NuGet package from Visual Studio - it's trying to find the dependent (not existing) package, and obviously fails. However if I add the "master" package manually through *.csproj file - it's added and can be successfully restored. There is also no resolved code - so everything works. Except some better tooling support..

Collapse
 
aleksanderis profile image
Aleksandras

Nevermind.. seems like "ProjectB.dll" did the work, and now it works more correctly. I just didn't want to reference a dll in a "hardcoded" way initially..

Collapse
 
milovidov983 profile image
Bullock

Thanks!!!!!

Collapse
 
mmuecke profile image
mmuecke

Hi
thanks a lot for this solution. I like it to structure my nuget solutions in projects.
Is there a way to include the xml doc files form the referenced projects into the nuget.
This would make IntelliSense possible for the nuget.

Collapse
 
mmuecke profile image
mmuecke • Edited

I manged to include *.xml and *.pdb files in the output folder into the nuget with this changes:

  <PropertyGroup>
    ...
    <TargetsForTfmSpecificBuildOutput>
      $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage
    </TargetsForTfmSpecificBuildOutput>
    <AllowedOutputExtensionsInPackageBuildOutputFolder>
      $(AllowedOutputExtensionsInPackageBuildOutputFolder);.xml
      $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb
    </AllowedOutputExtensionsInPackageBuildOutputFolder>
  </PropertyGroup>
Enter fullscreen mode Exit fullscreen mode
  <Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->Replace('.dll', '.xml'))" />
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->Replace('.dll', '.pdb'))" />
    </ItemGroup>
  </Target>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bazen profile image
Bazen

useful writeup!

Collapse
 
korde profile image
Togne

Thanks! Saved my day. Cheers!😊

Collapse
 
mohammadimajid profile image
majid mohammadi

I just registered to dev.to to say thank you! You saved my day! :)