问题描述
我的NuGet软件包需要传递一些相当大的文件来构建输出目录.
在旧的NuGet模型中,此类文件必须存储在.nupkg
的content
文件夹中.在 NuGet 3.3中引入的新模型中,此类文件必须是存储在 contentFiles
文件夹中./p>
要与旧版本的NuGet保持兼容性,主要是与 Package.config
包管理格式,我需要将文件复制到两个文件夹中.不幸的是,这几乎使包装的尺寸增加了一倍.
有没有办法防止这种情况?我可以通过某种方式将contentFiles
链接到content
文件夹吗?
如果您只想将文件输出到构建输出(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.config
,PackageReference
),该目标都将自动导入到项目文件中,并且应向后兼容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文件夹中重复文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!