2
votes

I'm creating the package for my assembly but I dont' want to include the docs/*.xml files in my nuget package. I have tried the -Exclude switch of the pack command and the exclude property for the files section in the nuspec file to explictly excluding these files. None of these worked. So every time I generate my nuget package and then test it by installing it in the target project it always adds a docs folder with all the xml files. How can I avoid that the xml files are included into the nuget package? Any help would be highly appreciated.

3

3 Answers

1
votes

To exclude all .xml files you should use the **\*.xml wildcard. I am guessing that you are using *.xml which will not work.

To exclude all .xml files you could use a nuget command line similar to the following:

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

If you only need to exclude .xml files in the docs directory then you can use a nuget command line similar to the following:

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

The wildcards seem to work relative to the folder that your .nuspec file is in.So if you have an .xml file in a docs subfolder relative to the .nuspec file then a wildcard if docs*.xml should work too.

1
votes

Thank you Matt, I'm already doing what you mentioned but seems to me that Nuget does some other things by convention. Even if I use the exclude just like you said the docs folder is included. I resolved the issue by generating the nuspec file with the -a switch (I was using my .csproj file). I also had to copy the .dll file to a folder outside of my solution's folder. This way everything worked fine and as expected.

Anyway your answer is accurate but in my scenario it wasn't working. Not sure if this is by design. Here's my final msbuild file which I'm currently using to generate the package. Hopefully Nuget soon will add more switches to the spec command so we won't have to modify so much the nuspec file afterwards.

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

0
votes

Another thing I can think of is

  1. Create a nuspec file and edit it. this needs to be done only once (could be checked in as well). Any reasons you are editing the nuspec file while doing build?
  2. use files element in nuspec file to copy files to destination folders

<files> <file src="bin\Debug\*.dll" target="lib" /> <file src="bin\Debug\*.pdb" target="lib" /> <file src="tools\*\.*" exclude="*\.log" /> </files>

3. pack command can remain to be done at build time.

more details for files can be found here http://docs.nuget.org/docs/reference/nuspec-reference