为了账号安全,请及时绑定邮箱和手机立即绑定

nuget.exe 无法识别 nuspec 文件中的任何目标框架

nuget.exe 无法识别 nuspec 文件中的任何目标框架

C#
人到中年有点甜 2023-09-24 16:12:55
我有一个项目,通过在 CSPROJ 文件中设置以下选项,直接在 Visual Studio 中生成 NuGet 包:<GeneratePackageOnBuild>true</GeneratePackageOnBuild>这会导致生成的包包含所有已标记为“复制到输出目录=不复制”的嵌入资源文件。遗憾的是,Visual Studio 的自动打包程序选择始终将这些文件复制到 NuGet 包中。为了解决这个问题,我一直在研究编辑 .NUSPEC 文件并从命令行使用 NUGET.EXE 来创建包。然后我遇到了一个新问题。通过使用 NUGET.EXE 而不是 Visual Studio,生成的包将依赖项部分显示为“不受支持”,我通过在“NuGet Explorer”中打开包来看到这一点:这是用于创建包的 .bat 文件:c:\nuget\nuget.exe config -Set repositoryPath="%USERPROFILE%\.nuget\packages"c:\nuget\nuget.exe pack -IncludeReferencedProjects -properties Configuration=Release这是 NUSPEC 文件:<?xml version="1.0" encoding="utf-8"?><package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">  <metadata>    <id>Integrative.Lara</id>    <version>0.5.3</version>    <authors>Pablo Carbonell, Integrative Software LLC</authors>    <owners>Pablo Carbonell, Integrative Software LLC</owners>    <requireLicenseAcceptance>true</requireLicenseAcceptance>    <license type="file">LICENSE</license>    <projectUrl>https://github.com/integrativesoft/lara</projectUrl>    <iconUrl>https://integrative.b-cdn.net/Integrative.ico</iconUrl>    <description>Lara is ...</description>    <copyright>Copyright (c) 2019 Integrative Software LLC</copyright>    <tags>lara, web, html, html5, desktop, gui, cross, framework, mac, osx, platform, ui, blazor, razor</tags>    <repository url="https://github.com/integrativesoft/lara" />    <dependencies>      <group targetFramework=".NETStandard2.0">        <dependency id="Microsoft.AspNetCore" version="2.2.0" exclude="Build,Analyzers" />        <dependency id="Microsoft.AspNetCore.WebSockets" version="2.2.1" exclude="Build,Analyzers" />      </group>    </dependencies>  </metadata></package>有没有办法修复“不受支持”的目标?我也尝试使用“netstandard2.0”和其他标识符,但仍然得到相同的“不受支持”。或者,有没有办法使用 Visual Studio 的自动包生成并阻止它在包中包含文件?
查看完整描述

1 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

如您所见,当您使用 nuspec 时,您有责任做好每一件小事。使用 NuGet 的 MSBuild 包目标更容易,因为它可以自动执行创建依赖项等操作,包括在组中使用正确的 TFM。

NuGet 关于打包目标的文档包含与使用 msbuild 打包相关的内容(这就是您使用dotnet pack或时发生的情况GeneratePackageOnBuild)。特别是关于在包中包含内容的部分有以下示例:

<Content Include="..\win7-x64\libuv.txt">
 <Pack>false</Pack>
 </Content>

由于您的文件是嵌入的,您的 csproj 将包含类似<EmbeddedResource Include="whatever.ext" />. 因此,使用文档中的信息,您可以这样做<EmbeddedResource Include="whatever.ext" Pack="false" />,或者像文档一样使用多行版本。MSBuild 允许您以任何一种方式设置项目元数据。

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

关于GeneratePackageOnBuild的注意事项:让build为你创建包很方便,但这意味着当你在调试并且需要在再次测试之前更改一行代码时,你不仅要等待构建,还需要等待盒。如果你的包很小,它可能相当快,但它仍然会减慢你的“内循环”体验。大多数开发人员只需要打包的频率远低于构建的频率,因此我建议禁用GeneratePackageOnBuild,并dotnet pack在您实际需要 nupkg 时在项目(或解决方案)上运行。


查看完整回答
反对 回复 2023-09-24
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信