使用dotnet CLI和nuspec文件无法打包NuGet包

7
我有几个项目正在从.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 : 尝试解析清单文件中属性“version”的值''时发生错误。[F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

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

NuGet.Build.Tasks.Pack.targets(202,5): error : 参数名: value [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

根据输出,很明显底层目标文件没有从我的AssemblyInfo.cs中获取值,而是尝试直接从csproj文件中获取值。因此,我尝试简化该过程并运行了以下命令:dotnet pack MyProject.Model\MyProject.Model.csproj,它创建了我的NuGet包,但没有在我的nuspec或AssemblyInfo.cs文件中包含元数据。
我已经阅读了GitHub上许多来自dotnet-cli和NuGet项目的公开问题,似乎与我所经历的类似,但没有一个给我有效的解决方案。是否有人认为这是一个错误或者我漏掉了什么?

你可以直接从 .Net 标准库的属性 -> 包 -> 生成 NuGet 包来生成 NuGet 包。 - fstam
那无法集成到我的构建流程中。我有自定义脚本为我生成版本,并按照对我的程序集有意义的模式进行排列。使用Visual Studio选项不起作用,因为我的CI/CD流程不使用VS,而由VS生成的版本号也不符合我的模式。 - tj-cappelletti
你能展示一个填好$id$的nuspec文件的例子吗?我想版本号可能无法解析,因为它不是一个.NET版本,也许在其中有一个空字符串。 - fstam
我向您保证,这不是问题所在。在使用.NET Framework 4.7和nuget CLI时,该过程一直正常工作。只有两个东西发生了变化; 目标框架是.NET Standard 2.0,并且我正在使用dotnet CLI来打包项目。 - tj-cappelletti
我理解,我无法复现。这就是为什么我要求提供可重现的案例。 - fstam
1
你创建了一个带有项目文件、程序集信息和我在这里发布的nuspec文件的C#项目,并使用“dotnet”命令运行pack命令后得到了具有正确元数据的包? - tj-cappelletti
1个回答

9

我遇到了同样的问题,通过在csproj中添加<NuspecProperties>,按照这里的建议得以解决: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>

2
请问您将这些属性放在哪个节点中? - GGO
@GGO - 在 PropertyGroup 中 - Miq
Miq,你后面如何使用dotnet pack命令? - frozzen10
@frozzen10 只需使用 dotnet pack 命令。我一直在使用 Azure Pipelines,现在不记得普通的 cmd 命令了。我使用它的项目代码在这里:https://github.com/miqm/wpftoolkit/blob/master/azure-pipelines.yml - Miq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接