问题描述
我正在尝试创建内容文件(没有托管程序集)的.NET标准nuget包,以供.NET Framework程序集使用.我的目的是将这个文件的文件夹复制到使用程序集的bin文件夹中.
I am trying to create a .NET standard nuget package of content files (with no managed assemblies), to be consumed by .NET Framework assemblies. My aim is to get this folder of files copied to the bin folder of the consuming assembly.
这个问题类似于将NuGet包中的本机文件添加到项目输出目录(并使用此nuget包 http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/),但我需要我的nuget软件包为.NET Standard
This question is similar Add native files from NuGet package to project output directory (and uses this nuget package http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/), but I require my nuget package to be .NET Standard
我已将文件添加到程序集中的contentfiles \ any \ any \ myfiles下,并将以下内容添加到nuspec文件的元数据部分:
I have added the files in the assembly under contentfiles\any\any\myfiles and added the following to the metadata section of the nuspec file:
<contentFiles>
<files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>
当我尝试将其添加到.NET Framework程序集时出现错误
When I try to add this to a .NET framework assembly I get the error
我还尝试将文件添加到项目的根目录,并在nuspec文件的元数据元素下面添加以下内容:
I have also tried adding the files to the root of the project and adding the following beneath the metadata element in the nuspec file:
<files>
<file src="myfiles\**\*" target="myfiles" />
</files>
完整的nuspec文件:
Full nuspec file:
<?xml version="1.0"?>
<package>
<metadata>
<id>myPackage</id>
<version>1.0.0</version>
<title>myPackage</title>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>my files/description>
<releaseNotes></releaseNotes>
<tags></tags>
<contentFiles>
<files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="myfiles\**\*" target="myfiles" />
</files>
</package>
此外,在csproj中,我还将TargetFramework元素更改为:
Also, in the csproj I've also changed the TargetFramework element to:
<TargetFrameworks>netstandard1.3;netstandard2.0;net40;net45;net46</TargetFrameworks>
解决方案:
最后,这是解决方案:"myFiles"是一个位于项目根目录中的文件夹,其中包含许多不同的文件和子目录.
In the end, this is the solution: "myFiles" is a folder that sits in the project root and contains many different files and sub-directories.
nuspec文件:
<?xml version="1.0"?>
<package >
<metadata>
<id>myPackage</id>
<version>1.0.0</version>
<title>My Package</title>
<tags></tags>
<dependencies>
<group targetFramework=".NETStandard2.0" />
<group targetFramework=".NETFramework4.6.2" />
</dependencies>
<contentFiles>
<!-- this sets "CopyIfNewer" on all files in the project that references this package -->
<files include="**/*" buildAction="None" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="myFiles\**\*" target="contentFiles\any\any\myFiles"/>
</files>
</package>
引用nuget包的项目将似乎有一个名为"myfiles"的文件夹(所有文件上带有链接图标),该文件夹将在构建时复制到bin文件夹中.
The project that references the nuget package will appear to have a folder called "myfiles" (with a link icon over all the files) which will get copied to the bin folder on build.
推荐答案
有一些推断的方法,但是add可以在此处将targetFramework添加为依赖项设置.
There are some inferred ways to do this, but add can add the targetFramework as setting of dependencies here.
<dependencies>
<group targetFramework=".NETStandard1.3" />
<group targetFramework=".NETStandard2.0" />
<group targetFramework=".NETFramework4.0" />
<group targetFramework=".NETFramework4.5" />
<group targetFramework=".NETFramework4.6" />
<group targetFramework=".NETFramework4.6.2" />
</dependencies>
我还建议在nuspec文件的files部分中将.dll文件与您的内容文件明确分开,并引用lib\<targetName>\
约定.(实际上与我上面引用的推断情感有关,但我暂时没有太多细节可提供)
I would also recommend separating the .dll files explicitly from your content files in the files section of the nuspec file, and reference the lib\<targetName>\
convention.. (which actually relates to the inferred sentiment I reference to above, but I don't have much detail to provide on that at the moment)
因此您的nuspec文件将如下所示:
So your nuspec file would look like this:
<?xml version="1.0"?>
<package>
<metadata>
<id>myPackage</id>
<version>1.0.0</version>
<title>myPackage</title>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>my files/description>
<releaseNotes></releaseNotes>
<tags></tags>
<contentFiles>
<files include="any/netstandard1.3/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/netstandard2.0/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net40/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net45/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net46/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net462/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>
<dependencies>
<group targetFramework=".NETStandard1.3" />
<group targetFramework=".NETStandard2.0" />
<group targetFramework=".NETFramework4.0" />
<group targetFramework=".NETFramework4.5" />
<group targetFramework=".NETFramework4.6" />
<group targetFramework=".NETFramework4.6.2" />
</dependencies>
</metadata>
<files>
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\netstandard1.3\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\netstandard2.0\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net40\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net45\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net46\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net462\<yourprojectassembly.dll>" />
<file src="<your-path>\myfiles\*" target="content\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\netstandard1.3\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\netstandard2.0\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net40\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net45\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net46\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net462\myfiles" />
</files>
</package>
一个引用了nuget包的项目的外观截图. (我的内容是一个文本文件,目标是"lib\<target>\any"
)
值得注意的是,虽然程序集中引用您的程序包的内容可能有一些行为方面的考虑.
It is worth noting that there may be some behavior considerations to the content in the assembly that references to your package though.
在上图中,来自nuget包的内容默认情况下称为无复制到输出",而C#编译称为BuildAction.
in the above image, the content that came from the nuget package is by default referenced as No Copy to Output and C# Compile as the BuildAction.
这篇关于仅用于内容文件的Nuget包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!