防止在 NuGet 内容和 contentFiles 文件夹中复制文件 [英] Prevent duplicating files in NuGet content and contentFiles folders
问题描述
我的 NuGet 包需要交付一些相当大的文件来构建输出目录.
My NuGet package needs to deliver some rather large files to build output directory.
在旧的 NuGet 模型中,此类文件必须存储在 .nupkg
的 content
文件夹中.在 NuGet 3.3 中引入的新模型 中,必须存储此类文件在 contentFiles
文件夹中.
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.
保持与旧版本 NuGet 的兼容性,主要与 Package.config
包管理格式,我需要将文件复制到两个文件夹中.不幸的是,这几乎是包装尺寸的两倍.
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.
有没有办法防止这种情况发生?我可以以某种方式将 contentFiles
链接到 content
文件夹吗?
Is there a way to prevent that? Can I somehow link contentFiles
to content
folder?
推荐答案
如果只想将文件输出到构建输出(content
只是将文件复制到输出目录但确实会导致设置为复制到输出目录项),您可以通过创建将包含在项目中的 msbuild 文件来使用完全不同的方法.
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.
您可以通过将文件(例如 test.jpg
放入 tools
文件夹(您也可以使用 build
)和将 Your.Package.Id.targets
文件添加到 build
文件夹(名称是您的包的包 ID,以 .targets
作为扩展名) 内容如下:
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).. ools est.jpg">
<Link>test.jpg</Link>
<Visible>false</Visible>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>
无论哪种风格",该目标都会自动导入到项目文件中.使用了 NuGet 引用(packages.config
、PackageReference
)并且应该向后兼容旧版本的 VS,只要它们支持 NuGet 和 ToolsVersion 4.0代码>.
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
.
Link
元数据表示文件将在输出/发布目录中的哪个位置结束.您可以将其设置为例如defaultContentimagesfoo.jpg
创建嵌套结构并重命名文件.(您甚至可以使用 MSBulid 变量来使用一些引用项目的配置).Visible
元数据阻止解决方案资源管理器显示文件的完整相对路径,这可能最终出现在许多嵌套的 ..
节点中.CopyToPublishDirectory
适用于 .NET Core/ASP.NET Core 应用程序或使用 Publish
目标进行发布的基于 SDK 的项目.
The Link
metadata denotes where in the output / publish directories the file will end up. You could set it to e.g. defaultContentimagesfoo.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.
请注意,您可以将 Inclue
-path 设置为任何内容,具体取决于文件在包中的位置.您也可以使用通配符(但随后将 Link
设置为 %(Filename)%(Extension)
)
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 内容和 contentFiles 文件夹中复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!