如何在NuGet中基于目标框架指定条件依赖项?

时间:2012-12-03 17:27:00

标签: nuget nuspec

我正在构建一个引用Microsoft CommonServiceLocator程序集的NuGet包。

Microsoft CommonServiceLocator有两个版本:

我的项目是一个可移植类库,但是因为它有时与Enterprise Library一起使用,所以我需要“有条件地”引用可移植版本,这样就没有冲突了。

  • 如果目标框架是完整的.NET 4.0 / 4.5,请使用原始的CommonServiceLocator包,以便人们也可以使用Enterprise Library位(也引用CommonServiceLocator包)。
  • 如果目标框架是可移植的(或其他任何东西),请使用Portable.CommonServiceLocator包。

I see the new "group" feature in the NuGet docs showing how to specify dependencies in your .nuspec file我想我会做我想做的事,但我不确定如何测试它。

以下是我想的我需要做的事情,我希望有人可以验证我的方法或指出我正确的方向:

<dependencies>
  <group>
    <!-- Always include regardless of target framework -->
    <dependency id="Autofac" />
  </group>
  <group targetFramework="net40">
    <!-- Also include the full CSL if it's full framework -->
    <dependency id="CommonServiceLocator" />
  </group>
  <group targetFramework="portable-win+sl50+wp8">
    <!-- Otherwise include the Portable CSL -->
    <dependency id="Portable.CommonServiceLocator" />
  </group>
</dependencies>

...具体

  • 我的targetFramework语法是否正确?我找不到任何示例,因此我不知道+分隔机制是否正确或是否应该是逗号分隔。
  • 默认组是否有效?具有未指定目标框架的组 - 是否会始终包含该组或是否需要在每个组中复制/粘贴它?

1 个答案:

答案 0 :(得分:6)

是的,这非常正确。有关可移植框架名称的详细信息,请访问http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Framework_Names

我发现还有一件事,因为win + sl50 + wp8默认包含net45,你可能想要包含它以便安装这个依赖组。