基于操作系统的不同 NuGet 包 [英] Different NuGet package based on operating system

查看:28
本文介绍了基于操作系统的不同 NuGet 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 dotnet 2.1 中有一个测试项目,它需要跨多个平台(特别是 Windows 和基于 linux 的系统)以及访问 DB2 数据库.

I have a test project in dotnet 2.1 that needs to work across multiple platforms (specifically, windows and linux-based systems) as well as access DB2 databases.

IBM 为不同的操作系统提供了单独的 NuGet 包:

IBM provides separate NuGet packages for different operating systems:

  1. IBM.Data.DB2.Core
  2. IBM.Data.DB2.Core-lnx
  3. IBM.Data.DB2.Core-osx

如何在我的 .csproj 文件中指定我想根据操作系统使用不同的包?

How can I specify in my .csproj file that I want to use different packages based on the operating system?

传入 RuntimeIdentifier (dotnet publish ... -r linux-x64) 是可能的,但我不确定如何在 中利用该信息csproj.我也不反对使用 Choose/When 构造,但不知道如何推断哪个系统正在尝试构建项目.

Passing in the RuntimeIdentifier (dotnet publish ... -r linux-x64) is possible, but I am unsure how to leverage that information inside the csproj. I am also not opposed to using the Choose/When construct, but don't know how to infer what system is trying to build the project.

推荐答案

我最终使用了 ConfigurationChoose/When 范例.

I ended up using Configuration and the Choose/When paradigm.

一个简单的例子 .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <Configurations>Debug;Release;Docker</Configurations>
    <Platforms>AnyCPU;x64</Platforms>
  </PropertyGroup>

  ... the rest of your .csproj and dependencies ...

  <Choose>
    <When Condition=" '$(Configuration)'=='Docker' ">
      <ItemGroup>
        <PackageReference Include="IBM.Data.DB2.Core-lnx" Version="1.2.2.100" />
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <PackageReference Include="IBM.Data.DB2.Core" Version="1.2.2.100" />
      </ItemGroup>
    </Otherwise>
  </Choose>

</Project>

在命令行上我会运行:dotnet build/your/project.csproj -c .

On the command line I would run: dotnet build /your/project.csproj -c <yourConfigurationName>.

我发现这个网站有助于在视觉上进行设置工作室 2017.

I found this site useful for helping set this up in visual studio 2017.

这篇关于基于操作系统的不同 NuGet 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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