nuspec 内容文件示例

nuspec contentFiles Example

昨天发布了 NuGet 3.3 (release notes) and there is a new contentFiles element supported (docs)。但是,我似乎无法正常工作。

我使用 NuGet.exe 作为构建过程。已更新至 v3.3。我也已将 Visual Studio 更新为 2015 Update 1 并重新启动。

这是我的 nuspec 文件 (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 运行 从命令行使用以下命令:

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

我得到以下信息:

MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild.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'.

我想我运行正在使用所有应该支持新的 contentFiles XML 元素的最新版本,但工具似乎不知道它。我错过了什么?我知道文件包含属性是垃圾,但是有人有使用新的 contentFiles 元素的完整示例 nuspec 文件吗?

根据 NuSpec reference,元素 <contentFiles> 必须在 <metadata> 内。所以它应该是这样的:

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

@Timothy,

你是对的。它是具有 metadata/contentFiles 元素以及 files 元素中的定义的组合。我有一个 C# 测试实用程序库,当使用 PackageReference 引用它时,它需要在项目 output/bin 文件夹中包含 PowerShell 文件。我将在下面为您添加 XML。我想你一定能从中得到你需要的东西。

特别说明: 请务必在路径中正确设置您的语言和框架值(即您的值类似于 cs/net45/YourFile.cs)。我正在使用 any/any/MyFile.psm1 因为我希望该文件被视为与语言和平台无关。如果我不这样做,我会在构建时遇到代码分析错误。

此外,将文件放在 'contentFiles' 目录中很重要。

(.nuspec 参考文档中定义的语言和框架选项) 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>

“/”在节点中很重要。如果使用它将不起作用:

<files include="any/any/x.dll" buildAction="None" copyToOutput="true" flatten="true" />

必须是:

<files include="any\any\x.dll" buildAction="None" copyToOutput="true" flatten="true" />

但它不适用于 .NET 框架??!