问题
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