Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy NuGet contentFiles transitively to referenced project

Tags:

I have a NuGet package in which is an extra file packaged as a Content in contentFiles folder.

Then I have two C# projects with SDK-style .csproj - A and B where Project B references Project A as a ProjectReference and there is a classic PackageReference of a NuGet package in the Project A like this:

NuGet package ← Project A ← Project B

My problem is that the extra file gets correctly copied to the build output of Project A, but it won't get copied to the output of Project B unless I do it manually.

Is there a way how to force copy the extra file from the NuGet dependency transitively to the Project B build output?

The only way I can think of is custom Post-build event with xcopy command but it's more of a workaround than a real solution.

Thanks for any advice in advance!

like image 898
Martin Vrábel Avatar asked Apr 10 '20 15:04

Martin Vrábel


1 Answers

If you are in control of the NuGet package

You can add additional build targets in which you are able to specify that the content files should be copied.

Add build/MyNuGetPackage.targets to the NuGet package:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>

    <!-- For all files in /contentFiles -->
    <Content Include="$(MSBuildThisFileDirectory)..\contentFiles\**\*.*">
      <!-- Copy to output directory on build -->
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>

  </ItemGroup>
</Project>

This will copy all content files of this package into the output folder, including cases where this package is transitively referenced.

like image 106
Bruno Zell Avatar answered Sep 22 '22 18:09

Bruno Zell