簡體   English   中英

Azure devops 無法為 Nuget 包連接 Nuget 還原和 VSBuild

[英]Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages

到目前為止,我有一個非常基本的構建管道

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.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 在 Visual Studio Online 上找不到還原的 NuGet 包

他們的解決方案(雖然 hacky)對我也不起作用。

如果我刪除 EPPlus 包,下一個包Entity Framework將產生相同的錯誤。
如果我刪除Entity Framework ,下一個包將產生相同的錯誤。

我只是想為此找到一個真正的解決方案,它不涉及這些硬編碼黑客(..並且顯然有效)

編輯:

感謝Leo Liu-MSFT工作,怪癖。
我的答案在那個下面。

Azure devops 無法為 Nuget 包連接 Nuget 還原和 VSBuild

恐怕除非您修改項目文件中的HintPath ,否則您無法使其工作。

將還原后的nuget包更改到..\\new_packages目錄下,還原后,新目錄下的包應該有如下結構層次:

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

dll 文件以 nuget 包的形式存儲在您的新目錄中,而不是直接存儲在新目錄中。

因此,當您在構建項目時指定/p:ReferencePath="d:\\a\\1\\new_packages"時,VS 將直接從ReferencePath找到dll文件。 您收到錯誤消息的原因是:

考慮“d:\\a\\1\\new_packages\\EPPlus.dll”

實際上, dll路徑應該是d:\\a\\1\\new_packages\\EPPlus.4.5.3.2\\lib\\net40 由於此路徑與包的名稱和版本相關,因此我們無法使用一個參數來指定多個dlls的路徑。

在這種情況下,我們必須修改項目文件中的HintPath

正確的過程是更改nuget.config文件中的nuget.config以更改本地上的 nuget 包目錄:

是否可以更改 NuGet 包的位置?

然后在包管理器控制台中使用命令行Update-Package -reinstall強制將包引用重新安裝到本地項目中,它將更新所有包的HintPath ,然后將更改提交到源代碼管理。 然后我們可以使用 Nuget Restore 任務將包恢復到..\\new_packages目錄

希望這可以幫助。

我讓它工作了,但有一些怪癖(和功能?)和幾個懸而未決的問題。

請參閱上面的/users/7460777/leo-liu-msft以了解有關為什么要采取這些步驟的一些背景信息。

正如 Leo 提到的,這與HintPath在使用packages.config的項目中沒有正確更新有關

為了完全避免這個問題,
將盡可能多的項目遷移到<PackageReference />而不是使用packages.config 為此,右鍵單擊每個項目內的References ,應該有一個選項可以這樣做。
某些項目(例如 -> 在我的情況下,我的 asp.net (.net Framework) MVC Web Client)無法遷移。
(..我稍后會添加更多細節)
更新:
https://devblogs.microsoft.com/nuget/migrate-packages-config-to-package-reference/
( https://github.com/NuGet/Home/issues/5877 )


下一步: -

1) 在包含.sln文件的頂級文件夾中,我創建了以下nuget.config文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value=".\packages" />
  </config>
</configuration>

因為我的packages目錄在這里。

2) 我更新的build.yml如下。

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

trigger:
- testing-build-yml

pool:
  vmImage: 'windows-latest'

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

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'
    packagesdirectory: '.\packages'

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

3)以下步驟使一切正常,但我仍然不確定究竟是為什么。
我刪除了packages文件夾中的所有內容,基本上只是將這些刪除推送到上游。 Nuget restore Task 進行了恢復並且 VSBuild 任務成功。


我還不確定的事情——

1) 為什么會創建一個臨時Nuget.config文件,即使我清楚地說明了它的存在位置?

Saving NuGet.config to a temporary config file.
[command]C:\hostedtoolcache\windows\NuGet\5.4.0\x64\nuget.exe sources Add -NonInteractive -Name NuGetOrg -Source https://api.nuget.org/v3/index.json -ConfigFile d:\a\1\Nuget\tempNuGet_140.config
Package source with Name: NuGetOrg added successfully.
[command]C:\hostedtoolcache\windows\NuGet\5.4.0\x64\nuget.exe restore d:\a\1\s\XYZ.sln -PackagesDirectory .\packages -Verbosity Detailed -NonInteractive -ConfigFile d:\a\1\Nuget\tempNuGet_140.config
NuGet Version: 5.4.0.6315
MSBuild auto-detection: using msbuild version '16.4.0.56107' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
MSBuild P2P timeout [ms]: 120000
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin\msbuild.exe 
"C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\0eie04ka.4ch.nugetinputs.targets" /t:GenerateRestoreGraphFile /nologo /nr:false /v:q /p:NuGetRestoreTargets="C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\y3ui4hhz.gfr.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_140.config" /p:RestorePackagesPath=".\packages" /p:SolutionDir="d:\a\1\s\\" /p:SolutionName="XYZ"

2)為什么刪除包使它工作而提交包卻無法找到任何dll文件,即使它查看了正確的位置?

Primary reference "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, 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 "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, 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 "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, 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\XYZ_WebClient\XYZ_WebClient.csproj]
          For SearchPath "{HintPathFromItem}".
          Considered "D:\a\1\s\packages\NLog.4.6.7\lib\net45\NLog.dll", but it didn't exist.
          For SearchPath "{TargetFrameworkDirectory}".
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\NLog.winmd", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\NLog.dll", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\NLog.exe", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\NLog.winmd", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\NLog.dll", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\NLog.exe", but it didn't exist.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM