Copy content files from Nuget package referenced by another Nuget package

Viewed 910

I want to get a file from a referenced Nuget package into my output folder. The packages are generated using Visual Studio 2017 on a .NET Standard project with the new csproj file format and 'GeneratePackageOnBuild' enabled.

For example, I have two Nuget packages (A and B) and package B depends on A. Package A includes a content file, like a config file. Next to these packages I have an application which includes package B. This application uses stuff from package A and B, and therefor needs the file from page A.

NugetPackageA
|- ClassA.cs
|- RequiredFile.txt

NugetPackageB
|-Dependencies
  |- Nuget
    |- NugetPackageA
|- ClassB.cs

Application
|-Dependencies
  |- Nuget
    |- NugetPackageB
|- Program.cs

What I did is enable the packing of the file in the project file (based on link):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="RequiredFile.txt">
      <Pack>true</Pack>
      <PackageCopyToOutput>true</PackageCopyToOutput>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <PackagePath>contentFiles\any\any\;content\any\any\</PackagePath>
    </Content>
  </ItemGroup>

</Project>

If I open the Nuget package and nuspec file I see that the files are correctly packed and the properties seems to be ok:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>NugetPackageA</id>
    ...
    <contentFiles>
      <files include="any/any/RequiredFile.txt" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>
</package>

When the Application directly depends on NugetPackageA the file is correctly added to the project and copied to the output directory.

So for now I see these workarounds;

  1. Adding the Nuget reference directly
  2. Make a script that tries to find the correct package and version in $(NuGetPackageFolders) and manually copy the files
  3. Change NugetPackageB to also includes RequiredFile.txt

Does anybody else have a solution for this?

Thanks.

0 Answers
Related