21

I've been looking for any way to set the CopyToOutput attribute to true for content files I'm including in a NuGet package built in VS2017 from a .NET Standard Library project.

When adding the files using the Content node, I can see the files in the package, but when looking at the nuspec that is pulled out when it's cached locally, there is no CopyToOutput, so it's false by default. In this case, when it's referenced in an ASP.NET Core site, nothing is copied into the application. If I manually update that cached version to include the attribute and set it to true and restore, everything gets copied.

Unfortunately, I looked into the Nuget.Build.Tasks.Pack.dll and it looks like there's no way to pass that value through an MSBuild property.

Did anybody run into this issue and has a workaround?

Pang
  • 9,564
  • 146
  • 81
  • 122
Chris Biggs
  • 246
  • 2
  • 4
  • I'm struggling with the issue, but not finding a workaround. I hope to see an answer here shortly. – Jim Speaker May 19 '17 at 19:53
  • It seems that we're actually trying to do two different things. Apparently there is no support for content, which is what I'm trying to accomplish: http://stackoverflow.com/questions/41827597/include-content-files-in-nuget-package-using-project-json – Jim Speaker May 19 '17 at 22:41
  • I have looked at nuget's source code that generates the nuspec and it currently isn't possible without specifying custom NuSpec file, so I opened https://github.com/NuGet/Home/issues/5259 – Martin Ullrich May 20 '17 at 09:47

1 Answers1

32

See this pull request: Allow specifying copyToOutput and flatten as Metadata on Content items when packing sdk csproj #1450

You'll need to set PackageCopyToOutput to true in the source csproj for the content.

<Content Include="...">
    <PackageCopyToOutput>true</PackageCopyToOutput>
</Content>

and once build the package, it will include CopyToOutput="true" for that content.

Pang
  • 9,564
  • 146
  • 81
  • 122
Chuanbo
  • 319
  • 3
  • 7
  • 2
    Great help! But i'd like to know is there a way to copy the specific file to the build folder of the consuming project? – Farshan Sep 12 '18 at 12:03
  • 1
    I have searched stackoverflow and found many complex solutions but this simple solution really works! I wonder why it is not chosen as the correct answer? – Arman Nayyeri Dec 03 '19 at 07:57
  • But it uses 'Copy if newer' option - is there a solution to be able to set option 'Copy always'? – Piotr M. Oct 27 '20 at 10:05
  • This solution works great! But: @PiotrM.is right, when you check the content files properties in a consuming project it will show "Copy if newer" for the copy to output directory. I could not figure out why it is not set to "Copy always". – rekcul Jan 08 '21 at 14:32
  • 1
    What path should be given in Include "". I have files located in "C:\Users\xxxxx\.nuget\packages\xxxx\xxxxx\contentFiles\any\netstandard2.0\File\HoloLensSampleValues.xml". I want to give a generic path without username in path. (Ex: \contentFiles\any\netstandard2.0\File\HoloLensSampleValues.xml") – Rizwan May 26 '21 at 11:30