将 NuGet 包中的本机文件添加到项目输出目录 [英] Add native files from NuGet package to project output directory

查看:21
本文介绍了将 NuGet 包中的本机文件添加到项目输出目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 .Net 程序集创建 NuGet 程序包,该程序集会 pinvoke 到本机 win32 dll.我需要将程序集和本机 dll 打包,并将程序集添加到项目引用中(这部分没有问题),并且应将本机 dll 复制到项目输出目录或其他一些相关目录中.

I'm trying to create NuGet package for a .Net assembly which does pinvoke to a native win32 dll. I need to pack both the assembly and the native dll with the assembly added to the project references (no problem at this part) and the native dll should be copied into the project output directory or some other relative directory.

我的问题是:

  1. 如何在没有 Visual Studio 尝试将其添加到引用列表的情况下打包本机 dll?
  2. 我是否必须编写一个 install.ps1 来复制本机 dll?如果是这样,我如何访问包内容以进行复制?

推荐答案

使用targets文件中的Copy目标来复制需要的库,不会将这些文件复制到引用该项目的其他项目中,导致 DllNotFoundException.这可以通过一个更简单的目标文件来完成,使用 None 元素,因为 MSBuild 会将所有 None 文件复制到引用项目.

Using the Copy target in the targets file to copy required libraries won't copy those files to other projects which reference the project, resulting in a DllNotFoundException. This can be done with a much simpler targets file though, using a None element, as MSBuild will copy all None files to referencing projects.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <NativeLibs Include="$(MSBuildThisFileDirectory)***.dll" />
    <None Include="@(NativeLibs)">
      <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

将目标文件与所需的本机库一起添加到 nuget 包的 build 目录.目标文件将包含 build 目录的所有子目录中的所有 dll 文件.因此,要添加 Any CPU 托管程序集使用的本机库的 x86x64 版本,您最终会得到类似于以下的目录结构以下:

Add the targets file to the build directory of the nuget package along with the required native libraries. The targets file will include all dll files in all child directories of the build directory. So to add an x86 and x64 version of a native library used by an Any CPU managed assembly you would end up with a directory structure similar to the following:

  • 构建
    • x86
      • NativeLib.dll
      • NativeLibDependency.dll
      • NativeLib.dll
      • NativeLibDependency.dll
      • net40
        • ManagedAssembly.dll

        构建时,将在项目的输出目录中创建相同的 x86x64 目录.如果您不需要子目录,则可以删除 **%(RecursiveDir) 并在 build 中包含所需的文件直接目录.其他需要的内容文件也可以用同样的方法添加.

        The same x86 and x64 directories will be created in the project's output directory when built. If you don't need subdirectories then the ** and the %(RecursiveDir) can be removed and instead include the required files in the build directory directly. Other required content files can also be added in the same way.

        在目标文件中添加为 None 的文件在 Visual Studio 中打开时不会显示在项目中.如果您想知道为什么我不使用 nupkg 中的 Content 文件夹,那是因为无法设置 CopyToOutputDirectory 元素 不使用 powershell 脚本(它只会在 Visual Studio 中运行,而不是从命令提示符、构建服务器或其他 IDE 中运行,并且是 在 project.json/xproj DNX 项目中不支持),我更喜欢使用 Link 到文件,而不是在项目中拥有文件的额外副本.

        The files added as None in the targets file won't be shown in the project when open in Visual Studio. If you are wondering why I don't use the Content folder in the nupkg it's because there's no way to set the CopyToOutputDirectory element without using a powershell script (which will only be run inside Visual Studio, not from the command prompt, on build servers or in other IDEs, and is not supported in project.json / xproj DNX projects) and I prefer to use a Link to the files rather than having an additional copy of the files within the project.

        更新:尽管这也应该与 Content 而不是 None 一起使用,但似乎 msbuild 中存在一个错误,因此文件不会被复制到引用项目超过一个步骤(例如 proj1-> proj2 -> proj3,proj3 不会从 proj1 的 NuGet 包中获取文件,但 proj2 会).

        Update: Although this should also work with Content rather than None it appears that there's a bug in msbuild so files won't be copied to referencing projects more than one step removed (e.g. proj1 -> proj2 -> proj3, proj3 won't get the files from proj1's NuGet package but proj2 will).

        这篇关于将 NuGet 包中的本机文件添加到项目输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆