nuspec contentFiles Example

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:09:45

问题


Yesterday NuGet 3.3 was released (release notes) and there is a new contentFiles element supported (docs). However, I can't seem to get this working.

I'm using the NuGet.exe as the build process. It is updated to v3.3. I have also updated my Visual Studio to 2015 Update 1 and rebooted.

Here is my nuspec file (Hello.world.nuspec):

<?xml version="1.0" encoding="utf-8"?>
<package>
    <metadata minClientVersion="3.3">
        <id>Hello.world</id>
        <version>1.0.0</version>
        <title>Greeting library</title>
        <authors>Timothy Klenke</authors>
        <description>Greetings for the world</description>
    </metadata>
    <contentFiles>
        <files include="*" />
    </contentFiles> 
</package>

I run from the command line using the following:

nuget.exe update -Self
nuget.exe pack Hello.world.nuspec

And I get the following:

MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'. Attempting to build package from 'Hello.world.nuspec'. The element 'package' in namespace 'http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd' has invalid child element 'contentFiles' in namespace 'http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd'. List of possible elements expected: 'files' in namespace 'http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd'.

I think I am running all the latest versions that should support the new contentFiles XML element, but the tools don't seem to know about it. What am I missing? I know the files include attribute is garbage, but does someone have a full example nuspec file using the new contentFiles element?


回答1:


Element <contentFiles> has to be inside <metadata> according to NuSpec reference. So it should look like this:

<?xml version="1.0" encoding="utf-8"?>
<package>
    <metadata minClientVersion="3.3">
        <id>Hello.world</id>
        <version>1.0.0</version>
        <title>Greeting library</title>
        <authors>Timothy Klenke</authors>
        <description>Greetings for the world</description>
        <contentFiles>
            <files include="*" />
        </contentFiles> 
    </metadata>
</package>



回答2:


I have a package that includes a common config file. This is for an enterprise NuGet repository. When I add the package to a project, the content folder structure is added to the package folder. However, when I build the project, the config file is not copied to the output folder (e.g., "\Project\bin\Debug").

I have tried the following targets:

  • target="content\any\any"
  • target="contentFiles\any\any"

Also, I have tried both:

  • buildAction="content"
  • buildAction="none"

Here is the relevant nuspec entries:

<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>Utilities</id>
    ...
    <contentFiles>
      <files include="any/any/CommonAppConfig.config" buildAction="content" copyToOutput="true" flatten="true" />
    </contentFiles>
  </metadata>
  <files>
    <file src="Utilities.dll" target="lib\net452\Utilities.dll" />
    <file src="Utilities.pdb" target="lib\net452\Utilities.pdb" />
    <file src="CommonAppConfig.config" target="content\any\any" />
  </files>
</package>



回答3:


@Timothy,

You were right. It is a combination having a metadata/contentFiles element as well as a definition in the files element. I have a C# test utility library that needs to include PowerShell files in a projects output/bin folder when it is referenced using a PackageReference. I will include the XML below for you. I think you will be able to derive what you need from it.

Special Note: Be sure to get your language and framework values right in the path (i.e. yours will something like cs/net45/YourFile.cs). I'm using any/any/MyFile.psm1 because I want the file to be treated as language and platform agnostic. If I don't, I get code analysis errors on build.

Additionally, placing the files in a 'contentFiles' directory is important.

(language and framework options defined in the .nuspec reference documentation) https://docs.microsoft.com/en-us/nuget/reference/nuspec#including-content-files

<package>
  <metadata>
    <id>SomeLibrary</id>
    <version>$version$</version>
    <title>SomeLibrary</title>
    <authors>Somebody</authors>
    <owners>Somebody</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Some library that does things I enjoy.</description>
    <releaseNotes />
    <copyright>Copyright 2017</copyright>
    <tags>PowerShell Testing</tags>
    <contentFiles>
      <files include="any/any/SomePS.psd1" buildAction="none" copyToOutput="true" />
      <files include="any/any/SomePS.psm1" buildAction="none" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>
    <file src="*.dll" target="lib\net461" />
    <file src="*.pdb" target="lib\net461" />
    <file src="SomePS.psd1" target="contentFiles\any\any" />
    <file src="SomePS.psm1" target="contentFiles\any\any" />
  </files>
</package>


来源:https://stackoverflow.com/questions/34031650/nuspec-contentfiles-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!