【问题标题】:How to copy a text file from NuGet package during build of C# code that uses the package?如何在构建使用包的 C# 代码期间从 NuGet 包中复制文本文件?
【发布时间】:2018-07-04 05:16:48
【问题描述】:

如何复制与 NuGet 包中的 DLL 一起包含的文本文件?

当我在另一个解决方案(本示例中为c:\dev\)中使用我的自定义 NuGet 包时,c:\dev\package\projectId\lib\netstandard2.0\ 中生成的文件结构包含许多 DLL 和一个文本文件,例如 file.txt。在构建解决方案时会复制所有 DLL,但留下了文本文件。

.nuspec 文件中,我最初将文件包含在<files><file src="foo\file.txt" target="lib\netstandard2.0"/></files> 下。还原 NuGet 包时,file.txt 最终会出现在 packages 文件夹中,但不会复制到构建目录中。

尝试 1:我尝试在 nuspec 文件中使用 contentFiles 属性,因为 nuspec reference 多次指向那里。我得到了nuget.exe pack 命令来使用这个新属性(即没有语法错误),但内容(file.txt)的处理方式没有改变。

尝试 2:我尝试使用 projectId.targets 文件。这使用了一个Target,它有一个包含该文件的ItemGroup。然后,我尝试使用Copy 事件,将目标文件夹指向为$(OutputPath)

将包中包含的文件复制到构建目录似乎非常困难,不得不深入研究 MSBuild 事件等。

我在这里不知所措,欢迎任何指点。

编辑#1:

我尝试根据以下建议将此部分添加到元数据中:

<contentFiles> <files include="any\any\file.txt" buildAction="EmbeddedResource" /> </contentFiles>

这适用于一个小测试用例。 file.txt 在 Visual Studio 和构建目录中都很好地显示。奇怪的是,它在我的主项目中使用相同的确切语法不起作用(我在两者中都使用 .NET Core 2.0)。此外,在 NuGet 包资源管理器中,它单独显示在包内容中。但是当我在&lt;files&gt;&lt;file src="lib\netstandard2.0\test.dll" target="lib\netstandard2.0"/&gt;&lt;/files&gt; 下添加一些内容时,它会从该视图中消失。

编辑#2:

我认为还有其他事情发生...Here is the .nuspec file from our main project。当我添加具有以下工作建议的内容文件时,它仍然没有显示(对于 .NET Core 2.0 或 .NET Framework 4.7.1)。 .targets 文件是否以某种方式搞砸了?

【问题讨论】:

    标签: c# msbuild nuget nuget-package nuget-spec


    【解决方案1】:

    如何在构建使用该包的 C# 代码时从 NuGet 包中复制文本文件?

    您应该使用contentFiles 属性并将copyToOutput="true" 设置为文本文件file.txt

    我的测试 .nuspec 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
      <metadata>
        <id>MyTestCore</id>
        <version>5.0.0</version>
        <authors>TestContentFile</authors>
        <owners>TestContentFile</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Package Description</description>
        <contentFiles>
          <files include="any/any/file.txt" buildAction="content" flatten="true" copyToOutput="true"/>
        </contentFiles>
      </metadata>
    
      <files>
        <file src="contentFiles/any/any/file.txt" target="contentFiles/any/any" />
      </files>
    </package>
    

    打包完这个.nuspec文件,然后将nuget包添加到项目中,构建项目,文本文件将复制到构建目录:

    查看update answer for the similar issue 了解更多详情。

    【讨论】:

    • 感谢您的指点和视频!据推测,测试包应该只有./any/any/file.txt,对吗?如果我使用上面的 .nuspec 文件,我会收到以下错误:Could not find a part of the path 'E:\source\repos\mzLib\contentFiles'. 当我从 &lt;file src="contentFiles/any/any/file.txt" target="contentFiles/any/any" /&gt; 的 src 属性中删除 contentFiles/ 时,测试用例有效。
    • 我得到的效果与我在上面 Edit # 1 中提到的效果相同...此修复不适用于我的主项目,但至少 contentFile 显示在测试用例的 NuGet 包资源管理器。
    【解决方案2】:

    您必须为文件定义构建操作。

    <contentFiles>
            <!-- Include Assets as Content -->
            <files include="foo\file.txt" buildAction="EmbeddedResource" />
    </contentFiles>
    

    【讨论】:

    • 感谢您的指点。这确实有效,但在测试用例之外我仍然遇到问题。我在上面的问题中添加了我所看到的内容。
    猜你喜欢
    • 2013-02-03
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2013-10-22
    相关资源
    最近更新 更多