防止在NuGet content和contentFiles文件夹中重复文件 [英] Prevent duplicating files in NuGet content and contentFiles folders

查看:318
本文介绍了防止在NuGet content和contentFiles文件夹中重复文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的NuGet软件包需要传递一些相当大的文件来构建输出目录.

在旧的NuGet模型中,此类文件必须存储在.nupkgcontent文件夹中.在 NuGet 3.3中引入的新模型中,此类文件必须是存储在 contentFiles文件夹中./p>

要与旧版本的NuGet保持兼容性,主要是与解决方案

如果您只想将文件输出到构建输出(content仅将文件复制到输出目录,但会导致将其设置为copy输出目录项),可以通过创建将包含在项目中的msbuild文件来使用完全不同的方法.

您可以通过将两个文件(例如test.jpg)放入tools文件夹(也可以使用build)并将Your.Package.Id.targets文件添加到build文件夹(名称为您的包裹的包裹ID(后缀为.targets),其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Content Include="$(MSBuildThisFileDirectory)..\tools\test.jpg">
      <Link>test.jpg</Link>
      <Visible>false</Visible>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
</Project>

无论使用哪种样式"的NuGet引用(packages.configPackageReference),该目标都将自动导入到项目文件中,并且应向后兼容VS的较早版本,只要它们支持NuGet和ToolsVersion 4.0.

Link元数据表示文件在输出目录/发布目录中的最终位置.您可以将其设置为defaultContent\images\foo.jpg创建嵌套结构并重命名文件. (您甚至可以使用MSBulid变量来使用某些引用项目的配置). Visible元数据阻止解决方案资源管理器显示文件的完整相对路径,该路径可能最终导致大量嵌套的..节点. CopyToPublishDirectory适用于使用Publish发布目标的.NET Core/ASP.NET Core应用程序或基于SDK的项目.

请注意,您可以将Inclue -path设置为任何值,具体取决于文件在文件包中的位置.您也可以使用通配符(但将Link设置为%(Filename)%(Extension))

My NuGet package needs to deliver some rather large files to build output directory.

In an old NuGet model, such files have to be stored in content folder of the .nupkg. While in a new model introduced in NuGet 3.3, such files have to be stored in contentFiles folder.

To maintain a compatibility with older versions of NuGet and mainly with Package.config package management format, I need to duplicate the files into both folders. That unfortunately almost doubles a size of the package.

Is there a way to prevent that? Can I somehow link contentFiles to content folder?

解决方案

If you only want to output the file to the build output (content only copies the file to the output directory but does cause it to be set as copy to output directory item), you can use a completely different approach by creating an msbuild file that will be included in the project.

You can do this by putting both the file - say test.jpg into the tools folder (you could also use build) and add a Your.Package.Id.targets file to the build folder (the name being the package id of your package with .targets as extension) with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Content Include="$(MSBuildThisFileDirectory)..\tools\test.jpg">
      <Link>test.jpg</Link>
      <Visible>false</Visible>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
</Project>

This target will be automatically imported into the project files regardless of which "style" of NuGet reference is used (packages.config, PackageReference) and should be backwards compatible to older versions of VS as long as they support NuGet and ToolsVersion 4.0.

The Link metadata denotes where in the output / publish directories the file will end up. You could set it to e.g. defaultContent\images\foo.jpg to create a nested structure and rename the file. (you could even use MSBulid variables to use some of the referencing project's configuration). The Visible metadata prevents the solution explorer from showing the full relative path to the file, which could end up in lots of nested .. nodes. The CopyToPublishDirectory applies to .NET Core / ASP.NET Core apps or SDK-based projects using the Publish target for publishing.

Note that you can set the Inclue-path to anything depending on where in your package the file is. You can also use wildcards (but then set Link to %(Filename)%(Extension))

这篇关于防止在NuGet content和contentFiles文件夹中重复文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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