在构建时生成 Nuget 包不包括所有依赖项 [英] Generating Nuget package on build does not include all dependencies

查看:38
本文介绍了在构建时生成 Nuget 包不包括所有依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案树看起来像:

The tree of my solution looks like :

项目 A

  • 参考 Nuget 包一些包"

项目 B

  • 参考项目 A

在构建的时候,项目B产生了一个包,我们称之为PackageB

When building, project B produces a package, let's call it PackageB

在 ProjectB.csproj 中,我使用了以下内容:

In ProjectB.csproj I have used the following:

   <ProjectReference Include="ProjectA.csproj">
        <PrivateAssets>All</PrivateAssets>
    </ProjectReference>

含义PackageB,除了ProjecdtB.dll,还包括ProjectA.dll

但是它不包含Some Package",所以当我启动一个引用 PackageB 的客户端时,我收到一个运行时错误,抱怨 dll 包含在Some Package"中.不见了.

However it does not include "Some Package", so when I launch a client that references PackageB, I get a runtime error complaining that the dll contains in "Some Package" is missing.

我如何确保某些包裹"作为 PackageB 的依赖添加.我想在 csproj 中执行此操作,而不依赖于 nuspec 文件.这可能吗?

How can I make sure "Some Package" is added as a depencency of PackageB. I'd like to do this in csproj, without relying on a nuspec file. Is this possible ?

为了将 ProjectA 包含在 PackageB 中,我还需要提及我正在使用 Teronis.MSBuild.Packaging.ProjectBuildInPackage.

In order to get ProjectA included in the PackageB, I also need to mention that I'm using the Teronis.MSBuild.Packaging.ProjectBuildInPackage.

推荐答案

感谢使用我的包.

让我们首先假设以下内容:

Let's first assume the following:

<!-- Project A -->
<ItemGroup>
  <PackageReference Include="SomePackage" Version="*" />
</ItemGroup>

<!-- Project B -->
<ItemGroup>
  <ProjectReference Include="ProjectA" PrivateAssets="all">
  <!--<PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="0.1.7" />-->
</ItemGroup>

您告诉 NuGet,您不希望项目 A 作为 NuGet 依赖项被选中.这是隐含的,您无法控制.缺点是项目 A 的程序集,而不是项目 A 的程序包的程序集,不存在于项目 B 的程序包中.

You are telling NuGet that you don't want to have Project A to be picked up as NuGet-dependency. This is implicit, you don't have control about that. The down-side is the assmeblies of Project A, but not the assemblies of the packages of Project A, are not present in package of Project B.

通过删除 PrivateAssets=all",您将禁用 NuGet 的隐式行为,项目 A 将被选择为 NuGet 依赖项和每个非依赖项包(也称为传递包).

By removing PrivateAssets="all" you disable the implicit behaviour of NuGet and the Project A will be picked up as NuGet dependency and EACH non-dependency package (also called transitive package).

现在让我们假设:

<!-- Project A -->
<ItemGroup>
  <PackageReference Include="SomePackage" Version="*" />
</ItemGroup>

<!-- Project B -->
<ItemGroup>
  <ProjectReference Include="ProjectA" PrivateAssets="all">
  <PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="0.1.7" />
</ItemGroup>

通过安装我的包,我协助了 NuGet 的隐式行为:通过不选择项目 A 作为 NuGet 依赖项,将项目 A 生成的直接程序集复制到项目 B 的 bin 文件夹中.这有以下缺点:

By having installed my package I assist in the implicit behaviour of NuGet: By not picking up Project A as NuGet-dependency copy over the direct assemblies produced by Project A to the bin-folder of Project B. This has the following drawback:

由于隐式行为和我的包的使用,您在项目 B 中有项目 A 的程序集,这些程序集需要您在项目 A 中引用的包(在您的示例中为某些包")提供的程序集.所以一种解决方法是将项目 A 中的包显式添加到项目 B 中,如下所示:

Because of the implicit behaviour and the usage of my package you have assemblies of Porject A in Project B that are in need of the assemblies provided by packages (in your example "Some Package") you referenced in Project A. So a workaround is to add the packages from Project A in Project B explicitly as shown here:

<!-- Project A -->
<ItemGroup>
  <PackageReference Include="SomePackage" Version="*" />
</ItemGroup>

<!-- Project B -->
<ItemGroup>
  <ProjectReference Include="ProjectA" PrivateAssets="all">
  <PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="0.1.7" />
  <!-- Use the SAME version like in Project A. -->
  <PackageReference Include="SomePackage" Version="*" />
</ItemGroup>

这应该可以解决您的问题.当它不起作用时,请提供反馈.

This should solve your problem. Please provide feedback when it does NOT work.

这篇关于在构建时生成 Nuget 包不包括所有依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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