vdWes
vdWes

Reputation: 127

Removing a NuGet package's dependent NuGet packages

Background:

I am creating NuGet packages of projects for my company, these NuGet packages will then be installed into other internal projects. Currently, I am doing it with the built-in functionality of .NET Standard/Core projects. This works as desired, however, all our projects have a "wrapper NuGet package" installed for some code analysis (StyleCop NuGet package + company-specific ruleset files and Json files).

If I want to install a NuGet package (that I created) into another project, it always adds the dependency on our style analyzer NuGet package.

This is what it looks like installed in a project:

Package Tree

Question:

Is there a way that I can remove the dependency on a specific NuGet package upon NuGet package creation? i.e Is there a way that I can do something in the line of (.csproj file):

<ItemGroup>
    <PackageReference Condition="'$(Configuration)'=='Debug'" Include="CompanyStyleAnalyzer" Version="1.1.6" />
</ItemGroup>

but instead of specifying the actual version I ALWAYS want to remove the dependency no matter what the version is when I build the project in the "Release" configuration, or even better, every time the Dotnet pack is run, something specifying to not include the dependency in the final package.

Is this a "frowned-upon" way of removing dependencies in the project?

The previous way I generated NuGet packages (in .Net Framework), was to use the NuGet CLI - pack command, and then extract the file and edit the .Nuspec file to remove the dependency manually. I do not want to have to edit the Nuspec file and repack the package.

Can anyone specify what I can do in this regards, please?

Upvotes: 1

Views: 1649

Answers (1)

Jeremy Caney
Jeremy Caney

Reputation: 7594

As per @Ian-Kemp's comment, this can be achieved by using the PrivateAssets element under your PackageReference:

<ItemGroup>
  <PackageReference Include="CompanyStyleAnalyzer" Version="1.1.6" />
    <PrivateAssets>all</PrivateAssets>
  </PackageReference>
</ItemGroup>

Upvotes: 2

Related Questions