NuGet更新和条件引用

NuGet更新和条件引用

本文介绍了NuGet更新和条件引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的nupkg软件包包含同一个dll的多个版本(x86,x64,AnyCPU),并且在csproj文件中,在引用中,我使用条件引用来根据当前平台集选择特定的dll.结果,我对同一个库有多个引用(只是不同的平台编译).

Our nupkg packages contain multiple versions of the same dll (x86, x64, AnyCPU) and in the csproj files, in references I use conditional references to pick a specific dll depending on the current platform set. As a result I have multiple references to the same library (just different platform compilation).

这是我的csproj文件的一部分:

here's a fragment of my csproj file:

<Reference Include="xxxx" Condition="'$(Platform)'=='x86'">
   <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x86\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx" Condition="'$(Platform)'=='x64'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x64\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx" Condition="'$(Platform)'=='AnyCPU'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\AnyCPU\xxxx.dll</HintPath>
</Reference>

此构造在MSBuild和Visual Studio中都非常有效.

This construction works very well in both MSBuild and in Visual Studio.

不幸的是,在 nuget更新之后,csproj引用被弄乱了.结果如下:

Unfortunately after nuget update the csproj references get messed up. Here's the result:

<Reference Include="xxxx" Condition="'$(Platform)'=='x86'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x86\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx" Condition="'$(Platform)'=='x64'">
  <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x64\xxxx.dll</HintPath>
</Reference>
<Reference Include="xxxx">
  <HintPath>..\..\packages\xxxx.2.7.0.1094\lib\net45\x86\xxxx.dll</HintPath>
</Reference>

因此,似乎只有一个引用被更新,并且... 条件部分被删除,并且使用了列表中的第一个dll.

So looks like only one reference got updated and... the Condition section got dropped as well as the first dll on the list was used.

不是我所期望的.有什么想法可以最好地解决该问题吗?有人在带有nuget的csproj中使用条件引用吗?任何建议将不胜感激!

Not what I was expecting. Any ideas how to best work around that problem? Anyone using conditional references in your csproj's with nuget? Any advice would be greatly appreciated!

推荐答案

Nuget允许您部署项目中自动包含的.targets文件(请参阅大量文档).您可以将条件引用包含在自定义目标文件中,并将dll部署到程序包的tools文件夹中,这样Nuget不会将它们自动添加为引用.

Nuget lets you deploy a .targets file that is automatically included in your project (see Nuget docs). Yo you can include the conditional references in the custom targets file and deploy the dlls in the tools folder of the package so they are not added as references by Nuget automatically.

让我们假设您的软件包名为"PackageWithConditionalReferences".创建nuget包的文件夹结构如下所示:

Lets assume your package is named 'PackageWithConditionalReferences'. The folder structure your nuget package is created from could look like this:

tools
    lib\net45\x86\xxxx.dll
    lib\net45\x64\xxxx.dll
    lib\net45\AnyCPU\xxxx.dll
build
    PackageWithConditionalReferences.targets

其中PackageWithConditionalReferences.targets具有内容:

where PackageWithConditionalReferences.targets has content:

    <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MyLibDir>$(MSBuildThisFileDirectory)..\tools\net45</MyLibDir>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="xxxx", Condition="'$(Platform)' == 'x64'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(MyLibDir)\x64\xxxx.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="xxxx", Condition="'$(Platform)' == 'x86'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(MyLibDir)\x86\xxxx.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="xxxx", Condition="'$(Platform)' == 'AnyCPU'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(MyLibDir)\AnyCPU\xxxx.dll</HintPath>
      <Private>True</Private>
    </Reference>
  </ItemGroup>

</Project>

确保您的.targets文件被命名为包喜欢.安装软件包后,必须重新启动VisualStudio才能使引用可见(已在VisualStudio 2015中进行了测试).

Make sure your .targets file is named liked the package. After installing the package a restart of VisualStudio is neccessary for the references to get visible (tested with VisualStudio 2015).

这篇关于NuGet更新和条件引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:39