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

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

问题描述

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

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.

我已经尝试过: NuGet ContentFiles Demystified ,但我明白了我引用的js和css文件(它们没有复制到我的项目内容中).

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

在我的研究中,我尝试了所有构建选项:EmbeddedResource,Content,None和Compile,但这些引用始终以Compile模式导入.因此,当我开始调试时会遇到编译错误.

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.

我知道Package.config项目可以做到这一点,这很简单,但是我所有的使用者项目都是PackageReference.

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

这是我的秘诀

<?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>

我用以下powershell命令打包我的nuget:

I pack my nuget with this powershell command:

nuget pack MyProject.nuspec

尽管我也尝试过使用csproj:

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?

推荐答案

安装忽略了我的构建操作.为什么总是尝试编译我的内容文件?有没有更好的方法将静态内容添加到使用者项目?

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?

要回答您以前的问题在nuget上打包文件,我创建了一个示例nuget包并将内容文件的构建操作设置为content,在安装该nuget软件包之后,构建操作将设置为content:

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:

然后我检查了您的.nuspec文件,发现它应该正确.因此,该问题与您的.nuspec文件无关.

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

此外,在上图中,您会注意到内容文件的路径是nuget本地缓存:

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软件包时,NuGet首先会从本地缓存中提取nuget软件包,以避免下载计算机上已经存在的软件包.在其他方面,尽管我们已在本地更新了nuget软件包,但nuget会首先检测本地缓存,如果它在缓存中找到了相同的软件包,nuget将从缓存而不是本地feed中安装它.

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.

要解决此问题,请在安装更新的nuget软件包之前尝试在本地缓存中删除nuget软件包.通常,当我们再次打包相同的软件包时,我们会d better change the package version in the .nuspec`.文件,因此nuget本地缓存将无法捕获它们.

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.

更新评论:

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

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

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

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>

以下是我的.nuspec脚本(不需要内容节点):

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>

这是nuget包:

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

您可以下载并进行测试.

You can download it and test.

然后将其安装到ASP.NET核心MVC项目:

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

希望这会有所帮助.

Hope this helps.

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

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