Force project references to be included in netstandard nuget package

You can control where assemblies are deployed in the nuget package using an item in an itemgroup, similar to this:

<ItemGroup>
    <None Include="!!path_to_assembly!!">
        <PackagePath>lib\net462</PackagePath>
        <Pack>true</Pack>
        <Visible>false</Visible>
    </None>
</ItemGroup>

That should include the specified assembly in the package.


You can add the following target to your .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net47</TargetFrameworks>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" PrivateAssets="all" />
    <ProjectReference Include="..\ClassLibrary3\ClassLibrary3.csproj" Condition="'$(TargetFramework)' == 'net47'" PrivateAssets="all" />
  </ItemGroup>

  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
    </ItemGroup>
  </Target>
</Project>

Source 1

Source 2

Reference: Advanced extension points to create customized package