忽略 Azure 管道中 .vdproj 项目上的 NuGet 包还原



我正在尝试针对包含Visual Studio Installer Project的.NET Framework 4.7.2解决方案设置Azure DevOps构建管道。我已经在安装了Visual Studio 2019 Community的Windows Server 2019 VM上设置了一个自承载代理。生成管道包含 NuGet 安装程序任务,后跟 NuGet 任务,设置为还原引用的 NuGet 包。下面是 YAML 代码段:

- task: NuGetCommand@2
inputs:
command: 'restore'
restoreSolution: '$(solution)'

但是,使用此配置运行生成会在生成日志中导致以下错误:

[错误]nuget 命令失败,退出代码 (1( 和错误 (C:\###.vdproj(1,1(:错误 MSB4025:无法加载项目文件。根级别的数据无效。第 1 行,位置 1。

这似乎是由于较新版本的 nuget.exe 中进行了性能增强。基于此 GitHub 问题的建议是启用使用RestoreUseSkipNonexistentTargetsMSBuild 设置跳过不存在的包目标。

GitHub 问题提到使用NUGET_RESTORE_MSBUILD_ARGSNuGet CLI 环境变量来设置此属性,但我不知道如何通过 NuGet 生成任务实现这一点。

由于 NuGet 现在与 MSBuild 集成,因此我随后尝试通过 NuGet 任务上的命令行参数将此属性设置为false。我修改了 YAML,将命令设置为custom以便传递参数。我的语法基于 MSBuild 还原文档。它现在如下所示:

- task: NuGetCommand@2
inputs:
command: 'custom'
arguments: 'restore "$(solution)" -p:RestoreUseSkipNonexistentTargets=false'

此生成配置会导致以下错误:

[错误]nuget 命令失败,退出代码 (1( 和错误(未知选项:"-p:恢复使用跳过不存在的目标=false"(

我的问题是,如何获取 NuGet 还原任务以跳过 .vdproj 项目上的包还原?

编辑

解决方案中的另一个项目是 C# WinForms .NET Framework 项目。我们使用 packages.config 而不是 PackageReference。

至于你原来的问题:MSB4025

正如你上面提到的,这是一个悬而未决的问题。任何对此感兴趣的人都可以在那里跟踪问题。

[错误]nuget 命令失败,退出代码 (1( 和错误 (未知( 选项:"-p:RestoreUseSkipNonexistentTargets=false"(

nuget 还原命令无法识别 msbuild 属性。在此处查看类似问题和更多详细信息。

The other project in the solution is a C# WinForms .NET Framework project. We're using packages.config rather than PackageReference.年以来

解决此问题的方法是使用 nuget 自定义命令,如下所示:

- task: NuGetCommand@2
inputs:
command: 'custom'
arguments: 'restore YourProjectNamepackages.config -PackagesDirectory $(Build.SourcesDirectory)packages'

这可以跳过安装程序项目的还原步骤。

一般来说,你不需要再显式调用nuget restore了。MSBuild 在生成过程中自动执行此操作(因此你可能会执行两次(。可以将p:RestoreUseSkipNonexistentTargets=false属性添加到VSBuild任务或DotNet生成或发布任务的 MSBuild 参数:

- task: DotNetCoreCLI@2
displayName: Build/Publish
inputs:
command: 'publish'
publishWebProjects: false
projects: '$(solution)'
arguments: '-r $(runtimeIdentifier) /p:RestoreUseSkipNonexistentTargets=false'
zipAfterPublish: false
modifyOutputPath: false

我发现的最好的方法: 在 VS2022 托管映像上测试的管道上使用 3 个任务

所有任务都使用 Visual Studio 2022 开发人员 PowerShell。

任务 1 - 在忽略不受支持的 vdproj 错误的情况下还原 nuget 任务 2 - 使用 msbuild 生成解决方案不会生成 vdproj 任务 3 - 仅使用 DevEnv 构建 vdproj

- task: PowerShell@2
displayName: "restore nuget"
inputs:
targetType: 'inline'
script: |
& 'C:Program FilesMicrosoft Visual Studio2022EnterpriseCommon7ToolsLaunch-VsDevShell.ps1'
msbuild -t:restore .<solution>.sln -p:RestoreUseSkipNonexistentTargets=false
ignoreLASTEXITCODE: true
pwsh: true
- task: PowerShell@2
displayName: "build solution"
inputs:
targetType: 'inline'
script: |
& 'C:Program FilesMicrosoft Visual Studio2022EnterpriseCommon7ToolsLaunch-VsDevShell.ps1'
msbuild .<solution>.sln
pwsh: true
- task: PowerShell@2
displayName: "create install "
inputs:
targetType: 'inline'
script: |
& 'C:Program FilesMicrosoft Visual Studio2022EnterpriseCommon7ToolsLaunch-VsDevShell.ps1'
devenv ".<project>.vdproj" /Build
pwsh: true

最新更新