c# - Azure devops 无法连接 Nuget Restore 和 MSBuild for Nuget Packages

标签 c# msbuild azure-devops nuget nuget-package-restore

到目前为止我有一个非常基本的构建管道

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- testing-build-yml-1

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'
    # packagesDirectory: '..\packages'
    restoreDirectory: '..\new_packages'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    #msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)" /p:ReferencePath="d:\a\1\new_packages"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

我正在尝试使用 Nuget Restore 任务将包恢复到 ..\new_packages 目录,随后希望 VSBuild 任务能够从那里获取它们并构建解决方案。

Nuget Restore 执行以下命令(全部在一行中)

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin\msbuild.exe 
  "C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\jgtjjpe0.nac.nugetinputs.targets" 
  /t:GenerateRestoreGraphFile 
   /nologo 
   /nr:false 
   /v:q 
  /p:NuGetRestoreTargets="C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\whba0xm4.c0f.nugetrestore.targets" 
  /p:RestoreUseCustomAfterTargets="True" /p:RestoreTaskAssemblyFile="C:\hostedtoolcache\windows\NuGet\5.4.0\x64\nuget.exe" 
  /p:RestoreSolutionDirectory="D:\a\1\s\\" 
  /p:RestoreConfigFile="D:\a\1\Nuget\tempNuGet_105.config" 
  /p:RestorePackagesPath="..\new_packages" 
  /p:SolutionDir="D:\a\1\s\\" 
  /p:SolutionName="XYZ"

然后 VSBuild Task 运行 msbuild(全部在一行中)

##[command]"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\msbuild.exe" 
 "D:\a\1\s\XYZ.sln" 
   /nologo 
   /nr:false 
   /dl:CentralLogger,
 "D:\a\_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda\1.161.2\ps_modules\MSBuildHelpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll";
 "RootDetailId=e5869970-8e7b-4a06-a8fd-f0662f639a6d|SolutionDir=D:\a\1\s"*ForwardingLogger,"D:\a\_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda\1.161.2\ps_modules\MSBuildHelpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll"
 /p:DeployOnBuild=true 
 /p:WebPublishMethod=Package 
 /p:PackageAsSingleFile=true 
 /p:SkipInvalidConfigurations=true 
 /p:PackageLocation="D:\a\1\a" 
 /p:ReferencePath="d:\a\1\new_packages" 
 /p:platform="Any CPU" 
 /p:configuration="Release" 
 /p:VisualStudioVersion="16.0" 
 /p:_MSDeployUserAgent="VSTS_607486bc-2d64-463d-9a7f-0cf06fe82263_build_16_0"

解决方案查看 /p:ReferencePath="d:\a\1\new_packages 中指定的相应目录。

但它仍然找不到 Nuget 包的任何 dll,最终构建失败。

    Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.
Build started 1/9/2020 10:11:18 PM.
Project "D:\a\1\s\XYZ.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "D:\a\1\s\XYZ.sln" (1) is building "D:\a\1\s\ServiceLayer\InputOutputServiceLayer.csproj" (2) on node 1 (default targets).
PrepareForBuild:
  Creating directory "bin\Release\".
  Creating directory "obj\Release\".
ResolveAssemblyReferences:
  Primary reference "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [D:\a\1\s\ServiceLayer\InputOutputServiceLayer.csproj]
          For SearchPath "d:\a\1\new_packages".
          Considered "d:\a\1\new_packages\EPPlus.winmd", but it didn't exist.
          Considered "d:\a\1\new_packages\EPPlus.dll", but it didn't exist.
          Considered "d:\a\1\new_packages\EPPlus.exe", but it didn't exist.
          For SearchPath "{HintPathFromItem}".
          Considered "D:\a\1\s\packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll", but it didn't exist.

从技术上讲,我知道我们不应该对 /p:ReferencePath=.. 进行硬编码来执行此操作,但我只是想让它发挥作用。

如果没有 VSBuild Task,似乎不知道该去哪里查看。

它看起来

 ResolveAssemblyReferences:
  Primary reference "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [d:\a\1\s\ServiceLayer\InputOutputServiceLayer.csproj]
          For SearchPath "..\d:\a\1\a".
          Considered "..\d:\a\1\a\EPPlus.winmd", but it didn't exist.
          Considered "..\d:\a\1\a\EPPlus.dll", but it didn't exist.
          Considered "..\d:\a\1\a\EPPlus.exe", but it didn't exist.
          For SearchPath "{HintPathFromItem}".
          Considered "d:\a\1\s\packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll", but it didn't exist.
          For SearchPath "{TargetFrameworkDirectory}".
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\EPPlus.winmd", but it didn't exist.

使用旧的管道使用方式有一个类似的问题

MsBuild does not find restored NuGet-Packages on Visual Studio Online

他们的解决方案(虽然很hacky)也不适合我。

如果我删除 EPPlus 包,下一个包Entity Framework 将产生相同的错误。
如果我删除 Entity Framework ,下一个包将产生相同的错误。

我只是想找到一个真正的解决方案,不涉及这些硬编码黑客(..并且显然有效)

编辑:

得益于 Leo Liu-MSFT,工作中充满了怪癖。
我的答案就在下面。

最佳答案

Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages

恐怕除非您修改项目文件中的HintPath,否则您无法使其工作。

当您更改还原到 ..\new_packages 目录的 nuget 包时,还原后,新目录中的包应具有以下结构级别:

d:\a\1\new_packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll

dll文件以nuget包的形式存储在你的新目录中,而不是直接存储在新目录中。

所以,当你在构建项目时指定/p:ReferencePath="d:\a\1\new_packages"时,VS会直接找到dll文件来自ReferencePath。您收到错误消息的原因是:

Considered "d:\a\1\new_packages\EPPlus.dll"

实际上,dll路径应该是d:\a\1\new_packages\EPPlus.4.5.3.2\lib\net40。由于该路径与包的名称和版本都相关,因此我们无法使用一个参数来指定多个dll的路径。

在这种情况下,我们必须修改项目文件中的HintPath

正确的过程是更改nuget.config文件中的repositoryPath来更改本地的nuget包目录:

Is it possible to change the location of packages for NuGet?

然后在程序包管理器控制台中使用命令行 Update-Package -reinstall 强制将程序包引用重新安装到本地项目中,它将更新 所有包的 HintPath,然后将更改提交到源代码管理。然后我们可以使用 Nuget Restore 任务将包恢复到 ..\new_packages 目录

希望这有帮助。

关于c# - Azure devops 无法连接 Nuget Restore 和 MSBuild for Nuget Packages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59673188/

相关文章:

azure - 是否可以使用 PowerShell 脚本创建 Azure Databricks 集群?

docker - 如何从 Azure key 保管库获取证书以用于 docker 镜像

c# - 如何解决此错误 "file is encrypted or is not a database file is encrypted or is not a database"Source=System.Data.SQLite

asp.net-mvc - 基于条件编译符号启用 MVCBuildViews

msbuild - 从 Azure Devops 中的命令行任务运行 MSBuild

c++ - Visual Studio 的 "Add reference"实际上是做什么的?

azure-devops - 是否可以一次性从 VSTS 导出所有构建和发布定义?

c# - IoC比较

c# - 如何在 asp.net 5 项目中添加本地 dll

c# - 寻找一种更灵活的方法将定界字符串转换为 StringDictionary