基于操作系统的不同NuGet软件包

Mut*_*hop 5 c# db2 nuget .net-core

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

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

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

如何在.csproj文件中指定要基于操作系统使用不同的软件包?

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

小智 17

使用IsOsPlatform(platform)MSBuild 属性函数:

<PackageReference Include="NetVips.Native.linux-x64" Version="8.9.1" Condition="$([MSBuild]::IsOsPlatform('Linux'))" />
<PackageReference Include="NetVips.Native.osx-x64" Version="8.9.1" Condition="$([MSBuild]::IsOsPlatform('OSX'))" />
<PackageReference Include="NetVips.Native.win-x64" Version="8.9.1" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
Run Code Online (Sandbox Code Playgroud)


Mut*_*hop 5

我最终使用了Configuration范式Choose/When

一个简单的例子.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>
Run Code Online (Sandbox Code Playgroud)

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

我发现这个网站对于帮助在 Visual Studio 2017 中进行设置很有用。