【问题标题】:How can I include different packages in different Configuration/Platform combinations?如何在不同的配置/平台组合中包含不同的包?
【发布时间】:2021-10-19 12:52:19
【问题描述】:

我有一个旨在与 Sap Business One 的 SDK 配合使用的库。 V10 的 SDK 与 V9.3 的不同,我也有 x86/x64 和 SQL/HANA 版本,这给了我 8 个排列,因此有 8 个包。

使用这些包的项目也将有 8 个构建。我想设置项目文件和目标,以便为特定的配置和平台选择特定的包。我正在努力解决这个问题,但这绝对没有意义。

目前我的项目文件中有以下内容:

  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v93SQL|x64' ">
    <PackageReference Include="OchALCommon.v93SQLx64" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v93SQL|x86' ">
    <PackageReference Include="OchALCommon.v93SQLx86" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v93HANA|x64' ">
    <PackageReference Include="OchALCommon.v93HANAx64" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v93HANA|x86' ">
    <PackageReference Include="OchALCommon.v93HANAx86" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v10SQL|x64' ">
    <PackageReference Include="OchALCommon.v10SQLx64" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v10SQL|x86' ">
    <PackageReference Include="OchALCommon.v10SQLx86" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v10HANA|x64' ">
    <PackageReference Include="OchALCommon.v10HANAx64" Version="1.0.*" />
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'B1v10HANA|x86' ">
    <PackageReference Include="OchALCommon.v10HANAx86" Version="1.0.*" />
  </ItemGroup>

Visual Studio 指出所有 8 个包和每个包的依赖关系的构建过程 - 这似乎是错误的。我还在“Directory.Build.targets”文件中尝试了这段代码,最初似乎可以工作,但随后 Visual Studio 停止响应目标文件中的更改(即使在重新启动后)。

我们过去一直使用这种引用与Assembly references,它似乎工作,我不知道如何制作PackageReference函数。有人知道如何在我的场景中最好地打包这个库吗?

在理想的世界中,我想以某种方式将我的 64 位和 32 位构建以及一些适当的目标存储在单个 nuget 包中,以便使用项目获得正确的位数和正确的子项目引用。目前我无法弄清楚如何做到这一点,也无法获得任何其他可行的方案。

再次,有人知道如何做这样的事情吗?

谢谢。

【问题讨论】:

  • 不知道 VS 如何查看所有引用,因为似乎没有 2 个条件可以同时为真(它真的在构建过程中尝试所有这些条件吗?),但你试过一个像&lt;PackageReference Include="OchALCommon.$(Configuration.Substring(2))$(Platform)" /&gt; 这样的单一参考?
  • 我实际上最终在最终文件名中使用了一些参数注入,并且有效,非常感谢。问题是 ProjectReference 是粘性的,计算的包列表保存在 projects.assets.json 中。 StackOverflow 上的另一个答案说,完成这项工作的唯一方法是将 PackageReference 放在一个单独的文件中,并有条件地包含该文件。我已经使用该技术制定了工作包的结构。
  • @MarkRabjohn 如果可能的话,我建议你总结并发布作为答案,这将是一个很好的参考,非常有用。

标签: msbuild visual-studio-2019


【解决方案1】:

到目前为止,我已经能够通过为每个 ProjectReference 使用单独的文件 (.targets) 来解决该问题,然后通过引用特定的 .targets 文件确保只有那些包含参考信息的文件包含在项目中。

然后我将 .targets 文件放入一个伞形 nupkg 中,以选择我想要使用的实际有效负载。整个安排如下:

我发布了8个payload文件如下:

  • MyLibrary.v93HANAx64
  • MyLibrary.v93HANAx86
  • MyLibrary.v93SQLx64
  • MyLibrary.v93SQLx86
  • MyLibrary.v10HANAx64
  • MyLibrary.v10HANAx86
  • MyLibrary.v10SQLx64
  • MyLibrary.v10SQLx86

每个有效负载文件仅包含一个标准 lib/net40 文件夹,其中包含 https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package 页面中指定的库。

我的消费项目有 4 个配置:

  • B1v93HANA
  • B1v93SQL
  • B1v10HANA
  • B1v10SQL

然后我有一个包含以下内容的总括项目“MyLibrary.Targets”:

##build/net40/MyLibrary.Targets.targets

<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
    <Import Project="MyLibrary/$(Configuration).targets" />
</Project>

##build/net40/MyLibrary/B1v10HANA.targets

<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <ItemGroup>
    <PackageReference Include="MyLibrary.v10HANA$(Platform)" Version="1.0.*" />
  </ItemGroup>
</Project>

##build/net40/MyLibrary/B1v10SQL.targets

<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <ItemGroup>
    <PackageReference Include="MyLibrary.v10SQL$(Platform)" Version="1.0.*" />
  </ItemGroup>
</Project>

##build/net40/MyLibrary/B1v93HANA.targets

<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <ItemGroup>
    <PackageReference Include="MyLibrary.v93HANA$(Platform)" Version="1.0.*" />
  </ItemGroup>
</Project>

##build/net40/MyLibrary/B1v93SQL.targets

<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <ItemGroup>
    <PackageReference Include="MyLibrary.v93SQL$(Platform)" Version="1.0.*" />
  </ItemGroup>
</Project>

我在这个文件夹中也有目标文件,“Debug.targets”和“Release.targets”在这些情况下使用我首选的默认库。

所有软件包的.nupec 文件都是非常标准的,根据上面链接的 Microsoft 指南。可以使用“nuget spec”命令创建默认的 nuspec 文件,然后可以对其进行编辑。

我的功能包 nuspec 文件在“包/元数据/依赖项”中包含标识所需包的内容:

    <dependencies>
      <group targetFramework=".NETFramework4.0">
          <dependency id="CryptLib" version="*" /> 
          <dependency id="SAPBusinessOneSDK.HANA" version="10.0.*" />
      </group>
    </dependencies>

我的选择器包“MyLibrary.targets”没有依赖项,但有一个文件部分“包/文件”:

  <files>
      <file src="readme.txt" target="" />
  
      <file src="build\**" target="build" />
  </files>

希望这可以节省一些时间。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2019-01-19
  • 2019-10-07
  • 2017-08-13
相关资源
最近更新 更多