【问题标题】:How to avoid xml document generation when using nuget pack使用 nuget pack 时如何避免生成 xml 文档
【发布时间】:2012-11-29 18:00:43
【问题描述】:

我正在为我的程序集创建包,但我不想在我的 nuget 包中包含 docs/*.xml 文件。我已经尝试了 pack 命令的 -Exclude 开关和 nuspec 文件中 files 部分的 exclude 属性来明确排除这些文件。这些都不起作用。所以每次我生成我的 nuget 包,然后通过在目标项目中安装它来测试它时,它总是添加一个包含所有 xml 文件的 docs 文件夹。如何避免 xml 文件包含在 nuget 包中?任何帮助将不胜感激。

【问题讨论】:

    标签: nuget nuget-package


    【解决方案1】:

    要排除所有 .xml 文件,您应该使用 **\*.xml 通配符。我猜你使用的 *.xml 不起作用。

    要排除所有 .xml 文件,您可以使用类似于以下内容的 nuget 命令行:

    nuget.exe pack MyPackage.nuspec -Exclude **\*.xml
    

    如果您只需要排除 docs 目录中的 .xml 文件,则可以使用类似于以下的 nuget 命令行:

    nuget.exe package MyPackage.nuspec -Exclude **\docs\*.xml
    

    通配符似乎相对于您的 .nuspec 文件所在的文件夹有效。因此,如果您在相对于 .nuspec 文件的 docs 子文件夹中有一个 .xml 文件,那么通配符 if docs*.xml 也应该有效。

    【讨论】:

      【解决方案2】:

      谢谢你,Matt,我已经在做你提到的事情,但在我看来,Nuget 按照惯例做了一些其他的事情。即使我像您所说的那样使用排除,也包含 docs 文件夹。我通过使用 -a 开关生成 nuspec 文件解决了这个问题(我使用的是我的 .csproj 文件)。我还必须将 .dll 文件复制到解决方案文件夹之外的文件夹中。这样,一切都按预期正常工作。

      无论如何,您的答案是准确的,但在我的情况下它不起作用。不确定这是否是设计使然。 这是我目前用来生成包的最终 msbuild 文件。希望 Nuget 很快会在 spec 命令中添加更多开关,这样我们之后就不必修改太多 nuspec 文件了。

      <Project DefaultTargets="NugetPackage" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
      
      <PropertyGroup>
       <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
       <OutputPathCore>NugetPkgs\$(Configuration)\My.Assembly</OutputPathCore>
       <NuGetExePath>assets\nuget.exe</NuGetExePath>     
      

      <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
      
      <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>
      
      <Target Name="NugetPackage" DependsOnTargets="PackageClean;BuildNugetPackageMyAssembly">   
      

      <Target Name="PackageClean">  
      <RemoveDir Directories ="NugetPkgs\$(Configuration)" ContinueOnError ="true"/>  
      

      <Target Name="BuildNugetPackageMyAssembly">   
         <MakeDir Directories="$(OutputPathCore)" />
         <MakeDir Directories="$(OutputPathCore)\Package" />
         <MakeDir Directories="$(OutputPathCore)\lib\net40" />
         <MakeDir Directories="$(OutputPathCore)\lib\net20" />      
         <MakeDir Directories="$(OutputPathCore)\lib\net20-cf" />   
      
         <Copy
             DestinationFolder="$(OutputPathCore)\lib\net40" 
             SourceFiles="Source\My.Assembly\bin\$(Configuration)\My.Assembly.dll" />
      
          <Copy
             DestinationFolder="$(OutputPathCore)\lib\net20" 
             SourceFiles="VS2008\Source\My.Assembly\bin\$(Configuration)\My.Assembly.dll" />
      
          <Copy
             DestinationFolder="$(OutputPathCore)\lib\net20-cf" 
             SourceFiles="VS2008\Source\My.Assembly.CF\bin\$(Configuration)\My.Assembly.CF.dll" />
      
         <Copy DestinationFolder="$(OutputPathCore)\content" SourceFiles="CHANGES" />
      
         <Copy SourceFiles="Release Notes.txt" DestinationFiles="$(OutputPathCore)\Readme.txt" />
      
         <Exec Command="&quot;$(NuGetExePath)&quot; spec -a  &quot;$(OutputPathCore)\lib\net40\My.Assembly.dll&quot;" />              
      
          <XmlUpdate Namespace="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" XmlFileName="My.Assembly.nuspec" XPath="//package/metadata/licenseUrl" Value="http://someurl" />          
          <XmlUpdate Namespace="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" XmlFileName="My.Assembly.nuspec" XPath="//package/metadata/projectUrl" Value="http://someurl" />          
          <XmlUpdate Namespace="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" XmlFileName="My.Assembly.nuspec" XPath="//package/metadata/iconUrl" Value="http://somenice.png" />            
          <XmlUpdate Namespace="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" XmlFileName="My.Assembly.nuspec" XPath="//package/metadata/tags" Value="My.Assembly" />           
          <XmlUpdate Namespace="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" XmlFileName="My.Assembly.nuspec" XPath="//package/metadata/releaseNotes" Value="Review readme.txt for details." />  
      
      <ItemGroup>      
        <file Include="Source\My.Assembly\bin\$(Configuration)\My.Assembly.dll"/>
        <file Include="VS2008\Source\My.Assembly\bin\$(Configuration)\My.Assembly.dll"/>    
        <file Include="$(OutputPathCore)\Readme.txt"/>
      </ItemGroup>
      
      <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="RemoveElement" File="My.Assembly.nuspec" Element="dependencies" XPath="//package/metadata/dependencies" />
      
      <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement" File="My.Assembly.nuspec" Key="" Value="" Element="files" XPath="//package" InsertAfterXPath="//package" />
      <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement" File="My.Assembly.nuspec" Key="src" Value="%(file.Identity)" Element="file" XPath="//package/files" />
      
      <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddAttribute" File="My.Assembly.nuspec" XPath="//package/files/*[1]"  Key="target" Value="lib\net40" />
      <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddAttribute" File="My.Assembly.nuspec" XPath="//package/files/*[2]"  Key="target" Value="lib\net20" />  
      <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddAttribute" File="My.Assembly.nuspec" XPath="//package/files/*[3]"  Key="target" Value=""/>
      
      
      <Exec Command="&quot;$(NuGetExePath)&quot; pack My.Assembly.nuspec -OutputDirectory &quot;$(OutputPathCore)\Package&quot; -NoPackageAnalysis" />
      
      <Delete Files ="My.Assembly.nuspec" /> 
      

      【讨论】:

        【解决方案3】:

        我能想到的另一件事是

        1. 创建一个 nuspec 文件并对其进行编辑。这只需要完成一次(也可以签入)。您在构建时编辑 nuspec 文件的任何原因?
        2. 使用 nuspec 文件中的 files 元素将文件复制到目标文件夹

        文件>

        3.打包命令可以保留在构建时完成。

        文件的更多细节可以在这里找到http://docs.nuget.org/docs/reference/nuspec-reference

        【讨论】:

        • 感谢迪帕克的建议。但是我有不同版本的程序集,所以我想在构建每个版本之后制作 nuspec 文件。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多