Azure Devops 上带有项目样式 PackageReference 的 Dotnet 包失败,并显示“NuGet 错误 NU5019:找不到文件”

问题描述

我正在尝试从 Azure DevOps 上的 C# 项目生成 NuGet 包。 我已经在构建管道中设置了一个“dotnet pack”任务来生成包。 该项目的目标是 netstandard20。 项目样式是 packagereference。 它有一些依赖项,但只有在 nuget.org 上可用。

构建任务失败并显示 error NU5019: File not found

这是构建任务的输出

Build log

NU5019 表示 nuspec 文件“...包含不存在的文件”:https://docs.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5019

由构建任务生成提取的 .nuspec 文件包含此 部分:

  <files>
    <file src="C:\agent\_work\23\s\GenericDataAccess\bin\Production\netstandard2.0\Sparinvest.GenericDataAccess.dll" target="lib\netstandard2.0\Sparinvest.GenericDataAccess.dll" />
  </files>

在我看来,构建步骤构建了 .dll,然后立即无法找到它进行打包。 但是,生成了 .dll。

当我在我的机器上本地执行相同的命令行(在构建日志中看到)时,没有问题。

但是,我注意到我的机器上的 .Net 版本与构建服务器上的不同(请参阅构建日志): My machine

谁能帮我弄清楚构建步骤的问题是什么以及我如何使它工作?

注意:我更喜欢使用“dotnet pack”而不是 NuGet.exe,因为 dotnet 能够在包中包含依赖项(NuGet 不支持 packagereference 样式)。

解决方法

根据您提供的快照,dotnet pack 命令在 Azure 管道中为 pack ***.csproj --output ***/a /p:Configuration=Production /p:PackageVersion=1.0.1675,但在您的本地命令中为 pack ***.csproj --output ***/a /p:Configuration=Production /p:PackageVersion=1.0.0

您是否在 Azure 管道中指定变量 PackageVersion 导致它使用不同的包版本?如果您使用.NET Core CLI task,请检查其参数设置。 enter image description here

,

这似乎是通过较新的工具版本解决的问题(我看到您的 msbuild 版本是 16.3 - 在撰写本文时最近的 SDK 带有 16.916.10 预览版)。

使用 Use .NET Core 任务通过管道 UI 安装更新的 dotnet SDK 版本,或者对于 YAML 管道,请使用以下内容:

- task: UseDotNet@2
  displayName: 'Install .NetCore 5.0.x'
  inputs:
    packageType: 'sdk'
    version: '5.0.x'
,

谢谢你,马丁,你把我引向了正确的方向。

我不知道它到底是什么,旧的 dotnet SDK 版本无法做到,但新版本解决了这个问题。

我为构建工作做了什么:

要查看我的机器上有哪个版本的 pack 命令可以正常工作:

dotnet --info

(您可以在管道中的构建任务中执行相同的命令以查看构建服务器上安装的版本) 在我的机器上,最新的 SDK 版本是 5.0.101。

我在打包任务之前添加了“.NET Core SDK/运行时安装程序”任务并指定了版本 5.0.101。 注意:为了安装 SDK 5.0.101 版,我必须选择较新的 Task 版本 (2.* (preview))。

Use .NET 5.0.101 task

此后第一次构建运行的输出要好得多:

##[section]Starting: dotnet pack
==============================================================================
Task         : .NET Core
Description  : Build,test,package,or publish a dotnet application,or run a custom dotnet command
Version      : 2.153.3
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
[command]C:\Windows\system32\chcp.com 65001
Active code page: 65001
[command]C:\agent\_work\_tool\dotnet\dotnet.exe pack C:\agent\_work\23\s\GenericDataAccess\GenericDataAccess.csproj --output C:\agent\_work\23\a /p:Configuration=Production /p:PackageVersion=1.0.1678

Welcome to .NET 5.0!
---------------------
SDK Version: 5.0.101

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored C:\agent\_work\23\s\GenericDataAccess\GenericDataAccess.csproj (in 4,06 sec).
  GenericDataAccess -> C:\agent\_work\23\s\GenericDataAccess\bin\Production\netstandard2.0\Sparinvest.GenericDataAccess.dll
  Successfully created package 'C:\agent\_work\23\a\Sparinvest.GenericDataAccess.1.0.1678.nupkg'.
##[section]Finishing: dotnet pack