在 Nuget 中为 PackageReferece 项目打包静态内容 [英] Packing static content in Nuget for PackageReferece projects

查看:16
本文介绍了在 Nuget 中为 PackageReferece 项目打包静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库 (net47) 项目,我想将我的 dll 和几个静态内容文件(js、css、图像...)打包到一个 nuget 中.我想使用这个 dll 和来自消费者项目的内容.这些项目将是 MVC PackageReference 项目.在这些项目中,本地静态文件位于 wwwroot 文件夹中.

我试过这个:

然后我检查了您的 .nuspec 文件,发现它应该是正确的.所以这个问题与你的 .nuspec 文件无关.

另外,在上图中,你会注意到内容文件的路径是nuget local cache:

C:Users<用户名>.nugetpackages

安装 nuget 包时,NuGet 会先从本地缓存中提取 nuget 包,避免下载计算机上已有的包.换句话说,虽然我们更新了本地的nuget包,但nuget会先检测本地缓存,如果在缓存中找到相同的包,nuget会从缓存而不是本地feed安装.

要解决此问题,请尝试在安装更新后的 nuget 包之前,在本地缓存中删除您的 nuget 包. 通常,当我们再次打包相同的包时,我们最好更改.nuspec` 文件中的包版本,因此 nuget 本地缓存不会捕获它们.

评论更新:

<块引用>

我已尝试增加版本号并删除 nuget 缓存,但问题仍然存在.我的构建操作始终设置为C# 编译器".我只是尝试更改js文件的名称并且项目导入了新名称所以我认为这不是缓存问题

在测试你的 nuget 包后,我找到了你遇到这个问题的原因,我们应该保持 srctarget 路径相同.nuspec 文件.由于要将内容文件设置到 wwwroot 文件夹,因此应该将文件设置在 wwwroot 文件夹中,然后打包 .nuspec:

<contentFiles><文件包括="any/any/wwwroot/css/bd.css" buildAction="Content" copyToOutput="false" flatten="true"/><文件包括="any/any/wwwroot/js/bd.js" buildAction="Content" copyToOutput="false" flatten="true"/></内容文件>

按照我的 .nuspec 脚本(不需要内容节点):

<?xml version="1.0" encoding="utf-8"?><package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"><元数据><id>BancaDigitalViewProvider</id><版本>1.0.37</版本><作者>Ibercaja</作者><owners>Ibercaja</owners><requireLicenseAcceptance>false</requireLicenseAcceptance><description>登录视图提供程序</description><版权>版权 2018</版权><tags>Banca Digital View Provider</tags><依赖关系/><内容文件><文件包括="any/any/wwwroot/css/bd.css" buildAction="Content" copyToOutput="false" flatten="true"/><文件包括="any/any/wwwroot/js/bd.js" buildAction="Content" copyToOutput="false" flatten="true"/></内容文件></元数据><文件><文件 src="contentFiles/any/any/wwwroot/css/bd.css" target="contentFiles/any/any/wwwroot/css/bd.css"/><文件 src="contentFiles/any/any/wwwroot/js/bd.js" target="contentFiles/any/any/wwwroot/js/bd.js"/><文件 src="bindebugBancaDigitalViewProvider.dll" 目标="lib
et47BancaDigitalViewProvider.dll"/></文件></包>

这是 nuget 包:

希望这会有所帮助.

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]...contentFilesanyanyd.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?

解决方案

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:

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>.nugetpackages

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>

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="bindebugBancaDigitalViewProvider.dll" target="lib
et47BancaDigitalViewProvider.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:

Hope this helps.

这篇关于在 Nuget 中为 PackageReferece 项目打包静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆