1

Note - this is a follow-up question to How do I create a Nuget package with Visual Studio 2017

My CodeFirstWebFramework NuGet package on GitHub needs to install a specifically named folder of content files in any project which uses it. These files are javascript, css, html, image and template files to support the web server the package creates. They need to be in a folder called CodeFirstWebFramework, and need to be copied to the project output folder.

In the absence of specific documentation on how to do this, I have tried various experiments. I can create a folder called contentFiles, with the CodeFirstWebFramework folder inside it, and get the contentFiles folder copied to the using project OK (although, pending an as yet unreleased update to VS 2017, this is not automatically copied to the output folder).

I tried putting the files in a CodeFirstWebframework folder (i.e. removing contentFiles from the hierarchy), and setting ContentTargetFolders in the .csproj to .. This copied the files into the package OK, but they were not copied out into the using project.

 <ItemGroup>
    <Content Include="CodeFirstWebFramework/**/*.*" copyToOutput="true">
      <IncludeInPackage>true</IncludeInPackage>
        <CopyToOutput>true</CopyToOutput>
        <BuildAction>Content</BuildAction>
        <copyToOutput>true</copyToOutput>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        <PackageCopyToOutput>true</PackageCopyToOutput>
    </Content>
  </ItemGroup>

[Note there are some incorrect and unnecessary XML elements in the above, as part of my attempts to get this to do what I want - but this worked when the content was in a contentFiles folder]

I now realise I am not exactly being clear. My code is a DLL which runs a web server. It needs to be able to find certain js, html, etc. files and folders in a subfolder of the using project's output folder. It is currently expecting that subfolder to be called CodeFirstWebFramework, but I could change that.

It is usual for the project using the DLL to have another folder, or even series of folders, where the DLL will also search for these files (so the project author can provide versions more suitable to their needs).

I would like these folders to be consistently named so they relate to the project (i.e. I am currently calling the one I supply CodeFirstWebFramework, and the using project's one after the name of the using project).

If I use contentFiles/CodeFirstWebFramework for mine, the using project would have to use contentFiles/ProjectName for its ones - I imaging that would cause conflicts, wouldn't it?

Nikki Locke
  • 2,759
  • 6
  • 29
  • 53
  • Why don't you want the items in the `contentFiles` folder? NuGet expects the files to be in that folder – Martin Ullrich Jun 28 '17 at 12:00
  • When another package references the Nuget package, and then compiles, I want the files to appear in a folder called `CodeFirstWebFramework` in the project output folder (because that's where my code expects to find them). If I can achieve this some other way, that's fine. Any ideas how? – Nikki Locke Jun 28 '17 at 16:34

1 Answers1

2

This works by specifying the content files to not being flattened:

<Project>
  <PropertyGroup>
    <NuGetBuildTasksPackTargets>junk-value-to-avoid-conflicts</NuGetBuildTasksPackTargets>
  </PropertyGroup>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  <PropertyGroup>
    <TargetFramework>netstandard1.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Update="SomeExampleFolder\**\*" Pack="true" PackageCopyToOutput="true" PackageFlatten="false" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="NuGet.Build.Tasks.Pack" Version="4.3.0-preview3-4168" PrivateAssets="All" />
  </ItemGroup>
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

The workaround using the preview version of NuGet.Build.Tasks.Pack can be removed when using VS 2017 15.3 or a 2.0.0 .NET Core CLI.

When referenced, it produces the following output: enter image description here

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Thanks. Do I need all those Property and Item groups? My target is net 45 - does that make a difference? I notice in your image, there is a netcoreapp1. 1 folder - where does that come from? Is the csproj XML you give for the project that creates the nuget package? Is the folder structure image you show of the project that creates the nuget package, or the one that uses it? – Nikki Locke Jun 29 '17 at 14:38