17

Please consider the following nuspec file:

<?xml version="1.0"?>
<package >
  [SOME METADATA]
  <files>
    <file src="bin\x64\$configuration$\GR*.filetype" target="content\" />
  </files>
</package>

The above has successfully packaged up the filetype files starting with 'GR' and has added them to my new, referencing, solution.

The problem is that I want these files to always be copied to the output directory. Can I do this via nuspec without having to manually amend the properties in my new solution?

  • 2
    Possible duplicate of [Set content files to "copy local : always" in a nuget package](https://stackoverflow.com/questions/21143817/set-content-files-to-copy-local-always-in-a-nuget-package) – Martin Ullrich Jun 25 '17 at 16:23

2 Answers2

26

How can I set the 'copy to output directory' property in my nuspec file?

Martin pointed out the right direction, I have same request before and kjbartel`s answer is nice to me. I post the answer here with more detail for you question, hope this can give you some help.

To resolve this question, you can follow below steps:

  1. Add a xx.targets file in your project folder, make sure the name of the target file is the same name as the package id(TestDemo is my package ID, so the name of .targets is TestDemo.targets).

  2. Add below code in the targets file:

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

Note: The path of "$(MSBuildThisFileDirectory)" should be relative path, if you are not familiar with it, you can use the absolute path.

  1. In the nuspec file, add required file to the Build directory along with the targets file.

      <files>
        <file src="bin\x64\Debug\GR*.txt" target="Build\" />
        <file src="TestDemo.targets" target="Build\" />
        <file src="bin\Debug\TestDemo.dll" target="lib\462" />
      </files>
    
  2. Pack this package, then add it on other project to test, it work fine.

Matt W
  • 11,753
  • 25
  • 118
  • 215
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 4
    I used the same approach today, and it works all right. But I find it very misleading that the included file afterwards reads “Do not copy” in the file properties in VS. If the user doesn’t know what is going on behind the scenes (and most probably he doesn’t!) he’ll be left very confused! – Martin Christiansen Jul 11 '18 at 19:59
  • 2
    Does anyone know if it is possible to use wildcards in the .targets file? – Mr Brown Nov 06 '18 at 07:58
  • how do you place a file in a subfolder of the target? I tried to add a folder under the build folder and the in the target file I did `Include="$(MSBuildThisFileDirectory)subfolder\RuleSupport.clp"` but it still placed the file in the root bin\debug folder – erotavlas Mar 29 '19 at 13:25
  • 1
    If anyone's looking for an answer to the questions from Mr Brown and erotavlas above, see [this answer](https://stackoverflow.com/a/30316946/339939) – Falconne Jul 18 '19 at 05:00
  • @erotavlas \SubFolder\File.txt – DavidTheDev Dec 06 '19 at 21:18
  • Don't forget to also add `` to the start of the `.targets` file. I know it's trivial, but I just copy-pasted the code from section 2, and it only failed at the step when installing the package... – nvirth Jun 10 '20 at 10:32
4

The accepted answer will be useful for the non-content files as it wont be linked to project when installed.

However, I had a requirement to have a settings xml file which will be added to the project and the nuget package user can edit it and package dll will load the edited xml file from output directory.

Since content files wont be copied to build directory, I had to use .targets file to copy content file to output directory.

nuspec file

<file src="TestDemo.targets" target="build"/>
<file src="Settings.xml" target="content/Configuration"/>

.targets file (file name has to be same as package id)

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="AfterBuild">
    <Copy SourceFiles="$(ProjectDir)Configuration\Settings.xml" DestinationFolder="$(TargetDir)Configuration\" ContinueOnError="true" />
  </Target>
</Project>
Kishor
  • 4,006
  • 7
  • 30
  • 47
  • Where does the tag go in nuspec file? Straight under the tag? Under the ? Also, I created the .targets file in my project folder. But when publishing it, I don't see the .targets file anywhere as part of the .nupkg zip file. Can someone please clarify? I'm trying to copy a simple README.txt file from nuget pkg to whatever project imports it. – G.A. Nov 24 '20 at 04:56