Terrible_ Theo
Terrible_ Theo

Reputation: 57

NuGet Package Packing - Is it possible to copy files to a custom directory?

I'm trying to package a few files into a NuGet package, but the issue is that all of the files are sent to the "content" folder within the NuGet package by default when packaged. Normally this is okay, but for the JSON files I have in "ABCJsons" I'd like them to be sent to "content/NewFolderName".

In my example below, the first block is my AbcToolTester, which has all of its project files files being successfully sent to the content directory in the NuGet package. The second block, is where I attempted to copy all the json files with ABCLibrary (ABCLibrary has subfolders where the actual Jsons are located) to the destination folder "ABCJsons". I thought this would do the trick, but unfortunately the ABCJson files just get sent to the content folder along with all the other files.

<ItemGroup>
    <Content Include="..\AbcToolTester\bin\Debug\netcoreapp3.1\**" Exclude="..\AbcToolTester\bin\Debug\netcoreapp3.1\*.pdb">
        <IncludeInPackage>true</IncludeInPackage>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
<ItemGroup>

<Target Name="CopyABCLibrary" AfterTargets="AfterBuild">
    <ItemGroup>
        <ABCJsonsInclude="..\..\tests\ABCLibrary\**\*.*"/>
    </ItemGroup>
    <Copy SourceFiles="@(ABCJsons)" DestinationFolder="$(TargetDir)\ABCJsons" SkipUnchangedFiles="true" />
</Target>

Upvotes: 0

Views: 760

Answers (1)

Hank
Hank

Reputation: 2466

It's hard to tell if the NuGet package you are creating is actually dependent on AbcToolTester project because there are easier ways to package that. That's another question though.

For your actual issue, you can simplify the copying process while also telling it where to pack the files. Replace your CopyABCLibrary target with this:

<ItemGroup>
  <Content Include="..\..\tests\ABCLibrary\**\*.*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <Pack>true</Pack>
    <PackagePath>ABCJsons\%(RecursiveDir)</PackagePath>
    <!-- This line hides the items from showing in the solution explorer -->
    <Visible>false</Visible>
  </Content>
</ItemGroup>

This will put all those files into the root of the nuget package into the ABCJsons folder and preserve the directory structure. Change the path accordingly to put it somewhere else.

Upvotes: 2

Related Questions