Search code examples
msbuildpackagenugettarget

Nuget targets, how to send a renamed content file to solution output folder?


I have created a Nuget package with a default json configuration file, 'config.json.pp'. On installation it is transformed and added as content; 'config.json'. I have also added a targets file in the build folder of the package, it renames the config file based on the assembly root namespace the package is installed in:

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

  <ItemGroup>
    <ConfigFile Include="config.json"/>
  </ItemGroup>

  <Target Name="CopyConfigFile" AfterTargets="Build;Rebuild">
    <Copy SourceFiles="@(ConfigFile)" DestinationFiles ="@(ConfigFile->'$(OutDir)\$(RootNameSpace)%(Extension)')" />
  </Target>

</Project>

When the assembly is build in a solution with more assemblies, it is not added to the output directory, only to the bin\$(configuration) folder of the assembly where the package is installed.

How can I add the file to the output directory of the main application of the solution?


Solution

  • You can use the link metadata to do this:

    <ItemGroup>
      <Content Include="config.json"
               Link="$(RootNameSpace)%(Extension)"
               CopyToOutputDirectory="PreserveNewest"
               Visible="False"
               Condition="Exists('config.json')" />
    </ItemGroup>