Nuget 包内容文件未复制到 .NET Core 项目
Nuget package contentFiles not copied to .NET Core project
我正在尝试将内容文件从 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 博客 post,该博客写于 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 包中的内容文件吗?
谢谢
nupkg 应该有 contentFiles/
目录:
安装到 netcore 项目:
它不会是消费项目中的松散文件,它会是一个 EmbeddedResource
当您构建项目时:
经过几个小时的谷歌搜索解决方案后,我最终解决了这个问题,所以我决定写另一个答案来弄清楚因为 MS 文档很烂
<files>
.nuspec
文件中的元素用于 packer。它告诉 nuget 哪些文件要 pack(如果你的 nuspec 中没有 <files>
元素 - nuget 将使用目录命名约定)
<contentFiles>
元素用于 消费者 - 它告诉如何以及何时 提取 文件。
- 如果你想让一个文件在安装后出现在你的项目中 - 你必须 pack 它带有一个
target
表示 contentFiles/any/any
其中“any/any" 部分告诉 nuget 它适用于任何语言和任何框架。
- (可选)如果您 也 希望此文件与使用 packages.config 的旧 nuget 模式一起工作 - 您必须 pack 再次,第二次,这次 - 到“content”文件夹,这样年长的消费者仍然可以使用它。
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>
PS。我的博客中有更多详细信息 post here
对我来说,contentFiles 不适用于 ProjectReferenes。我需要将我自己的 c++ dll 与 c# 代理一起使用。对于 packages.config 和 PackageReferences 的通用 nuget,我接下来做了:
c++ dll 和 MyCompany.MyDep.CppClient.targets 已添加到 nuget 数据包项目:
- CppClient(解决方案目录)
- CppClient(c#代理项目目录)
- lib\mycpp.dll (c++ dll)
- MyCompany.MyDep.CppClient.targets
- CppClient.csproj
- ...
- CppClient.sln.
向 CppClient.csproj 添加了下一个声明:
-- 这适用于 packages.config 个项目
总是
真的
内容\$(目标框架)
-- 这是针对 ProjectReferenes 项目的 - dll
总是
真的
构建\$(目标框架)
-- 这也适用于 ProjectReferenes 项目 - 复制 dll 的目标
总是
真的
构建\$(目标框架)
MyCompany.MyDep.CppClient.targets 是:
%(递归目录)%(文件名)%(扩展名)
保留最新
来自 CppClient(解决方案目录)运行 命令
dotnet pack CppClient -c Release --output ./package /p:Version=1.0.0 /p:PackageID=MyCompany.MyDep.CppClient
MyCompany.MyDep.CppClient.1.0.0.nupkg 有下一个结构:
- MyCompany.MyDep.CppClient.1.0.0
- build\arch
- MyCompany.MyDep.CppClient.targets
- mycpp.dll
- content\arch
- mycppdll
- lib\arch
- CppClient.dll
- ...
MyCompany.MyDep.CppClient.1.0.0.nupkg 可以安装到具有两种类型的 nuget 包管理的项目中。
对于具有 packages.config 的项目,在 nuget pack 中 content 目录的神奇部分,在将 NUGET PACK 安装到项目之后,它的所有内容将被复制到目标项目文件夹。然后在运行的时候复制到最终的bin目录下下载(最好自己做)。
对于带有 ProjectReferenes 作品的项目,魔法副本 build\arch\mycpp.dll by build\arch\MyCompany.MyDep.CppClient.targets不是在安装 nuget pack 之后,而是在目标项目已经构建时。 mycpp.dll 安装 nuget 包后不会复制到目标项目源目录。它将包含在目标项目的引用 nuget 中,并在构建目标项目后直接复制到二进制文件的输出目录。
我正在尝试将内容文件从 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 博客 post,该博客写于 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 包中的内容文件吗?
谢谢
nupkg 应该有 contentFiles/
目录:
安装到 netcore 项目:
它不会是消费项目中的松散文件,它会是一个 EmbeddedResource
当您构建项目时:
经过几个小时的谷歌搜索解决方案后,我最终解决了这个问题,所以我决定写另一个答案来弄清楚因为 MS 文档很烂
<files>
.nuspec
文件中的元素用于 packer。它告诉 nuget 哪些文件要 pack(如果你的 nuspec 中没有<files>
元素 - nuget 将使用目录命名约定)<contentFiles>
元素用于 消费者 - 它告诉如何以及何时 提取 文件。- 如果你想让一个文件在安装后出现在你的项目中 - 你必须 pack 它带有一个
target
表示contentFiles/any/any
其中“any/any" 部分告诉 nuget 它适用于任何语言和任何框架。 - (可选)如果您 也 希望此文件与使用 packages.config 的旧 nuget 模式一起工作 - 您必须 pack 再次,第二次,这次 - 到“content”文件夹,这样年长的消费者仍然可以使用它。
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>
PS。我的博客中有更多详细信息 post here
对我来说,contentFiles 不适用于 ProjectReferenes。我需要将我自己的 c++ dll 与 c# 代理一起使用。对于 packages.config 和 PackageReferences 的通用 nuget,我接下来做了:
c++ dll 和 MyCompany.MyDep.CppClient.targets 已添加到 nuget 数据包项目:
- CppClient(解决方案目录)
- CppClient(c#代理项目目录)
- lib\mycpp.dll (c++ dll)
- MyCompany.MyDep.CppClient.targets
- CppClient.csproj
- ...
- CppClient.sln.
向 CppClient.csproj 添加了下一个声明:
-- 这适用于 packages.config 个项目 总是 真的 内容\$(目标框架) -- 这是针对 ProjectReferenes 项目的 - dll 总是 真的 构建\$(目标框架) -- 这也适用于 ProjectReferenes 项目 - 复制 dll 的目标 总是 真的 构建\$(目标框架)MyCompany.MyDep.CppClient.targets 是:
%(递归目录)%(文件名)%(扩展名) 保留最新来自 CppClient(解决方案目录)运行 命令 dotnet pack CppClient -c Release --output ./package /p:Version=1.0.0 /p:PackageID=MyCompany.MyDep.CppClient
MyCompany.MyDep.CppClient.1.0.0.nupkg 有下一个结构:
- MyCompany.MyDep.CppClient.1.0.0
- build\arch
- MyCompany.MyDep.CppClient.targets
- mycpp.dll
- content\arch
- mycppdll
- lib\arch
- CppClient.dll
- ...
- build\arch
- MyCompany.MyDep.CppClient.1.0.0
MyCompany.MyDep.CppClient.1.0.0.nupkg 可以安装到具有两种类型的 nuget 包管理的项目中。 对于具有 packages.config 的项目,在 nuget pack 中 content 目录的神奇部分,在将 NUGET PACK 安装到项目之后,它的所有内容将被复制到目标项目文件夹。然后在运行的时候复制到最终的bin目录下下载(最好自己做)。 对于带有 ProjectReferenes 作品的项目,魔法副本 build\arch\mycpp.dll by build\arch\MyCompany.MyDep.CppClient.targets不是在安装 nuget pack 之后,而是在目标项目已经构建时。 mycpp.dll 安装 nuget 包后不会复制到目标项目源目录。它将包含在目标项目的引用 nuget 中,并在构建目标项目后直接复制到二进制文件的输出目录。