【问题标题】:Unable to pack a NuGet package using dotnet CLI and nuspec file无法使用 dotnet CLI 和 nuspec 文件打包 NuGet 包
【发布时间】:2019-03-04 07:50:24
【问题描述】:

我有几个项目要从 .NET Framework 4.7 迁移到 .NET Standard 2.0。因此,我尝试使用 dotnet pack 命令创建我的 NuGet 包,同时使用带有令牌的 nuspec 文件。我有几个为我生成版本号的自定义构建脚本。我生成的文件不是版本控制的一部分,但 nuspec 文件是我想尝试找到一种方法让令牌工作的原因,否则我会被困在编写新脚本。

我做的第一件事是在项目文件中将GenerateAssemblyInfo 设置为false。在我最简单的项目中,csproj 文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
     <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

然后我会生成一个AssemblyInfo.cs,作为构建过程的一部分。在我编译项目之前文件是这样的:

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany("Company Name")]
[assembly: AssemblyCopyright("Copyright © 2019")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

[assembly: AssemblyTitle("The Title")]
[assembly: AssemblyDescription("The Description")]
[assembly: AssemblyProduct("The Product")]
[assembly: ComVisible(false)]
[assembly: Guid("48d6ef54-a8fb-4876-8a9a-2fb8e8cd27e7")]
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.0.0")]

然后我有一个 .nuspec 文件,我用它来创建我的 NuGet 包,如下所示:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>My name</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>My Release Notes</releaseNotes>
    <copyright>Copyright 2019</copyright>
    <projectUrl>http://url.local</projectUrl>
  </metadata>
</package>

经过大量阅读(不喜欢为 .NET Core 编写新文档的人),我相信这是我需要运行的命令才能使用我的 nuspec 文件: dotnet pack SubFolder\MyProject.Model.csproj /p:NuspecFile=MyProject.Model.nuspec /p:NuspecBasePath=nuget

结果是这个错误:

NuGet.Build.Tasks.Pack.targets(202,5): error : 发生错误时 试图解析清单中属性“版本”的值“” 文件。 [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5):错误:值不能为空 或空字符串。 [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5):错误:参数名称:值 [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

根据输出,很明显底层目标文件没有从我的AssemblyInfo.cs 中获取值,而是尝试直接从csproj 文件中获取值。因此,我尝试简化流程并运行以下命令:dotnet pack MyProject.Model\MyProject.Model.csproj,它创建了我的 NuGet 包,但在我的nuspecAssemblyInfo.cs 文件中没有元数据。

我在 GitHub 上从 dotnet-cliNuGet 项目中阅读了很多未解决的问题,这些问题似乎与我正在经历的类似,但它们都没有给我一个有效的解决方案。有没有其他人认为这是一个错误或者我错过了什么?

【问题讨论】:

  • 您可以直接从 .Net 标准库的属性中生成 NuGet 包 -> 包 -> 在构建时生成 NuGet 包。
  • 这不会集成到我的构建过程中。我有自定义脚本可以为我生成版本以及对我的程序集有意义的模式。使用 Visual Studio 选项将不起作用,因为我的 CI/CD 流程不使用 VS,并且 VS 生成的版本号不符合我的模式。
  • 你能展示一个示例 nuspec 文件吗?我的意思是填充了 $id$。我想版本无法解析,因为它不是 dot net 版本,并且可能某处有一个空字符串。
  • 我向你保证,这不是问题所在。使用 .NET Framework 4.7 和 nuget CLI,此过程运行良好。此后只有两件事发生了变化。目标框架是 .NET Standard 2.0,我正在使用 dotnet CLI 打包项目。
  • 那么您创建了一个 C# 项目,其中包含我在此处发布的项目文件、程序集信息和 nuspec 文件,并使用 dotnet 命令运行了 pack 命令并获得了包含正确元数据的包?跨度>

标签: c# .net-core nuget-spec


【解决方案1】:

我遇到了同样的问题,并按照此处的建议将&lt;NuspecProperties&gt;添加到 csproj 解决了它:https://github.com/NuGet/Home/issues/6636

<NuspecFile>package.nuspec</NuspecFile>
<NuspecProperties>$(NuspecProperties);configuration=$(Configuration)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);version=$(Version)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);id=$(PackageId)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);author=$(Authors)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageProjectUrl=$(PackageProjectUrl)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);Description=$(Description)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageReleaseNotes=$(PackageReleaseNotes)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);Copyright=$(Copyright)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageTags=$(PackageTags)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);RepositoryType=$(RepositoryType)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);RepositoryUrl=$(RepositoryUrl)</NuspecProperties>

【讨论】:

  • 请把这些属性放在哪个节点上?
  • @GGO - 在 PropertyGroup 中
猜你喜欢
  • 2015-08-17
  • 2022-10-13
  • 2015-10-13
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多