我正在尝试将内容文件从 Nuget 包提取到引用我的包的项目中。
Packages.config projects use the content folder
Project.json/PackageReference/NETCore SDK projects use the contentFiles folder
太好了,我创建了一个 .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 打开它
太棒了,我的照片按预期出现了。
现在是最后的非工作步骤。我在我的 .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 包中的内容文件吗?
谢谢
最佳答案
经过几个小时的谷歌搜索解决方案,我最终找到了这个问题,所以我决定写另一个答案来弄清楚因为 MS 文档很烂
-
<files>
.nuspec
中的元素文件用于加壳器。它告诉 nuget 要打包哪些文件(如果您的 nuspec 中没有<files>
元素 - nuget 将使用目录命名约定) -
<contentFiles>
元素用于消费者 - 它告诉如何以及何时提取文件。 - 如果您希望某个文件在安装后出现在您的项目中 - 您必须使用
target
将其打包那说contentFiles/any/any
其中“任何/任何”部分告诉 nuget 它适用于任何语言和任何框架。 - (可选)如果您还希望此文件在使用 packages.config 的旧 nuget 模式下工作 - 您必须再次打包,第二次,这一次 - 到“内容”文件夹,这样年长的消费者仍然可以使用它。
核规范
<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
关于c# - Nuget 包内容文件未复制到 .NET Core 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51959638/