0

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 仍在子文件夹中(flatten 被忽略),Build Action 和 CopyToOutputDirectory 是默认值(buildAction、copyToOutput 被忽略)

阅读我能找到的所有文档,例如

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

我究竟做错了什么?

4

1 回答 1

0

我想你对这部分有一些误解。

首先contentFiles适用于具有PackageReference nuget 管理格式Net Core的 new-sdk 项目(和Net Standard),而不是具有packages.config nuget 管理格式的项目。Net Framework

并且contentFiles适用于内容文件而不是 lib 文件夹。所以你不应该将这些 dll 文件打包到target="lib\any\any\.....dll". 您应该将它们打包到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>

<files>

然后,您应该在Net Core项目上安装这个 nuget 包。

完成后,使用nuget pack命令重新打包项目,然后,在安装新项目之前,先清理 nuget 缓存以删除旧版本。然后,在一个Net Core项目上安装新版本,可以看到如下效果:

在此处输入图像描述

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

如果您仍想在Net Framework项目中使用此功能,则应将这些文件打包在contentnode 上而不是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>

但是这些根本无法改变导入文件的属性。而对于 net framework 项目,更改文件的属性不能在xxx.nuspec文件上完成。

应该使用<packages_id>.props 或目标文件。

1)在解决方案资源管理器<packages_id>.props的 build 文件夹下创建一个名为的文件,如果您的 nuget 包命名为,则应将其命名为 ,以便它可以工作。ThisProject.1.0.0.nupkgThisProject.props

这是我的:

在此处输入图像描述

2)在道具文件中添加这些:

<Project>
    <ItemGroup>
        <Content Include="First.dll">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="Second.dll">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        ......
            
    </ItemGroup>

</Project>

3)在文件中添加一行nuspec以将道具文件包含到nupkg中。

 <file src="build\xxx.props(the physical, relative path of the file on your project folder)" target="build"></file>

4)然后重新打包 nuget 包,清理 nuget 缓存,然后在 Net Framework 项目上安装这个新的packages.config.

注意:虽然解决方案资源管理器上导入的内容文件的属性窗口没有显示更改的值,并且仍然显示旧值,但文件已经复制到项目的输出文件夹中。这是解决方案资源管理器上的 UI 显示问题,更改的值已被使用并且运行良好。所以你不必太在意。

于 2020-11-09T09:15:14.740 回答