如何从类库 NuGet 包中排除包? [英] How to exclude a package from a Class Library NuGet package?

查看:24
本文介绍了如何从类库 NuGet 包中排除包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将内置到 NuGet 包中的 .NET Standard 类库项目.我已经安装了 docfx.console 包,这样我每次构建时都可以生成一个文档网站.

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.

现在,当另一个项目安装我的库的 NuGet 包时,docfx.console NuGet 包也会被安装 - 这是我不想要的.

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

在Package下的项目属性中,我选择了Generate NuGet package on build.这将在我构建库时自动为我的库生成 NuGet 包.但在此选项卡中,我看不到可以配置排除任何包的任何地方.

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.

所以为了排除docfx.console包,我创建了一个.nuspec文件,内容如下:

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>

但它不起作用.在构建 NuGet 包时,如何更正它以排除 docfx.console 包?

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

或者,有没有其他方法可以从 NuGet 包中排除 docfx.console 包?

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

推荐答案

如何从类库 NuGet 包中排除包?

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

根据 NuGet 官方文档 控制依赖资产:

According to the NuGet official documentation Controlling dependency assets:

您可能将依赖项纯粹用作开发工具,并且可能不想将它暴露给会消耗你的项目包裹.在这种情况下,您可以使用 PrivateAssets 元数据来控制这种行为.

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>

因此,正如 UserName 所评论的,您可以将 <PrivateAssets>all</PrivateAssets> 添加到 docfx.consolePackageReference 中你的项目.

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

要完成此操作,请编辑您的项目文件 .csproj,如下所示:

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

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

或者

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

如果您使用的是 .nuspec 文件,您应该在 .nuspec 文件中移动以下依赖信息:

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>

这篇关于如何从类库 NuGet 包中排除包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆