Set content files to “copy local : always” in a nuget package

Instead of using a PowerShell script another approach is to use an MSBuild targets or props file with the same name as the package id:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)importantfile.xml">
      <Link>importantfile.xml</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

In the nuspec file then, instead of adding the required files to the Content directory, add them to the Build directory along with the targets file.

  • Build
    • importantfile.xml
    • MyPackage.targets
  • lib
    • net45
      • MyAssembly.dll

If you require different content for different architectures then you can add architecture folders under Build also each with their own targets file.

Benefits to using a targets file over the PowerShell script with NuGet Content directory:

  • required content files aren’t shown in the project in Visual Studio
  • content files are linked to rather than copied into the directory of each project which references the NuGet package (preventing there being multiple copies and keeping behaviour the same as for assemblies / libraries from NuGet packages)
  • PowerShell scripts only work in Visual Studio and aren’t run when NuGet is run from the commandline (build servers, other IDEs and other OS), this approach will work everywhere
  • PowerShell install scripts are not supported in NuGet 3.x project.json system.

Leave a Comment