简体   繁体   English

针对特定依赖项 NuGet

[英]Target specific dependencies NuGet

I have the following dependencies in my .nuspec file:我的 .nuspec 文件中有以下依赖项:

<dependencies>
        <group>
          <dependency id="Dep1" version="2.4.11" />
          <dependency id="Dep2" version="1.0.4" />
          <dependency id="Dep3" version="1.0.4" />
          <dependency id="Dep4" version="1.0.0" />
          <dependency id="Dep5" version="1.0.4" />
          <dependency id="Dep6" version="1.0.4" />
        </group>
    </dependencies>

I've created a local nuget server and I'm installing this NuGet package in a Xamarin Forms solution.我已经创建了一个本地 nuget 服务器,我正在 Xamarin Forms 解决方案中安装这个 NuGet 包。

The thing is that in the Android part of the solution it will install, but in the .netstandard part it will complain that Dep 4, 5, 6 are only for MonoAndroid (which is correct).问题是在解决方案的 Android 部分它将安装,但在 .netstandard 部分它会抱怨 Dep 4、5、6 仅适用于 MonoAndroid(这是正确的)。 I don't need the dep 4,5 and 6 in my .netstandard solution.我的 .netstandard 解决方案中不需要 dep 4,5 和 6。

NU1202: Dep4 1.0.0 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). NU1202:Dep4 1.0.0 与 netstandard2.0 (.NETStandard,Version=v2.0) 不兼容。 Package Dep4 1.0.0 supports: monoandroid10 (MonoAndroid,Version=v1.0) NU1202: Package Dep5 1.0.4 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package Dep4 1.0.0 支持:monoandroid10 (MonoAndroid,Version=v1.0) NU1202: Package Dep5 1.0.4 与 netstandard2.0 (.NETStandard,Version=v2.0) 不兼容。 Package Dep5 1.0.4 supports: monoandroid44 (MonoAndroid,Version=v4.4)封装 Dep5 1.0.4 支持:monoandroid44 (MonoAndroid,Version=v4.4)

How can I separate dependencies per targetFramework?如何分离每个 targetFramework 的依赖项? I've tried multiple variations of groups within the dependencies category, but nothing will solve this problem.我在依赖项类别中尝试了多种组变体,但没有任何方法可以解决此问题。

After thoroughly reading仔细阅读后

https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element

https://docs.microsoft.com/en-us/nuget/reference/target-frameworks https://docs.microsoft.com/en-us/nuget/reference/target-frameworks

https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/nuget-manual https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/nuget-manual

it can be summed up to:可以总结为:

Version 2.0+版本 2.0+

As an alternative to a single flat list, dependencies can be specified according to the framework profile of the target project using elements within .作为单个平面列表的替代方案,可以根据目标项目的框架配置文件使用 .

Each group has an attribute named targetFramework and contains zero or more elements.每个组都有一个名为 targetFramework 的属性,并包含零个或多个元素。 Those dependencies are installed together when the target framework is compatible with the project's framework profile.当目标框架与项目的框架配置文件兼容时,这些依赖项会一起安装。

The element without a targetFramework attribute is used as the default or fallback list of dependencies.没有 targetFramework 属性的元素用作依赖项的默认或后备列表。 See Target frameworks for the exact framework identifiers.有关确切的框架标识符,请参阅目标框架。

Therefore in my case it would be:因此,就我而言,它将是:

    <dependencies>
       <group targetFramework="MonoAndroid10">
          <dependency id="Dep1" version="2.4.11" />
          <dependency id="Dep2" version="1.0.4" />
          <dependency id="Dep3" version="1.0.4" />
          <dependency id="Dep4" version="1.0.0" />
          <dependency id="Dep5" version="1.0.4" />
          <dependency id="Dep6" version="1.0.4" />
       </group>
       <group>
          <dependency id="Dep1" version="2.4.11" />
          <dependency id="Dep2" version="1.0.4" />
          <dependency id="Dep3" version="1.0.4" />
        </group>
    </dependencies>

This way when I'm installing the package on the .netstandard project it will check the targetFramework and see that it's not MonoAndroid and it will fallback to the element without a targetFramework and use only Dep1, Dep2 and Dep3.这样,当我在 .netstandard 项目上安装包时,它会检查 targetFramework 并看到它不是 MonoAndroid,它将回退到没有 targetFramework 的元素,并且只使用 Dep1、Dep2 和 Dep3。 For the MonoAndroid10 project it will do the same and use all of them.对于 MonoAndroid10 项目,它将执行相同的操作并使用所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM