Nuget包contentFiles未复制到.NET Core项目

时间:2018-08-22 03:55:22

标签: c# .net-core nuget nuget-package

我正在尝试从Nuget包中提取内容文件到引用我的包的项目中。

基于Justin Emgarten's comment

  

Packages.config项目使用内容文件夹

     

Project.json / PackageReference / NETCore SDK项目使用contentFiles文件夹

很好,我创建了一个.NET Core 2.1控制台应用程序项目,并关注了NuGet ContentFiles Demystified博客文章,该文章写于2016年project.json时,但今天仍然可以使用。

我在c:\dev\ContentFilesExample\contentFiles\any\any\images\dnf.png创建了一个图像,然后创建了c:\dev\ContentFilesExample\ContentFilesExample.nuspec文件并复制粘贴了内容:

<?xml version="1.0"?>
<package>
    <metadata minClientVersion="3.3.0">
        <id>ContentFilesExample</id>
        <version>1.0.0</version>
        <authors>nuget</authors>    <!-- The NuGet team authored this package -->
        <owners>nuget</owners>      <!-- The NuGet team owns this package -->
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>A content v2 example package.</description>
        <tags>contentv2 contentFiles</tags>
        <!-- Build actions for items in the contentFiles folder -->
        <contentFiles>
            <!-- Include Assets as Content -->
            <files include="**/images/*.*" buildAction="EmbeddedResource" />
        </contentFiles>
    </metadata>
</package>

然后我用命令nuget pack ContentFilesExample.nuspec生成了Nuget程序包,并使用Nuget Package Explorer打开了它

enter image description here

太棒了,我的照片在那里。

现在是最后一个无效步骤。我在.NET Core 2.1项目中安装了此Nuget程序包,但是缺少该映像。在项目的根目录中,obj文件夹和bin文件夹中都没有图像的踪迹。

我试图关闭并重新打开Visual Studio,如某处的一些评论中所述,但这并不能解决问题。

我还尝试将.NET Core项目样式更改为PackageReference,但这仍然不能解决问题

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="ContentFilesExample" Version="1.0.0" />
    </ItemGroup>

    <ItemGroup>
        <None Update="appsettings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

</Project>

那我做错了什么? .net Core是否真的支持Nuget包中的内容文件?

谢谢

2 个答案:

答案 0 :(得分:4)

经过数小时的谷歌搜索以寻求解决方案后,我最终解决了这个问题,所以我决定写另一个答案来澄清问题因为 MS 文档很烂

    <files> 文件中的
  1. .nuspec 元素用于 packer。它告诉 nuget 要打包哪些文件(如果您的 nuspec 中没有 <files> 元素 - nuget 将使用目录命名约定)
  2. <contentFiles> 元素面向消费者 - 它告诉提取文件的方式和时间。
  3. 如果您希望在安装后一个文件出现在您的项目中 - 您必须打包一个 target 表示 contentFiles/any/any 其中“any/any”部分告诉nuget 它适用于任何语言和任何框架。
  4. (可选)如果您希望这个文件使用旧的nuget模式,使用packages.config - 你必须再次打包,第二次,这次 - 到“内容”文件夹,以便年长的消费者仍然可以使用它。

nuspec

  <metadata>
   ...
    <contentFiles>
      <files include="**/myfile.cs" />
    </contentFiles>
  </metadata>
  <files>
      <file src="myfile.cs" target="contentFiles\any\any" /> <!-- this is for new format -->
      <file src="myfile.cs" target="content" /> <!-- this is for the old format -->
  </files>

附注。我的博文 here

中有更多详细信息

答案 1 :(得分:0)

nupkg应该具有contentFiles/目录:
nupkg

安装到netcore项目:
enter image description here

在使用中的项目中它不会是一个松散的文件,在您构建项目时它将是一个EmbeddedResource
enter image description here

相关问题