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

蓝咒 提交于 2020-04-10 13:53:21

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/46376256/visual-studio-2017-nuget-pack-exclude-project-reference-as-another-nuget-referen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!