![](/img/trans.png)
[英]How to setup a NuGet package to copy content files to output build directory?
[英]How to tell nuget to add package resource files as links, and not copy them into project directory
要將一些資源文件打包到nuget包中,通常會執行以下操作。
將所有資源文件放入nuget包的content\\
目錄中。 這將由.nuspec
文件中的以下行指定:
<files>
<file src="Project\bin\Release\script.js" target="content\js\script.js" />
<files>
現在,當這個NuGet包被安裝到AnotherProject
,下面的文件結構出現了:
Solution.sln
packages\Project.1.0.0\content\js\script.js // the original resource file
AnotherProject\js\script.js // a physical copy
AnotherProject\AnotherProject.csproj // <Content /> tag (see below)
在軟件包安裝過程中, AnotherProject.csproj
注入了標簽:
<Content Include="js\script.js" />
這是原始資源的物理副本(位於packages\\
目錄下)。
我的目標不是在AnotherProject
目錄中擁有資源文件的物理副本,而是在packages\\
目錄下擁有原始資源的“鏈接”。 在csproj中,這應該是這樣的:
<Content Include="packages\Project.1.0.0\content\js\script.js">
<Link>js\script.js</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
現在,我能想到的一個“難以實現”的解決方法是:
content\\
因此它們不會自動添加, Install.ps1
腳本並手動添加必要的XML部分, 然而,這具有以下缺點:
Install.ps1
使用相同的腳本片段, 由於NuGet目前不支持開箱即用,因此您可以選擇使用PowerShell或使用自定義MSBuild目標。
電源外殼
如果使用Visual Studio對象模型(EnvDTE),則應該能夠避免項目重新加載提示 。 我會看看Project.ProjectItems.AddFromFile(...),看看它是否適合你。
MSBuild目標
通常,自定義.props和.targets用於自定義構建過程。 但是它們只是MSBuild項目文件,因此您可以將資源項添加到這些項目文件中。
請注意,安裝NuGet包時會在項目文件的開頭導入.props,同時在項目結束時導入.targets。
定制NuGet
另一個需要做更多工作的選擇是修改NuGet以支持你想要做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.