Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017 Nuget pack exclude Project reference as another nuget reference, add assembly instead

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:

  • Chose a different package name
  • include the dll of the project instead while creating the package
like image 766
Satyajit Avatar asked Sep 23 '17 05:09

Satyajit


Video Answer


1 Answers

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.

like image 192
Martin Ullrich Avatar answered Dec 08 '22 23:12

Martin Ullrich