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

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

问题描述

我有一个 .NET Standard 类库项目,该项目将内置到NuGet包中.我已经安装了 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.

在程序包"下的项目属性中,我选择了"在生成时生成NuGet程序包" .生成库时,它将自动为我的库生成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所评论的那样,您可以在 docfx.console PackageReference 中添加< PrivateAssets> all</PrivateAssets> 您的项目.

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天全站免登陆