0
votes

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?

1
Your question is a little unclear, but I think you might be looking for PrivateAssets=all? docs.microsoft.com/en-us/nuget/consume-packages/…Ian Kemp
You are right, that is exactly what I was looking for. I did not like the "'$(Configuration)'=='Debug'" approach I mentioned, the PrivateAssets approach seems more fit-for-purpose. You can also post it as an answer, as the documentation says all that is needed. Thank you.Stephan V.d Westhuizen
FYI: Since @IanKemp hadn't added this as an answer, I've done so on their behalf (and marked it as a community wiki, since it's not my contribution). That will allow that answer to be accepted, so this question doesn't show up as unanswered.Jeremy Caney

1 Answers

1
votes

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>