从静态文件为 dot net core 项目创建 Nuget 包 [英] Create Nuget package for dot net core project from static files

查看:40
本文介绍了从静态文件为 dot net core 项目创建 Nuget 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 npm 包,其中包含类似于 bootstrap 的 js 和 css 文件作为文件夹结构.我想为 .Net mvc web 应用程序提供相同的包,所以我创建了 .nuspec 文件,从构建输出指定 files 并创建了 Nuget 包.Nuget 和 NPM 包都运行良好.

现在我想为 dot net core 项目发布相同的包.当我在 dot net core web 应用程序中安装相同的 Nuget 包时,它安装成功但没有将静态文件复制到项目文件夹.

如何为 dot net core 应用程序创建/修复静态文件的 nugget 包.我不想创建 .net 核心项目来传送静态文件.如果我也可以为 dot net core 应用程序添加一些配置文件,比如 .nuspec,那就太好了.

我已经搜索过但无法获得任何帮助,因此任何建议或参考都将被采纳.

myproject.nuspec

<包><元数据><id>我的包裹</id><version>1.0.1</version><title>MyProject</title><authors>Me</authors><owners>Me</owners><projectUrl>一些网址...</projectUrl><requireLicenseAcceptance>false</requireLicenseAcceptance><description>这类似于 bootstrap</description><版权>版权 2020</copyright><标签></标签><依赖项><dependency id="jQuery"版本=[3.0.0, 4.0.0)";/></依赖项></元数据><文件><file src=distcss***.*";目标=内容内容css";/><file src=distfonts***.*";目标=内容内容字体";/><file src="distjsmypackage.js";目标=内容脚本"/><file src=distimages***.*";目标=内容内容图像"/></文件></包>

更新:我尝试了@thatguy 下面给出的解决方案,它确实将文件复制到了适当的文件夹中.我可以在 Visual Studio 中看到这些.但是新创建的文件和文件夹上有箭头符号,而其他文件没有.我尝试在页面代码中包含 css,但没有找到新创建的文件.

这个箭头是什么意思,为什么找不到文件?

解决方案

从静态文件为 dot net core 项目创建 Nuget 包

您应该使用

2) 然后在其中添加这些:

<目标名称=CopyFilesToProject"BeforeTargets=构建"><Message Text="复制 css 文件到项目";/><项目组><SourceScripts Include="$(MSBuildThisFileDirectory)....content***.* "/>//来自nuget包的文件</项目组><复制SourceFiles="@(SourceScripts)";DestinationFiles=@(SourceScripts -> '$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)')";/></目标></项目>

3) 更改为使用此 nusepc 文件:

<包><元数据><id>我的包裹</id><version>1.0.1</version><title>MyProject</title><authors>Me</authors><owners>Me</owners><projectUrl>一些网址...</projectUrl><requireLicenseAcceptance>false</requireLicenseAcceptance><description>这类似于 bootstrap</description><版权>版权 2020</copyright><标签></标签><依赖项><dependency id="jQuery"版本=[3.0.0, 4.0.0)";/></依赖项></元数据><文件><file src=distcss***.*";目标=内容内容css";/><file src=distfonts***.*";目标=内容内容字体";/><file src="distjsmypackage.js";目标=内容脚本"/><file src=distimages***.*";目标=内容内容图像"/><file src="buildMyPackage.props";目标=构建"/></文件></包>

4) 重新打包您的项目 MyPackage,然后首先卸载主项目中的旧 nuget 包 MyPackage.

然后首先清理 nuget 缓存 或删除 C:Usersxxx(current user).nugetpackages 下的所有缓存.

之后,安装新版本MyPackage,然后build你的项目,你可以看到文件被复制到主项目下.

此外,还有关于您的请求的类似问题 以及这个.

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

更新 1

如果您只想在 Net Core 项目上复制这些文件,您应该放弃使用 nupkg 中的 content 节点.安装包时,它会自动将文件复制到 NET Framework 主项目中.

相反,您可以将这些文件放在 nupkg 的名为 resource 的不同文件夹下.

你可以按照我的步骤:

1)MyPackage.props 文件更改为:

<目标名称=CopyFilesToProject"BeforeTargets=构建"><Message Text="复制 css 文件到项目";/><项目组><SourceScripts Include="$(MSBuildThisFileDirectory)....
esource***.* "/>//来自nuget包的文件</项目组><复制SourceFiles="@(SourceScripts)";DestinationFiles=@(SourceScripts -> '$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)')";/></目标></项目>

2)xxx.nuspec 文件更改为:

<包><元数据><id>我的包裹</id><version>1.0.1</version><title>MyProject</title><authors>Me</authors><owners>Me</owners><projectUrl>一些网址...</projectUrl><requireLicenseAcceptance>false</requireLicenseAcceptance><description>这类似于 bootstrap</description><版权>版权 2020</copyright><标签></标签><依赖项><dependency id="jQuery"版本=[3.0.0, 4.0.0)";/></依赖项></元数据><文件><file src=distcss***.*";目标=资源内容css";/><file src=distfonts***.*";目标=资源内容字体";/><file src="distjsmypackage.js";目标=资源脚本";/><file src=distimages***.*";目标=资源内容图像"/><file src="buildMyPackage.props";目标=构建"/></文件></包>

3) 然后重新打包您的项目并安装新的项目,在此之前,您应该先清理 nuget 缓存.

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="distcss***.*" target="contentContentcss" />
    <file src="distfonts***.*" target="contentContentfonts" />
    <file src="distjsmypackage.js" target="contentScripts" />
    <file src="distimages***.*" target="contentContentImages" />
  </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 ?

解决方案

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.

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="distcss***.*" target="contentContentcss" />
    <file src="distfonts***.*" target="contentContentfonts" />
    <file src="distjsmypackage.js" target="contentScripts" />
    <file src="distimages***.*" target="contentContentImages" />
    <file src="buildMyPackage.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:Usersxxx(current user).nugetpackages.

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)....
esource***.* "/> //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="distcss***.*" target="resourceContentcss" />
    <file src="distfonts***.*" target="resourceContentfonts" />
    <file src="distjsmypackage.js" target="resourceScripts" />
    <file src="distimages***.*" target="resourceContentImages" />
    <file src="buildMyPackage.props" target="build" />
  </files>
</package> 

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

这篇关于从静态文件为 dot net core 项目创建 Nuget 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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