NuGet包适用于Linux和Windows。

huangapple go评论66阅读模式
英文:

NuGet package for linux and windows alike

问题

I'm using the nuget package net.ibm.data.db2 (->windows), which is also available as net.ibm.data.db2-lnx (->linux).
现在,当我在VS 2022中构建我的.NET 6项目MyProj.csproj时,使用net.ibm.data.db2 (或-lnx),根据构建配置,我可以简单地选择要使用哪一个:

<Choose>
  <When Condition="'$(Configuration)'=='Debug-Linux' OR '$(Configuration)'=='Release-Linux'">
    <ItemGroup>
      <PackageReference Include="net.ibm.data.db2-lnx" Version="6.0.0.200">
        <PrivateAssets>none</PrivateAssets>
      </PackageReference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <PackageReference Include="net.ibm.data.db2" Version="6.0.0.200">
        <PrivateAssets>none</PrivateAssets>
      </PackageReference>
    </ItemGroup>
  </Otherwise>
</Choose>

但是,当我打包我的项目时,只有一个依赖项与打包时选择的构建配置相关。要么是Windows包,要么是Linux包。

-> 如果我想在其他地方引用这个包,我必须创建一个名为MyProj和MyProj-lnx的包,以便其他人可以在Windows和Linux上使用我的包。

如果他们想创建一个包,他们也必须提供一个Windows包和一个Linux包,依此类推。

我该如何创建一个包含两个版本的包,并根据构建配置或其他配置选择正确的版本?我希望为我的客户提供一个myproj.nupkg,其中包含用于构建和发布的Windows和Linux程序集,这样他们不必区分或选择任何内容。

可以假定所有客户都有Debug-LinuxRelease-Linux配置。

英文:

I'm using the nuget package net.ibm.data.db2 (->windows), which is also available as net.ibm.data.db2-lnx (->linux).
Now when building my own .Net 6 project MyProj.csproj in VS 2022 using net.ibm.data.db2(or -lnx), I can simply choose which one to use depending on build configuration:

<Choose>
  <When Condition="'$(Configuration)'=='Debug-Linux' OR '$(Configuration)'=='Release-Linux'">
    <ItemGroup>
      <PackageReference Include="net.ibm.data.db2-lnx" Version="6.0.0.200">
        <PrivateAssets>none</PrivateAssets>
      </PackageReference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <PackageReference Include="net.ibm.data.db2" Version="6.0.0.200">
        <PrivateAssets>none</PrivateAssets>
      </PackageReference>
    </ItemGroup>
  </Otherwise>
</Choose>

But when I pack my project it has only one dependency to the chosen build configuration upon packing. Either the windows package or the linux package.

-> If I want to reference this package somewhere else I have to create a package MyProj and MyProj-lnx to let others use my package for windows and linux.

And if they want to create a package, they have to provide a windows and a linux package as well. And so on.

How can I make one package that contains both versions and chooses the right one, depending on build configuration or sth else I can configure? I want to provide one myproj.nupkg for my clients with windows and linux assemblies for build and publish so they don't have to differ or choose anything.

It's ok to assume that all clients got the Debug-Linux and Release-Linux configurations

答案1

得分: 0

根据我的研究,net.ibm.data.db2和net.ibm.data.db2-lnx中的API应该是相同的。dll文件的名称也相同。它们之间的区别在于它们进行了针对Windows操作系统和Linux操作系统的优化。

我注意到它们的dll文件名称相同,API似乎也相同。

因此,我有一个关于您的需求的想法。您可以基于dll文件而不是包管理器。

首先,您可以在项目根目录中运行以下nuget命令:

nuget install Net.IBM.Data.Db2
nuget install Net.IBM.Data.Db2-lnx

然后,根据以下方式向项目引用添加所需的内容:

<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
    <Reference Include="windows_package_path\IBM.Data.Db2.dll" />
</ItemGroup>

<ItemGroup Condition="'$(OS)' == 'Linux'">
    <Reference Include="linux_package_path\IBM.Data.Db2.dll" />
</ItemGroup>

属性"OS"将获取当前的系统类型,Windows操作系统将获取"Windows_NT",Linux操作系统将获取"Linux"。

英文:

<!--Based on my research, the APIs in net.ibm.data.db2 and net.ibm.data.db2-lnx should be same. The dll file name also same. The differences between them is they make optimize for windows OS and linux OS.
-->

I notice the dll file name of them are same and the APIs seems also same.

So I have an idea for your requirements. You can based on dll file instead of package manager.

First you can run the nuget command in your project root directory:

nuget install Net.IBM.Data.Db2
nuget install Net.IBM.Data.Db2-lnx

Then add the content you need to the project reference like this:

  &lt;ItemGroup Condition=&quot;&#39;$(OS)&#39; == &#39;Windows_NT&#39;&quot;&gt;
	&lt;Reference Include=&quot;windows_package_path\IBM.Data.Db2.dll&quot; /&gt;
  &lt;/ItemGroup&gt;
  &lt;ItemGroup Condition=&quot;&#39;$(OS)&#39; == &#39;Linux&#39;&quot;&gt;
	&lt;Reference Include=&quot;linux_package_path\IBM.Data.Db2.dll&quot; /&gt;
  &lt;/ItemGroup&gt;

Property "OS" will get the current System Type, Windows OS will get "Windows_NT", Linux OS will get "Linux".

答案2

得分: -1

我通过使用一个包含了dll文件并将正确的文件复制到输出目录的nuget包解决了我的问题。我不需要直接引用它,它只在运行时使用。

nugetpkg:
lib -> 没有文件
contentfiles -> 两个dll文件
buildTransitive -> my_copydll.targets

my_copydll.targets包含一个取决于所使用的构建配置("Release"或"Release-Linux")或正在构建的操作系统的条件。根据这个条件,我要么将dll1复制到输出目录,要么将dll2复制到输出目录。

英文:

I resolved my problem by using a nuget package that contains both dlls and copies the right one to the output directory. I do not need to directy reference it, it's only used during runtime.

nugetpkg:
lib -&gt; no file
contentfiles -&gt; both dll files
buildTransitive -&gt; my_copydll.targets

my_copydll.targets contains a condition depending on the used build config ("Release" or "Release-Linux") or the operating system this is being build on. Depending on that, I either copy dll1 or dll2 to the output directory.

huangapple
  • 本文由 发表于 2023年2月27日 16:01:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定