在Nuget for PackageReferece项目中打包静态内容

问题描述 投票:4回答:1

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

我试过这个:NuGet ContentFiles Demystified但是我引用了我的js和css文件(它们没有被复制到我的项目内容中)。

在我的nuspec中,我尝试了所有构建选项:EmbeddedResource,Content,None和Compile,但这些引用总是在Compile模式下导入。所以当我开始调试时出现编译错误。

我知道这可以通过Package.config项目实现,而且非常简单但我的所有消费者项目都是PackageReference。

这是我的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>

我用这个powershell命令打包我的nuget:

nuget pack MyProject.nuspec

虽然我也试过csproj:

nuget pack MyProject.csproj

我的源文件夹结构是这样的:

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

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

nuget static-content nuspec
1个回答
2
投票

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

为了回答你之前的问题Packing files on nuget,我创建了一个示例nuget包,并为内容文件设置构建操作到content,在安装nuget包之后,构建操作将被设置为content

enter image description here

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

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

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

在安装nuget软件包时,NuGet将首先从本地缓存中提取nuget软件包,以避免下载计算机上已有的软件包。在其他病房中,尽管我们已在本地更新了nuget包,但nuget将首先检测本地缓存,如果它在缓存中找到相同的包,则nuget将从缓存而不是本地源安装它。

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

更新评论:

我已经尝试增加版本号并删除nuget缓存,问题仍然存在。我的构建操作始终设置为“C#Compiler”。我只是尝试更改js文件的名称,项目导入新名称,所以我不认为这是一个缓存问题

在测试了你的nuget包之后,我找到了你遇到这个问题的原因,我们应该保持srctarget路径在.nuspec文件中相同的路径。由于您希望将内容文件设置为wwwroot文件夹,因此您应该在wwwroot文件夹中设置该文件,然后打包.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

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

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

你可以下载并测试。

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

enter image description here希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.