如何扁平化Nuget包的contentFiles? [英] How to flatten Nuget package contentFiles?

查看:109
本文介绍了如何扁平化Nuget包的contentFiles?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 < references><参考文件="ThisProject.dll"</参考<参考文件="First.dll"</参考<参考文件="Second.dll"</参考<参考文件=".....">/参考</references>< contentFiles><文件包括="any/any/*"buildAction =内容"copyToOutput ="true";flatten =真"/></contentFiles></元数据><文件>< 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)

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

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

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

您只需要添加两行:

 < contentFiles><文件包括="any/any/*"buildAction =内容"copyToOutput ="true";flatten =真"/></contentFiles><文件>< file src ="xxx \ First.dll(项目文件夹上dll的物理相对路径)"target ="contentFiles \ any \ any \ First.dll"></file>< file src ="xxx \ Second.dll(项目文件夹上dll的物理相对路径)"target ="contentFiles \ any \ any \ Second.dll"</file>< file src ="xxx \ .... dll(项目文件夹上dll的物理相对路径)"target =" contntFiles \ any \ any \ ..... dll"</file>< file src ="xxx \ First.dll(项目文件夹上dll的物理相对路径)"target =内容"</file>< file src ="xxx \ Second.dll(项目文件夹上dll的物理相对路径)"target =内容"</file>.....<文件> 

但是这些根本无法更改导入文件的属性.对于Net Framework项目,不能在 xxx.nuspec 文件上更改文件的属性.

应该使用

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

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

3) nuspec 文件上添加一行,以将props文件包含在nupkg中.

 < file src ="build \ xxx.props(项目文件夹上文件的物理相对路径)"target ="build"</file> 

4),然后重新打包nuget程序包,清理nuget缓存,然后使用 packages.config 在Net Framework项目上安装此新程序.

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

Nuget package issues - buildAction, copyToOutput, flatten are ignored

Package project (ThisProject.vbproj)- .Net Standard Library 2.0, .nuspec file:

<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>

When importing in .net ClickOnce Framework 4.6.1 Project, the contentFiles are still in the subfolders (flatten is ignored), Build Action and CopyToOutputDirectory are the defaults (buildAction,copyToOutput are ignored)

Read all the documentation I could find e.g.

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

What am I doing wrong?

解决方案

I think you have some misunderstanding about this part.

First, contentFiles works for new-sdk projects(Net Core and Net Standard) with PackageReference nuget management format rather than Net Framework project with packages.config nuget management format.

And contentFiles works for content files rather than lib folder. So you should not pack these dll files on target="lib\any\any\.....dll". You should pack them into contentFiles folder.

Use this:

<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>

Then, you should install this nuget package on a Net Core project.

When you finish it, repack the project with nuget pack command, then, before you install the new one, clean the nuget caches first to remove the old previous version. Then, install the new version on a Net Core project and you can see the effect like this:

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

If you still want to have this function on a Net Framework project, you should pack these files on content node rather than contentFiles.

And you only need to add two lines:

        <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>

But these simply cannot change the attributes of the imported file. And for net framework project, changing the property of the files cannot be done on xxx.nuspec file.

You should use <packages_id>.props or targets file.

1) create a file called <packages_id>.props under the build folder on the Solution Explorer, if your nuget package is named as ThisProject.1.0.0.nupkg, you should name it as ThisProject.props so that it will work.

This is mine:

2) add these on the props file:

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

</Project>

3) add a line on nuspec file to include the props file into the nupkg.

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

4) then repack the nuget package, clean the nuget caches, then install this new one on the Net Framework project with packages.config.

Note: although the Properties window of the imported content file on the solution explorer does not show the changed value and still shows the old one, the files are already copied into the output folder of the project. And it is an UI display issue on Solution Explorer and the changed values are already be used and work well. So you do not have to care much about that.

这篇关于如何扁平化Nuget包的contentFiles?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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