Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget Content Files in .Net core solution not getting copied when installing through Nuget

I'm not able to copy the content static files in my .Net core web application project that I added when creating the nuget package using nuget package explorer. Same thing get copied correctly in .Net framework project template but not in .net core template. I'm using VS 2015 update 3.Am i Missing something here? Any help would be greatly appreciated.

Below is my snapshot of content file structure.

enter image description here

like image 707
Joshua I Avatar asked Nov 07 '16 15:11

Joshua I


2 Answers

It seems it is still not supported. Only way to "hack" it is with MSBuild Targets and Build events.

According to this documentation:

build
MSBuild .targets and .props files Automatically inserted into the project file or project.lock.json (NuGet 3.x+).

So to make it work with any file, e.g.: "config.xml" as Nuget Static Content:

  1. Create your XY.nuspec file (as you would normally)
  2. Include config.xml into the Nuget: <file src="config.xml" target="contentFiles\any\any\config.xml" />
  3. Add a new .targets file XY.targets
  4. Include new XY.targets file into your package to the "build" folder. <file src="XY.targets" target="build"/>

Content of the XY.targets file

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ContentFilesPath>$(MSBuildThisFileDirectory)..\contentFiles\any\any\</ContentFilesPath>
  </PropertyGroup>
  <Target Name="CopyConfigs" BeforeTargets="PreBuildEvent">
    <Copy SourceFiles="$(ContentFilesPath)\config.xml" DestinationFiles="$(ProjectDir)config.xml" SkipUnchangedFiles="true" Condition="!Exists('$(ProjectDir)config.xml')"></Copy>
  </Target>
</Project>

After packaging and installing the Nuget this MSBuild Target will be part of the Cached package and will run on each build (currently before build).

Issues with this solution:

  • Added files still linked until you build your solution. During the build paheses (BeforeTargets="") files are copied. Until this files still are just linked!!!
  • If you set up your content files to have Build actions and be copied to the Output directory of the project those settings will be lost!

Unfortunately this is the best solution for now.

like image 52
Major Avatar answered Sep 23 '22 04:09

Major


There is a nuget blog post about this, and it just isn't supported at this time.

Supported Project Types

This feature is only for packages that will be installed to projects that are managed using a project.json file. Currently only two projects types are managed by a project.json.

  • UWP apps
  • Portable class libraries

The contentFiles option is not available for other project types.

It's really a pity this basic functionality has been excluded from the .net Core projects. Especially because PCL is supported, which is a subset of a .net Core project.

There are quite some issues on GitHub about this, and it's very unclear whether or not this feature is coming back any time soon.

like image 25
Joep Beusenberg Avatar answered Sep 23 '22 04:09

Joep Beusenberg