How do you copy a text file that has been included along with the DLLs within a NuGet package?
When I use my custom NuGet package in another solution (c:\dev\
for this example), the resulting file structure within c:\dev\package\projectId\lib\netstandard2.0\
has many DLLs and a text file, say file.txt
. The DLLs are all copied upon building the solution, but the text file is left behind.
Within the .nuspec
file, I originally included the file under <files><file src="foo\file.txt" target="lib\netstandard2.0"/></files>
. The file.txt
ends up in the packages folder when the NuGet package is restored, but it's not copied into the build directory.
Attempt 1: I tried using the contentFiles
property within the nuspec file, since the nuspec reference points there a few times. I got nuget.exe pack
command to work with this new property (i.e. no syntax errors), but there was no change in how the content (file.txt
) was handled.
Attempt 2: I tried using a projectId.targets
file. This uses a Target
that has an ItemGroup
that includes the file. Then, I tried using a Copy
event, pointing to the destination folder as $(OutputPath)
.
It seems awfully hard to copy a file that is included in the package to the build directory, having to dive into MSBuild events and the like.
I'm at a loss here, and any pointers would be welcome.
Edits # 1:
I tried adding this section to the metadata, per a suggestion below:
<contentFiles>
<files include="any\any\file.txt" buildAction="EmbeddedResource" />
</contentFiles>
This works in a small test case. The file.txt
shows up nicely in both Visual Studio and in the build directory. Weirdly, it doesn't work in my main project using the same exact syntax (I'm using .NET Core 2.0 in both). Also, in NuGet Package Explorer, it shows up in the package contents when it's alone. But when I add something under <files><file src="lib\netstandard2.0\test.dll" target="lib\netstandard2.0"/></files>
, it disappears from that view.
Edits # 2:
I think there's something else going on... Here is the .nuspec file from our main project. When I add a content file with the working suggestions below, it still doesn't show up (for either .NET Core 2.0 or .NET Framework 4.7.1). Is the .targets
file messing this up somehow?