![](/img/trans.png)
[英]Nuget package `contentFiles` are not getting copied into the project
[英]Nuget package contentFiles not copied to .NET Core project
我正在嘗試將 Nuget 包中的內容文件提取到引用我的包的項目中。
Packages.config 項目使用 content 文件夾
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\\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 包資源管理器打開它
太好了,我的照片按預期在那里。
現在是最后的非工作步驟。 我在我的 .NET Core 2.1 項目中安裝了這個 Nuget 包,但缺少圖像。 在我的項目的根目錄中,無論是在obj
文件夾中還是在bin
文件夾中,都沒有圖像的痕跡。
我嘗試關閉並重新打開視覺工作室,如某處的某些評論所述,但這並沒有解決問題。
我還嘗試將我的 .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
打包,該target
表示contentFiles/any/any
其中“any/any”部分告訴 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>
附注。 在我的博客文章更多的細節在這里
對我來說,內容文件不適用於 ProjectReferenes。 我需要使用我自己的 c++ dll 和 c# 代理。 對於packages.config 和PackageReferences 的通用nuget,我接下來做了:
c++ dll 和 MyCompany.MyDep.CppClient.targets 被添加到 nuget packet 項目中:
在 CppClient.csproj 中添加了下一個聲明:
-- 這適用於packages.config 項目始終為真內容\\$(TargetFramework) -- 適用於ProjectReferenes 項目- dll 始終為真構建\\$(TargetFramework) -- 這也適用於ProjectReferenes 項目- 復制dll 的目標始終為真構建\\$(目標框架)MyCompany.MyDep.CppClient.targets 是:
%(RecursiveDir)%(FileName)%(Extension) PreserveNewest從 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.nupkg 可以安裝到具有這兩種類型的 nuget 包管理的項目中。 對於帶有packages.config的項目,nuget包中內容目錄的魔法部分,所有內容將被復制到目標項目文件夾,在NUGET PACK安裝到項目之后。 然后將它復制到最終的 bin 目錄,以便在運行時下載(你應該自己做)。 對於帶有 ProjectReferenes 的項目,在安裝 nuget 包之后,通過build\\arch\\MyCompany.MyDep.CppClient.targets魔法復制build\\arch\\mycpp.dll ,但當目標項目已經構建時。 安裝 nuget 包后,mycpp.dll 不會被復制到目標項目源目錄。 它將包含在目標項目的引用 nuget 中,並在構建目標項目后直接復制到二進制文件的輸出目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.