无法使用 dotnet CLI 和 nuspec 文件打包 NuGet 包 [英] Unable to pack a NuGet package using dotnet CLI and nuspec file

查看:30
本文介绍了无法使用 dotnet CLI 和 nuspec 文件打包 NuGet 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<属性组><GenerateAssemblyInfo>false</GenerateAssemblyInfo><TargetFramework>netstandard2.0</TargetFramework></PropertyGroup></项目>

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

使用 System.Reflection;使用 System.Runtime.InteropServices;[装配:装配公司(公司名称")][程序集:AssemblyCopyright("Copyright © 2019")]#if 调试[装配:装配配置(调试")]#别的[装配:装配配置(发布")]#万一[程序集:AssemblyTitle(标题")][装配:装配描述(描述")][组装:组装产品(产品")][程序集:ComVisible(false)][组装:Guid("48d6ef54-a8fb-4876-8a9a-2fb8e8cd27e7")][装配:装配版本(6.0.0.0")][程序集:AssemblyFileVersion("6.0.0.0")]

然后我有一个 .nuspec 文件,我用它来创建我的 NuGet 包,它看起来像这样:

<包><元数据><id>$id$</id><version>$version$</version><title>$title$</title><authors>$author$</authors><owners>我的名字</owners><requireLicenseAcceptance>false</requireLicenseAcceptance><description>$description$</description><releaseNotes>我的发行说明</releaseNotes><版权>版权 2019</copyright><projectUrl>http://url.local</projectUrl></元数据></包>

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

结果是这个错误:

<块引用>

NuGet.Build.Tasks.Pack.targets(202,5): error : 发生错误时试图解析清单中属性version"的值"文件.[F:GitMyProjectModelMyProject.ModelMyProject.Model.csproj]

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

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

根据输出,很明显底层目标文件没有从我的 AssemblyInfo.cs 中获取值,而是试图直接从 csproj 中获取值 文件代替.所以我试图简化这个过程并运行这个命令:dotnet pack MyProject.ModelMyProject.Model.csproj 它创建了我的 NuGet 包,但在我的 nuspec 中没有元数据code> 或在我的 AssemblyInfo.cs 文件中.

我在 GitHub 上从 dotnet-cliNuGet 项目中阅读了很多未解决的问题,这些问题似乎与我遇到的类似,但是他们都没有给我一个有效的解决方案.有没有其他人认为这是一个错误,还是我遗漏了什么?

解决方案

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

package.nuspec<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>

I have several projects that I'm migrating from .NET Framework 4.7 to .NET Standard 2.0. As a result, I'm trying to use the dotnet pack command to create my NuGet package while using my nuspec file with tokens. I have several custom build scripts that generate the version number for me. My generated files are not apart of version control but nuspec file is hence why I'd like to try and find a way to get the tokens to work, otherwise I'm stuck writing new scripts.

The first thing I did was to set the GenerateAssemblyInfo to false in the project files. In my most simplest project, the csproj file looks like this:

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

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

</Project>

I then have an AssemblyInfo.cs that I generate as part of my build process. The file looks like this before I compile the project:

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")]

I then have a .nuspec file that I use to create my NuGet package which looks like this:

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

After much reading (not a fan of who ever wrote the new docs for .NET Core) I believe this is the command I need to run in order to use my nuspec file: dotnet pack SubFolderMyProject.Model.csproj /p:NuspecFile=MyProject.Model.nuspec /p:NuspecBasePath=nuget

The result is this error:

NuGet.Build.Tasks.Pack.targets(202,5): error : An error occured while trying to parse the value '' of property 'version' in the manifest file. [F:GitMyProjectModelMyProject.ModelMyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5): error : Value cannot be null or an empty string. [F:GitMyProjectModelMyProject.ModelMyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5): error : Parameter name: value [F:GitMyProjectModelMyProject.ModelMyProject.Model.csproj]

Based on the output, it's clear that the underlying targets file isn't grabbing the values out of my AssemblyInfo.cs but rather is trying to grab the values directly out of the csproj file instead. So I tried to simplify the process and ran this command: dotnet pack MyProject.ModelMyProject.Model.csproj which created my NuGet package, but did not have the metadata in my nuspec or in my AssemblyInfo.cs file.

I've read a lot of open issues on GitHub from both the dotnet-cli and NuGet projects that seem to be similar to what I'm experiencing, but none of them have given me a solution that works. Does anyone else think this is a bug or am I missing something?

解决方案

I had same problem and solved it by adding <NuspecProperties>to csproj as suggested here: 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>

这篇关于无法使用 dotnet CLI 和 nuspec 文件打包 NuGet 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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