Packing static content in Nuget for PackageReferece projects
Asked Answered
B

1

8

I have a Class Library (net47) project and I'd like to pack into a nuget my dll and several files of static content (js, css, images...). I want to use this dll and the content from the consumer projects. These projects will be MVC PackageReference projects. In these projects the local static files are in the wwwroot folder.

I have tried this: NuGet ContentFiles Demystified but I get my js and css files referenced (they aren't copied to my project content).

In my nuspec I've tried with all build options: EmbeddedResource, Content, None and Compile, but these references are imported always in Compile mode. So I get a compile error when I start debugging.

I know this was possible with Package.config projects and it's very simple but all my consumer projects will be PackageReference.

This is my nuspec

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>MyProject</id>
    <version>1.0.0</version>
    <authors>My</authors>
    <owners>My</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>LVP</description>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
    <contentFiles>
        <files include="any/any/bd.js" buildAction="content" flatten="true" copyToOutput="false"/>
    </contentFiles>
  </metadata>
  <files>
    <file src="contentFiles/any/any/bd.js" target="contentFiles/any/any/bd.js" />
  </files>
</package>

I pack my nuget with this powershell command:

nuget pack MyProject.nuspec

Although I have also tried with the csproj:

nuget pack MyProject.csproj

And my source folder structure is this:

C:\...[projectPath]...\contentFiles\any\any\bd.js

Installation is ignoring my build action. Why is always trying to compile my content files? Is there a better way to add static content to the consumer project?

Blockbuster answered 12/2, 2018 at 16:24 Comment(0)
I
7

Installation is ignoring my build action. Why is always trying to compile my content files? Is there a better way to add static content to the consumer project?

To answer your previous question Packing files on nuget, I have created a sample nuget package and set the build action to content for the content files, after install that nuget package, the build action would be set content:

enter image description here

Then I checked your .nuspec file, found it should be correct. So the issue is not related to your .nuspec file.

Besides, in the above image, you will notice that the path of the content file is nuget local cache:

C:\Users\<UserName>\.nuget\packages\

NuGet will first extract the nuget package from the local cache when install the nuget package to avoid downloading packages that are already on the computer. In other wards, although we have updated the nuget package in the local, nuget will detect the local cache first, if it found the same package in the cache, nuget will install it from cache rather than local feed.

To resolve this issue, please try to remove your nuget package in the local cache before installing the updated nuget package. Generally, when we package the same package again, wed better change the package version in the.nuspec` file so nuget local cache will not catch them.

Update for comment:

I've tried increasing the version number and deleting the nuget cache and the problem persists. My build action is always set to "C# Compiler". I just tried changing the name of the js file and the project imports the new name so I do not think it's a cache problem

After test your nuget package, I found the reason why you get that issue, we should keep the path the src and target paths are the same in the .nuspec file. Since you want set content file to the wwwroot folder, you should set the file in the wwwroot folder, then pack the .nuspec:

<contentFiles>
  <files include="any/any/wwwroot/css/bd.css" buildAction="Content" copyToOutput="false" flatten="true" />
  <files include="any/any/wwwroot/js/bd.js" buildAction="Content" copyToOutput="false" flatten="true" />
</contentFiles>

enter image description here

enter image description here

Following in my .nuspec scripts(Not need content node):

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>BancaDigitalViewProvider</id>
    <version>1.0.37</version>
    <authors>Ibercaja</authors>
    <owners>Ibercaja</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Login View Provider</description>
    <copyright>Copyright 2018</copyright>
    <tags>Banca Digital View Provider</tags>
    <dependencies />
    <contentFiles>
      <files include="any/any/wwwroot/css/bd.css" buildAction="Content" copyToOutput="false" flatten="true" />
      <files include="any/any/wwwroot/js/bd.js" buildAction="Content" copyToOutput="false" flatten="true" />
    </contentFiles>
  </metadata>

  <files>
    <file src="contentFiles/any/any/wwwroot/css/bd.css" target="contentFiles/any/any/wwwroot/css/bd.css" />
    <file src="contentFiles/any/any/wwwroot/js/bd.js" target="contentFiles/any/any/wwwroot/js/bd.js" />
    <file src="bin\debug\BancaDigitalViewProvider.dll" target="lib\net47\BancaDigitalViewProvider.dll" />
  </files>
</package>

This is nuget package:

https://1drv.ms/u/s!Ai1sp_yvodHfhTk5xutPpaBZLC-A

You can download it and test.

Then install it to the ASP.NET core MVC project:

enter image description here Hope this helps.

Ion answered 13/2, 2018 at 3:17 Comment(11)
I've tried increasing the version number and deleting the nuget cache and the problem persists. My build action is always set to "C# Compiler". I just tried changing the name of the js file and the project imports the new name so I do not think it's a cache problem.Blockbuster
@Jesus, strange, it works fine on my side. Could you please share your nuget package to me by onedrive, so I could install it on my side to check if it still have this issue?Ion
@Jesus, thanks for your sharing. Found the reason for this issue, please check the updated answer for details.Ion
Thanks for your detailed explanation @Leo Liu-MSFT. Now I get Content on my Build Action and I can compile my project without problems. But when I try to use these files in one of my views I can't reach them. P.e., if I add this source to one of my views: <script src="~/css/bd.css"></script>. I get a 404 Not found error. How can I reference these files from my views? It's not posible to treat them like the others of my wwwroot folder?Blockbuster
@Jesus, How about add that file to the Views folder not wwwroot folder? This should be a another question, which is different from the previous question, and it seems more related to ASP.NET not NuGet, so I could not give you directly solution. You can open a new thread with ASP.NET tag for this issue. If above answer resolve your previous question, you can accept it as answer. This can be beneficial to other community members reading this thread.Ion
@LeoLiu-MSFT I tried this modifying to add some dlls... instead of adding dlls to a folder as static content, it added to the projects references. Is there a way to stop that? I dont want the dlls added to the references.Quarterback
I tried everything still i can't add content from nuget packages in projects configured to use PackageReferece Not core projects, normal .net 4.7.2 projectsDownswing
@L.Trabacchin, Since this thread is closed, please open a new thread with more details info.Ion
@Alpha75, did you solve the issue with accessing content files from package via HTTP? Can't find your question about this.Yod
@donRumatta, Finally I opted to use file providers: learn.microsoft.com/en-us/aspnet/core/fundamentals/…. So I can keep my resources into a package. You can view an example here: davepaquette.com/archive/2016/07/16/…Blockbuster
@Alpha75, thank you, I'll consider your approach. I also found out that we can move linked files to their necessary location via msbuild target before build.Yod

© 2022 - 2024 — McMap. All rights reserved.