如何告诉nuget将包资源文件添加为链接,而不是将它们复制到项目目录中 [英] How to tell nuget to add package resource files as links, and not copy them into project directory

查看:16
本文介绍了如何告诉nuget将包资源文件添加为链接,而不是将它们复制到项目目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将一些资源文件打包到一个 nuget 包中,通常会执行以下操作.

To pack some resource files into a nuget package, what one would normally do, is the following.

把所有的资源文件放到一个nuget包的content目录下.这将由 .nuspec 文件中的以下行指定:

Put all the resource files into the content directory of a nuget package. This would be specified by the following line in a .nuspec file:

<files>
  <file src="ProjectinReleasescript.js" target="contentjsscript.js" />
<files>

现在,当这个 nuget 包安装到 AnotherProject 中时,会出现以下文件结构:

Now, when this nuget package gets installed into AnotherProject, the following file structure emerges:

Solution.sln
packagesProject.1.0.0contentjsscript.js  // the original resource file
AnotherProjectjsscript.js                  // a physical copy 
AnotherProjectAnotherProject.csproj         // <Content /> tag (see below)

在安装包的过程中,AnotherProject.csproj 被注入了标签:

During package installation, AnotherProject.csproj was injected with tag:

<Content Include="jsscript.js" />

这是原始资源的物理副本(位于 packages 目录下).

and this is for the physical copy of the original resource (which is under packages directory).

我的目标不是在 AnotherProject 目录中拥有资源文件的物理副本,而是在 packages 目录下的原始资源的链接".在 csproj 中,应该是这样的:

My aim is not to have the physical copy of a resource file in the AnotherProject directory but rather a "link" to the original resource under packages directory. In the csproj, this should look like this:

<Content Include="packagesProject.1.0.0contentjsscript.js">
  <Link>jsscript.js</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

我宁愿避免的蛮力解决方案

现在,我能想到的一种硬着头皮做"的解决方法是:

Brute force solution that I would rather avoid

Now, one "do it the hard way" workaround I can think of is:

  • 不将资源文件放在 content 下,这样它们就不会被自动添加,
  • 编写 Install.ps1 脚本,该脚本将破解 csproj 文件结构并手动添加必要的 XML 片段,
  • not putting resource files under content so they do not get added automatically,
  • writing Install.ps1 script that would hack the csproj file structure and add the necessary XML piece manually,

然而,这有以下缺点:

  • 我所有的 nuget 包都需要在它们的 Install.ps1 中使用相同的脚本,
  • 在安装我的包时,Visual Studio 中会出现令人讨厌的项目重新加载提示".

推荐答案

由于 NuGet 目前不支持开箱即用,您可以选择使用 PowerShell 或使用自定义 MSBuild 目标.

Since NuGet currently does not support this out of the box your options are either to use PowerShell or to use a custom MSBuild target.

PowerShell

  • 将资源放在 NuGet 包中的 Content 目录之外(如您已建议的那样).
  • 在 install.ps1 中使用 PowerShell 添加文件链接.

如果您使用 Visual Studio 对象模型 (EnvDTE),您应该能够避免 项目重新加载提示.我会看看 Project.ProjectItems.AddFromFile(...) 看看它是否适合你.

You should be able to avoid the project reload prompt if you use the Visual Studio object model (EnvDTE). I would take a look at Project.ProjectItems.AddFromFile(...) to see if that works for you.

MSBuild 目标

  • NuGet 支持将导入语句添加到指向 MSBuild .props 和/或 .targets 文件的项目中.因此,您可以将资源放入 NuGet 包的 tools 目录中,并从自定义 MSBuild .props/.targets 文件中引用它们.
  • NuGet supports adding an import statement into a project that points to an MSBuild .props and/or .targets file. So you could put your resources into the tools directory of your NuGet package and reference them from a custom MSBuild .props/.targets file.

通常自定义 .props 和 .targets 用于自定义构建过程.但是它们只是 MSBuild 项目文件,因此您可以将资源项添加到这些项目文件中.

Typically the custom .props and .targets are used to customise the build process. However they are just MSBuild project files so you could add items for your resources into these project files.

请注意,安装 NuGet 包时,.props 会在项目文件的开头导入,而 .targets 会在项目结束时导入.

Note that .props are imported at the start of the project file when a NuGet package is installed, whilst .targets are imported at the end of the project.

自定义 NuGet

另一个需要更多工作的选项是修改 NuGet 以支持您想要做的事情.

Another option, which would take more work, would be to modify NuGet to support what you want to do.

这篇关于如何告诉nuget将包资源文件添加为链接,而不是将它们复制到项目目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆