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

问题描述

我正在尝试为一个.net程序集创建NuGet程序包,该程序集不会拼写到本机的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?如果可以,我如何访问包装内容以进行复制?

推荐答案

在目标文件中使用Copy目标复制所需的库不会将这些文件复制到引用该项目的其他项目,从而导致.不过,这可以通过使用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
      • build
        • x86
          • NativeLib.dll
          • NativeLibDependency.dll
          • NativeLib.dll
          • NativeLibDependency.dll
          • net40
            • ManagedAssembly.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.

              在Visual Studio中打开时,在目标文件中添加为None的文件将不会显示在项目中.如果您想知道为什么我不使用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包中的本机文件添加到项目输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:46