0

I have created npm package which has js and css files just similar to bootstrap as folder structure. I want to ship same package for .Net mvc web applications so I created .nuspec file specifying files from build output and created Nuget package. Both the Nuget and NPM package working great.

Now I want to publish same package for dot net core project. When I install same Nuget package in dot net core web application it installed successfully but does not copied static files to project folders.

How to create/fix nugget package of static files for dot net core application. I don't want to create .net core project to ship static files. It would be great if I could add some configuration file like .nuspec for dot net core application as well.

I have searched but not able to get any help in regards, So any suggestion or reference would be appriciated.

myproject.nuspec

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyPackage</id>
    <version>1.0.1</version>
    <title>MyProject</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <projectUrl>some url...</projectUrl>   
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>This is similar to bootstrap</description>
    <copyright>Copyright 2020</copyright>
    <tags></tags>
    <dependencies>
      <dependency id="jQuery" version="[3.0.0, 4.0.0)" />
    </dependencies>
  </metadata>
  <files>
    <file src="dist\css\**\*.*" target="content\Content\css" />
    <file src="dist\fonts\**\*.*" target="content\Content\fonts" />
    <file src="dist\js\mypackage.js" target="content\Scripts" />
    <file src="dist\images\**\*.*" target="content\Content\Images" />
  </files>
</package> 

Update : I tried solution given below by @thatguy it does copied the files in appropriate folders. I can see those in Visual Studio. But that newly created files and folder has arrow symbol on it while other files does not. I tried including css in page code but it does not found the newly created files.

What this arrow means and why its not finding the files ?

enter image description here

Dipak Telangre
  • 1,406
  • 4
  • 16
  • 40
  • Sorry, it seems that `contentFiles` in .NET Core does not support this scenario the way that `content` did before, see these related posts: [1](https://stackoverflow.com/questions/48994668/nuget-package-contentfiles-artifacts-installed-as-links-in-asp-net-core-mvc-proj), [2](https://stackoverflow.com/questions/55287761/net-core-nuget-package-copy-files-to-package-consumer-wwwroot?noredirect=1&lq=1) – thatguy Aug 21 '20 at 09:48
  • Also, you should keep the name of `.props` the same as the `package_id`. – Mr Qian Aug 24 '20 at 09:19
  • Hi Dipak, any update about this issue? Could you please tell us any progress about this issue? And we could give you any support if you want. – Mr Qian Aug 25 '20 at 02:43
  • @PerryQian-MSFT It worked as expected. Just a issue is it create files in .NET MVC project as well in .Net Core. Is there any way to condition it to only .NET core project ? – Dipak Telangre Aug 26 '20 at 04:48
  • If you want copy these files only in `net core` project, you should abandon using `content` node in `nupkg`.It will automatically copy files into the NET Framework main project when you install the package – Mr Qian Aug 26 '20 at 05:51
  • I have updated my answer. See `update 1`. – Mr Qian Aug 26 '20 at 06:05
  • @DipakTelangre If my answer helps you handle the issue, please do not forget to mark it. – Mr Qian Aug 26 '20 at 10:02
  • @PerryQian-MSFT Thanks !! – Dipak Telangre Aug 27 '20 at 05:29

1 Answers1

3

Create Nuget package for dot net core project from static files

You should use <package_id>.props file.

1) create a folder in your MyPackage called build and then add a file called MyPackage.props file in it.

enter image description here

2) Then add these in it:

<Project>
  <Target Name="CopyFilesToProject" BeforeTargets="Build">
    <Message Text="Copy css files to project" />
    <ItemGroup>
      <SourceScripts Include="$(MSBuildThisFileDirectory)..\..\content\**\*.* "/> //file from the nuget package
    </ItemGroup>
    <Copy
       SourceFiles="@(SourceScripts)"
       DestinationFiles="@(SourceScripts -> '$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)')"   
         />
  </Target>
  
</Project>

3) change to use this nusepc file:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyPackage</id>
    <version>1.0.1</version>
    <title>MyProject</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <projectUrl>some url...</projectUrl>   
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>This is similar to bootstrap</description>
    <copyright>Copyright 2020</copyright>
    <tags></tags>
    <dependencies>
      <dependency id="jQuery" version="[3.0.0, 4.0.0)" />
    </dependencies>
  </metadata>
  <files>
    <file src="dist\css\**\*.*" target="content\Content\css" />
    <file src="dist\fonts\**\*.*" target="content\Content\fonts" />
    <file src="dist\js\mypackage.js" target="content\Scripts" />
    <file src="dist\images\**\*.*" target="content\Content\Images" />
    <file src="build\MyPackage.props" target="build" />
  </files>
</package> 

4) repack your project MyPackage and then first uninstall the old nuget package MyPackage first in your main project.

Then, clean nuget caches first or delete all caches under C:\Users\xxx(current user)\.nuget\packages.

After that, install the new version MyPackage and then build your project first and you can see the files be copied under the main project.

In addition, there is a similar issue about your request and also this one.

==================================

Update 1

If you want these files only be copied on Net Core projects, you should abandon using content node in nupkg. It will automatically copy files into the NET Framework main project when you install the package.

Instead, you could put these files under a different folder called resource of the nupkg.

You could follow my steps:

1) change MyPackage.props file to:

<Project>
  <Target Name="CopyFilesToProject" BeforeTargets="Build">
    <Message Text="Copy css files to project" />
    <ItemGroup>
      <SourceScripts Include="$(MSBuildThisFileDirectory)..\..\resource\**\*.* "/> //file from the nuget package
    </ItemGroup>
    <Copy
       SourceFiles="@(SourceScripts)"
       DestinationFiles="@(SourceScripts -> '$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)')"   
         />
  </Target>

</Project>

2) change xxx.nuspec file to:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyPackage</id>
    <version>1.0.1</version>
    <title>MyProject</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <projectUrl>some url...</projectUrl>   
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>This is similar to bootstrap</description>
    <copyright>Copyright 2020</copyright>
    <tags></tags>
    <dependencies>
      <dependency id="jQuery" version="[3.0.0, 4.0.0)" />
    </dependencies>
  </metadata>
  <files>
    <file src="dist\css\**\*.*" target="resource\Content\css" />
    <file src="dist\fonts\**\*.*" target="resource\Content\fonts" />
    <file src="dist\js\mypackage.js" target="resource\Scripts" />
    <file src="dist\images\**\*.*" target="resource\Content\Images" />
    <file src="build\MyPackage.props" target="build" />
  </files>
</package> 

3) then repack your project and install the new one, before it, you should clean nuget caches first.

Mr Qian
  • 17,533
  • 1
  • 15
  • 28