【问题标题】:Nuget package for just content files仅用于内容文件的 Nuget 包
【发布时间】:2019-09-26 15:45:34
【问题描述】:

我正在尝试创建一个 .NET 标准 nuget 内容文件包(没有托管程序集),以供 .NET Framework 程序集使用。我的目标是将这个文件夹中的文件复制到消费程序集的 bin 文件夹中。

这个问题与Add native files from NuGet package to project output directory 类似(并使用此nuget 包http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/),但我要求我的nuget 包为.NET Standard

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

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

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

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

我还尝试将文件添加到项目的根目录,并在 nuspec 文件的元数据元素下方添加以下内容:

<files>
    <file src="myfiles\**\*" target="myfiles" />
</files>

完整的 nuspec 文件:

<?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 元素更改为:

<TargetFrameworks>netstandard1.3;netstandard2.0;net40;net45;net46</TargetFrameworks>

解决方案:

最后,这是解决方案:“myFiles”是位于项目根目录中的文件夹,包含许多不同的文件和子目录。

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 文件夹。

【问题讨论】:

  • 我最初认为构建操作应该类似于“嵌入式资源”或“内容”,而不是“无”。我将关闭 Visual Studio 中可用的选项。也许最好将其作为“Resource1.resx”文件,并将文件链接到该文件?
  • 没有。 @DanRayson 它不是那样工作的。这些类型的项目有效地通过目标框架构建多个 dll。相反,这里提出的问题是关于项目未定位net462
  • 另外,在 csproj 中,我还将 TargetFramework 更改为 TargetFrameworks "netstandard1.3;netstandard2.0;net40;net45;net46"
  • 必须是net462

标签: c# .net nuget .net-standard


【解决方案1】:

有一些推断的方法可以做到这一点,但是 add 可以在此处添加 targetFramework 作为依赖项的设置。

<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\&lt;targetName&gt;\ 约定。(这实际上与我在上面提到的推断情绪有关,但我目前没有太多细节可提供)

所以你的 nuspec 文件看起来像这样:

<?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\&lt;target&gt;\any"


值得注意的是,程序集中引用您的包的内容可能存在一些行为注意事项。

在上图中,来自 nuget 包的内容默认引用为 No Copy to Output 和 C# Compile 作为 BuildAction。

【讨论】:

  • 并不是我的截图不反映代码示例(基于问题示例);另外,在我的 csproj 中,我确实将 BuildAction 作为我的内容文件的“内容”,这反映在 nuspec 文件中,但我认为这实际上更多是构建考虑
  • 因此在 nuspec 中添加依赖项修复了添加到 .NET 框架项目时出现的错误。我可以在“myfiles”下获取包中包含的文件,但它们不包含在构建输出目录中
  • @SturmUndDrang 在你说你会试试这个之后我进行了一两次编辑,你能确认你使用了target="content\myfiles"target="contentFiles\any\net462\myfiles",如果是这样.. 尝试将buildAction 更改为@ 987654332@ 并删除 copyToOutput="true"。然后打包、推送和更新 nuget 包引用。
  • @SturmUndDrang 等待..“但它们不包含在构建输出目录中”..您的意思是在您对引用您的包的项目进行构建之后?我认为这与我上面提到的“行为考虑”有关。 Build Action 应该更改为“Content”.. 在这一点上.. 我不知道如何,或者如果你可以,在包中规定该行为,这似乎有点超出了问题和问题的范围.也许应该是一个单独的 SO 问题。
  • 是的 - 我想将文件包含在引用我的包的项目的输出文件夹中。我无法在该项目中设置构建操作,因为项目中没有可以执行此操作的文件。我可以添加一个构建后步骤来从 packages\myPackage\etc 复制文件,但这似乎不是一个非常干净的解决方案(但如果这是我可以复制文件的唯一方法,我会这样做)
猜你喜欢
  • 2015-08-12
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多