nuspec contentFiles示例

时间:2015-12-01 22:46:02

标签: nuget nuspec

昨天发布了NuGet 3.3(release notes)并且支持了一个新的contentFiles元素(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>

我使用以下命令从命令行运行:

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

我得到以下内容:

  

MSBuild自动检测:使用msbuild版本&#39; 14.0&#39;来自&#39; C:\ Program Files(x86)\ MSBuild \ 14.0 \ bin&#39;。试图从Hello.world.nuspec&#39;构建软件包。元素&#39;包&#39;在命名空间&#39; http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd&#39;有无效的子元素&#39; contentFiles&#39;在命名空间&#39; http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd&#39;。预期可能元素列表:&#39;文件&#39;在命名空间&#39; http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd&#39;。

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

4 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我有一个包含通用配置文件的软件包。这是用于企业NuGet存储库的。当我将包添加到项目中时,内容文件夹结构将添加到包文件夹中。但是,当我构建项目时,配置文件不会复制到输出文件夹中(例如“ \ Project \ bin \ Debug”)。

我尝试了以下目标:

  • target =“ content \ any \ any”
  • target =“ contentFiles \ any \ any”

此外,我都尝试过:

  • buildAction =“ content”
  • buildAction =“ none”

以下是相关的nuspec条目:

<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 :(得分:1)

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

<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框架吗?!