如何扁平化Nuget包的contentFiles?

问题描述

Nuget软件包问题-buildAction,copyToOutput,flatten被忽略

打包项目(ThisProject.vbproj)-.Net标准库2.0,.nuspec文件

<references>
      <reference file="ThisProject.dll"></reference>
      <reference file="First.dll"></reference>
      <reference file="Second.dll"></reference>
      <reference file="...."></reference>
    </references>
    <contentFiles>
      <files include="any/any/*" buildAction="Content" copyToOutput="true" flatten="true" />
    </contentFiles>
  </Metadata>
  <files>
    <file src="contentFiles\any\any\First.dll" target="lib\any\any\First.dll"></file>
    <file src="contentFiles\any\any\Second.dll" target="lib\any\any\Second.dll"></file>
   <file src="contentFiles\any\any\....dll" target="lib\any\any\.....dll"></file>
    </files>

在.net ClickOnce Framework 4.6.1项目中导入时,contentFiles仍在子文件夹中(忽略拼合),认值为Build Action和copyToOutputDirectory(忽略buildAction,copyToOutput)

阅读我能找到的所有文档

https://docs.microsoft.com/en-us/nuget/reference/nuspec

我在做什么错了?

解决方法

我认为您对此部分有误解。

第一 contentFiles 适用于使用PackageReference nuget management format而非{{1}的新SDK项目(Net CoreNet Standard) }}项目与packages.config nuget management format

Net Framework适用于内容文件而不是lib文件夹。因此,您不应将这些dll文件打包在contentFiles上。您应该将它们打包到target="lib\any\any\.....dll"文件夹中。

使用此:

contentFiles 

然后,您应该在 Net Core 项目上安装此nuget软件包。

完成后,请使用<contentFiles> <files include="any/any/*" buildAction="Content" copyToOutput="true" flatten="true" /> </contentFiles> <files> <file src="xxx\First.dll(the physical,relative path of the dll on your project folder)" target="contentFiles\any\any\First.dll"></file> <file src="xxx\Second.dll(the physical,relative path of the dll on your project folder)" target="contentFiles\any\any\Second.dll"></file> <file src="xxx\....dll(the physical,relative path of the dll on your project folder)" target="contntFiles\any\any\.....dll"></file> <files> 命令重新打包项目,然后在安装新项目之前,clean the nuget caches first删除旧的先前版本。然后,将新版本安装在 Net Core 项目上,您会看到如下效果:

enter image description here

================================================ ======================

如果您仍然希望在nuget pack项目中具有此功能,则应将这些文件打包在Net Framework节点上,而不是content

您只需要添加两行:

contentFiles

但是这些根本无法更改导入文件的属性。对于网络框架项目,无法在 <contentFiles> <files include="any/any/*" buildAction="Content" copyToOutput="true" flatten="true" /> </contentFiles> <files> <file src="xxx\First.dll(the physical,relative path of the dll on your project folder)" target="contentFiles\any\any\First.dll"></file> <file src="xxx\Second.dll(the physical,relative path of the dll on your project folder)" target="contentFiles\any\any\Second.dll"></file> <file src="xxx\....dll(the physical,relative path of the dll on your project folder)" target="contntFiles\any\any\.....dll"></file> <file src="xxx\First.dll(the physical,relative path of the dll on your project folder)" target="content"></file> <file src="xxx\Second.dll(the physical,relative path of the dll on your project folder)" target="content"></file> .......... <files> 文件上更改文件的属性。

应该使用<packages_id>.props or targets文件。

1),如果您的nuget包名为xxx.nuspec,则在 Solution Explorer 上的build文件夹下创建一个名为<packages_id>.props的文件,您应将其命名为ThisProject.1.0.0.nupkg,以便它可以正常工作。

这是我的:

enter image description here

2)将它们添加到props文件中:

ThisProject.props

3)<Project> <ItemGroup> <Content Include="First.dll"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="Second.dll"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> ...... </ItemGroup> </Project> 文件上添加一行,以将props文件包含在nupkg中。

nuspec

4),然后重新打包nuget包,清理nuget缓存,然后使用 <file src="build\xxx.props(the physical,relative path of the file on your project folder)" target="build"></file> 在Net Framework项目上安装这个新容器。

注意:尽管解决方案资源管理器上导入的内容文件的 Properties 窗口未显示更改后的值,但仍显示旧值,但文件已被复制到项目的输出文件夹中。这是 Solution Explorer 上的UI显示问题,更改后的值已被使用并且可以正常使用。因此,您不必对此太在意。