仅用于内容文件的 Nuget 包 [英] Nuget package for just content files

查看:29
本文介绍了仅用于内容文件的 Nuget 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 .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

我已将程序集中的文件添加到 contentfilesanyanymyfiles 下,并将以下内容添加到 nuspec 文件的元数据部分:

I have added the files in the assembly under contentfilesanyanymyfiles and added the following to the metadata section of the nuspec file:

<contentFiles>
    <files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>

当我尝试将其添加到 .NET 框架程序集时,出现错误

When I try to add this to a .NET framework assembly I get the error

无法安装包myPackage 1.0.0".您正在尝试将此包安装到以.NETFramework,Version=v4.6.2"为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件.如需更多信息,请联系软件包作者.

Could not install package 'myPackage 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

我还尝试将文件添加到项目的根目录,并在 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="contentFilesanyanymyFiles"/>
    </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>

我还建议将 .dll 文件与 nuspec 文件的文件部分中的内容文件显式分开,并参考 lib 约定..(这实际上与我在上面提到的推断情绪,但目前我没有太多细节可以提供)

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
etstandard1.3<yourprojectassembly.dll>" />
   <file src="<your-path><yourprojectassembly.dll>" target="lib
etstandard2.0<yourprojectassembly.dll>" />
   <file src="<your-path><yourprojectassembly.dll>" target="lib
et40<yourprojectassembly.dll>" />
   <file src="<your-path><yourprojectassembly.dll>" target="lib
et45<yourprojectassembly.dll>" />
   <file src="<your-path><yourprojectassembly.dll>" target="lib
et46<yourprojectassembly.dll>" />
   <file src="<your-path><yourprojectassembly.dll>" target="lib
et462<yourprojectassembly.dll>" />
   <file src="<your-path>myfiles*" target="contentmyfiles" />
   <file src="<your-path>myfiles*" target="contentFilesany
etstandard1.3myfiles" />
   <file src="<your-path>myfiles*" target="contentFilesany
etstandard2.0myfiles" />
   <file src="<your-path>myfiles*" target="contentFilesany
et40myfiles" />
   <file src="<your-path>myfiles*" target="contentFilesany
et45myfiles" />
   <file src="<your-path>myfiles*" target="contentFilesany
et46myfiles" />
   <file src="<your-path>myfiles*" target="contentFilesany
et462myfiles" />
</files>
</package>

<小时>

引用您的 nuget 包的项目的屏幕截图.(我的内容是一个文本文件,目标是"libany")

值得注意的是,可能对引用您的包的程序集中的内容有一些行为考虑.

It is worth noting that there may be some behavior considerations to the content in the assembly that references to your package though.

在上图中,来自 nuget 包的内容默认引用为 No Copy to Output,C# Compile 为 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 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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