【问题标题】:Excluding nuget package with x64 dll from output directory?从输出目录中排除带有 x64 dll 的 nuget 包?
【发布时间】:2021-10-14 17:27:45
【问题描述】:

我刚刚将 nuget 包 Grpc.Core.Api 添加到我的一个项目中。

此项目的构建平台目标标记为x86,但是构建过程将grpc_csharp_ext.x64.dll 和grpc_csharp_ext.x86.dll 放入我的输出目录。

大约 10.2MB,这感觉没有必要。有什么方法可以防止包含 x64 变体(除了在 Post-build 事件中手动添加删除命令)。

【问题讨论】:

    标签: nuget visual-studio-2019


    【解决方案1】:

    安装Grpc.Core NuGet包后可以看到.dll这两个文件:grpc_csharp_ext.x64.dll和grpc_csharp_ext.x86.dll,如果我只安装Grpc.Core.Api NuGet包,是不会生成的。

    通过查看详细的MSBuild输出日志,默认任务Copy复制了这两个文件。判断是复制还是跳过复制的逻辑(或控制)应该存放在Grpc.CoreNuGet包文件夹的.targets文件中。

    然后我在…\MyProject\packages\Grpc.Core.xxxxx\build\net45 下找到Grpc.Core.targets 文件。如果你找到并打开它,你会看到相关的设置:

    <ItemGroup Condition="'$(Grpc_SkipNativeLibsCopy)' != 'true'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x86\native\grpc_csharp_ext.x86.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>grpc_csharp_ext.x86.dll</Link>
          <Visible>false</Visible>
          <NuGetPackageId>Grpc.Core</NuGetPackageId>
        </Content>
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\grpc_csharp_ext.x64.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>grpc_csharp_ext.x64.dll</Link>
          <Visible>false</Visible>
          <NuGetPackageId>Grpc.Core</NuGetPackageId>
        </Content>
        ...
    

    &lt;condition="'$(Grpc_SkipNativeLibsCopy)'!='ture'"&gt; 和 &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt; 使 grpc_csharp_ext.x64.dll 被复制到项目输出目录。

    我认为这不能通过在.csproj 文件中使用 MSBuild 来覆盖、更改或阻止。正如您所提到的,在 Post-build 事件中手动添加删除命令应该可以工作,但它在文件被复制后才可以工作。

    我的想法/建议

    我可能会提供一个愚蠢或有风险的解决方法。我不建议修改Grpc.Core.targets文件,但是如果你把grpc_csharp_ext.x64.dll改成&lt;CopyToOutputDirectory&gt;Never&lt;/CopyToOutputDirectory&gt;,如果你只为x86构建这个文件就不会再生成了。

    这可能需要由此 NuGet 包的作者进行更改,除非这些操作(在 x86 下构建时将 ..x64 文件复制到输出目录)是必要的或由于某些我们没有注意到的原因。

    再次,我同意考虑使用构建后事件更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多