Xpleria
Xpleria

Reputation: 5853

How to exclude a package from a Class Library NuGet package?

I have a .NET Standard class library project that will be built into a NuGet package. I've installed the docfx.console package so that I can generate a documentation website every time I build.

Now when another project installs my library's NuGet package, the docfx.console NuGet package also gets installed - which I don't want.

In the project properties under Package, I have selected Generate NuGet package on build. This would automatically generate the NuGet package for my library when I build it. But in this tab, I don't see anywhere where I can configure to exclude any packages.

So in order to exclude the docfx.console package, I created a .nuspec file with the following contents:

<?xml version="1.0" encoding="utf-8" ?>

<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <!-- Required elements-->
    <id>My.Library</id>
    <version>1.0.1</version>
    <description>My Library's project dscription.</description>
    <authors>Me</authors>
    <copyright>My Copyright (c)</copyright>

    <!-- Optional elements -->
    <dependencies>
      <dependency id="docfx.console" version="2.36.1" exclude="build" />
    </dependencies>
    <!-- ... -->
  </metadata>
  <!-- Optional 'files' node -->
</package>

But it isn't working. How can I correct it to exclude the docfx.console package when building the NuGet package?

Or, is there any other way I could exclude the docfx.console package from the NuGet package?

Upvotes: 5

Views: 6617

Answers (1)

Leo Liu
Leo Liu

Reputation: 76710

How to exclude a package from a Class Library NuGet package?

According to the NuGet official documentation Controlling dependency assets:

You might be using a dependency purely as a development harness and might not want to expose that to projects that will consume your package. In this scenario, you can use the PrivateAssets metadata to control this behavior.

<ItemGroup>
    <!-- ... -->

    <PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0">
        <PrivateAssets>all</PrivateAssets>
    </PackageReference>

    <!-- ... -->
</ItemGroup>

So, just as UserName commented, you can add <PrivateAssets>all</PrivateAssets> to PackageReference of docfx.console in your project.

To accomplish this, edit your project file .csproj like following:

  <ItemGroup>
    <PackageReference Include="docfx.console" Version="2.36.2" PrivateAssets="All" />
  </ItemGroup>

Or

  <ItemGroup>
    <PackageReference Include="docfx.console" Version="2.36.2">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

If you are using .nuspec file, you should move the following dependency info in your .nuspec file:

<dependencies>
  <dependency id="docfx.console" version="2.36.1" />
</dependencies>

Upvotes: 7

Related Questions