When we create a nuget package in visual studio 2017, it adds the project references as another nuget reference by default. How can we disable that while creating package and instead:
The PackageReference
can be marked with PrivateAssets="All"
to ensure that it doesn't end up as a NuGet dependency of the consuming library when packed. Then the consuming library can use a custom target to add files to the built nuget package. A full example for the csproj
file could look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeP2PAssets</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\testprivatelib\testprivatelib.csproj" PrivateAssets="All" />
</ItemGroup>
<Target Name="IncludeP2PAssets">
<ItemGroup>
<BuildOutputInPackage Include="$(OutputPath)\testprivatelib.dll" />
</ItemGroup>
</Target>
</Project>
Where testprivatelib.csproj
is a project that builds a DLL that you want to additionally include into the .nupkg
file and not publish an extra NuGet package for that referenced project.
Specifying different NuGet packages is more difficult. It requires manually creating a .nuspec
file using it to pack a NuGet package. You can see an example how this can be done at https://github.com/dasMulli/nuget-include-p2p-example/tree/master/LibA - the LibA.csproj
is set up to use LibA.nuspec
when dotnet pack
is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With