简体   繁体   English

如何设置NuGet包以将内容文件复制到输出构建目录?

[英]How to setup a NuGet package to copy content files to output build directory?

NOTE: follow up from this question (now closed), as that one was too broad in scope, and the first part of that question, regarding .dlls, has been resolved, and this is a separate issue. 注意:由于该问题的范围太广, 因此对该问题 (现已结束)进行了跟进,并且该问题的第一部分有关.dll的问题已解决,这是一个单独的问题。

I'm working on a .NET Standard 2.0 project called ComputeSharp that I'd like to publish as a NuGet package, but I can't figure out how to have the package copy a content file to the output build directory of a project using it. 我正在开发一个名为ComputeSharp.NET Standard 2.0项目 ,我想将其发布为NuGet程序包,但我不知道如何让程序包使用以下命令将内容文件复制到项目的输出生成目录中:它。 Some info: 一些信息:

  • The project only targets .NET Standard 2.0 该项目仅针对.NET Standard 2.0
  • The project uses 2 referenced projects in the same solution, each using some other NuGet packages. 该项目在同一解决方案中使用2个引用的项目,每个项目都使用其他一些NuGet软件包。 Here is a screen of my current solution. 是我当前解决方案的屏幕。
  • My .nuspec is in the folder of the main project, ComputeSharp , and has the following structure: 我的.nuspec位于主项目ComputeSharp的文件夹中,并具有以下结构:
<?xml version="1.0"?>
<package >
  <metadata>
    <id>ComputeSharp</id>
    ...
    <dependencies>
      <dependency id="SharpDX.Direct3D12" version="4.2.1-beta0-gab36f12303" />
      ...
    </dependencies>
    <contentFiles>
      <files include="..\ComputeSharp.Shaders\Renderer\Templates\*.mustache" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>
</package>
  • In order to create the NuGet package, I first build ComputeSharp in Release mode, then open a cmd in the folder for that project, and run: 为了创建NuGet包,我首先在Release模式下构建ComputeSharp ,然后在该项目的文件夹中打开一个cmd,然后运行:
nuget pack ComputeSharp.csproj -Prop Configuration=Release -IncludeReferencedProjects
  • The resulting NuGet package, when inspected, looks like this (which looks perfectly fine, as I can see the .mustache file being present in the right folder structure in the package, under the "content" directory): 生成的NuGet包在检查时看起来像这样(看起来非常好,因为我可以看到包中正确的文件夹结构中“目录”目录下的.mustache文件):
ComputeSharp.x.x.x.nupkg
├───rels
│   └───...
├───content
│   └───Renderer
│       └───Templates
│           └───ShaderTemplate.mustache
├───lib
│   └───netstandard2.0
│       ├───ComputeSharp.dll
│       ├───ComputeSharp.Graphics.dll
│       └───ComputeSharp.Shaders.dll
├───package
│   └───...
├───[Content_Types].xml
└───ComputeSharp.nuspec

PROBLEM : once I create a test project and install the NuGet package, I can build it just fine, but the whole Renderer\\Templates\\ShaderTemplate.mustache tree is not copied in the build directory, so as a result my lib can't load that file (as it's loaded relative to the path of the lib assembly). 问题 :一旦我创建了一个测试项目并安装了NuGet软件包,就可以很好地构建它,但是整个Renderer\\Templates\\ShaderTemplate.mustache树都没有复制到构建目录中,因此我的lib无法加载该文件(因为它是相对于lib程序集的路径加载的)。

I've read countless SO questions as well as the docs, and tried a bunch of combinations here (eg. setting ContentType="None" instead of "Content" , but the result is always the same: the .mustache file is present in the package but it's not copied to the build directory of the project using it. Is there something else I need to do to just have the NuGet package recreate that tree + file in the output directory, when a project is built? 我已经阅读了无数SO问题以及文档,并在这里尝试了很多组合(例如,设置ContentType="None"而不是"Content" ,但结果始终相同: .mustache文件存在于程序包,但不会使用该程序包复制到项目的构建目录中。构建项目时,我还需要做一些其他事情来使NuGet程序包在输出目录中重新创建该树+文件吗?

Thank you for your help! 谢谢您的帮助!

I suggest embedding the files you need in the assembly with build action "Embedded resource". 我建议通过生成操作“嵌入的资源”将所需的文件嵌入到程序集中。 That way you don't have to rely on Nuget to install them. 这样,您不必依靠Nuget来安装它们。 Instead, upon first usage, you can read them from the assembly and copy them into the file system or directly consume them. 相反,在首次使用时,您可以从程序集中读取它们并将它们复制到文件系统中或直接使用它们。 Here is how to read an embedded file from the assembly and copy it into the file system: 这是从程序集中读取嵌入式文件并将其复制到文件系统中的方法:

private void CopyEmbeddedResourceToFile(Assembly assembly, string resourceName, string filePath)
{
        var key = GetResourceKey(assembly, resourceName);
        using (Stream stream = assembly.GetManifestResourceStream(key))
        using (var file = new FileStream(filePath, FileMode.Create))
        {
            if (stream == null)
                throw new ArgumentException($"Resource name '{resourceName}' not found!");
            stream.CopyTo(file);
        }
}

You want to use the contentFiles folder and not content . 您要使用contentFiles文件夹而不是content See this blog NuGet ContentFiles Demystified . 请参阅此博客NuGet ContentFiles Demystified

You can also read Enable support for 'content' folder with PackageReference that explains why the content folder doesn't work with PackageReference . 您还可以阅读使用PackageReference启用对“内容”文件夹的支持,以解释为何content文件夹不能与PackageReference

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nuget包将exe内容文件复制到项目输出目录 - Nuget package to copy exe content file to project output directory 将 nuget package 内容文件(类型为.exe)复制到使用该 nuget package 的项目的 output 目录 - Copy nuget package content file (of type .exe) to output directory of project using that nuget package 使用 .csproj 和 dotnet pack 命令中的 MsBuild 将文件从 Nuget 包复制到输出目录 - Copy files from Nuget package to output directory with MsBuild in .csproj and dotnet pack command VS代码:如何根据构建配置将文件复制到输出目录 - VS Code: How to copy files to output directory depending on build configurations 如何告诉nuget将包资源文件添加为链接,而不是将它们复制到项目目录中 - How to tell nuget to add package resource files as links, and not copy them into project directory 将 NuGet .pdb 复制到输出目录 - Copy NuGet .pdb to output directory 如何让 VS 将 nuget 包 dll 复制到 output 目录 - How to make VS to copy nuget packages dll's to output directory 每当我在NetStandard Library Project中使用Nuget包时,是否都必须将DLL从Nuget包复制到输出目录中? - Do I have to copy the DLLs from the Nuget Package to the output directory everytime I use the Nuget Package in NetStandard Library Project 如何强制将 nuget package 参考 dll 复制到 output 目录? - How to force nuget package reference dlls to be copied to output directory? 如何复制NuGet软件包的本机库文件? - How can I copy the NuGet package native library files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM