【问题标题】:Azure devops Cant connect Nuget Restore and VSBuild for Nuget PackagesAzure devops 无法连接 Nuget Restore 和 VSBuild for Nuget 包
【发布时间】:2020-01-09 22:44:09
【问题描述】:

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

# 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 还原任务将包恢复到 ..\new_packages 目录,随后希望 VSBuild 任务能够从那里获取它们并构建解决方案。

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

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

他们的解决方案(虽然很老套)对我也不起作用。

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

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

编辑:

感谢Leo Liu-MSFT
我的答案在那个下面。

【问题讨论】:

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


    【解决方案1】:

    Azure devops 无法连接 Nuget Restore 和 VSBuild for Nuget 包

    除非您修改项目文件中的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会直接从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文件中的repositoryPath,改变你本地的nuget包目录:

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

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

    希望这会有所帮助。

    【讨论】:

    • nuget.config
    • 构建 yml 有或没有 restoreDirectory: '..\packages' 或 ` '.\packages' `
    • 更新——所有使用<PackageRefrence>而不是packages.config的项目都在工作。 MVC WebClient 很困难,因为预装了 MS-libraries /packages
    • 它不断创建一个临时 nuget 配置文件,即使我一直指定查看位置
    • 我得到了它的工作,我删除了包文件夹中的所有内容并推送。它迫使 azure 下载和恢复,并以某种方式帮助构建。我在下面放了更多细节
    【解决方案2】:

    我得到了它的工作,但有一些怪癖(和功能?)和几个悬而未决的问题。

    请参阅上面的/users/7460777/leo-liu-msft,了解为什么要采取这些步骤。

    正如 Leo 所说,这与使用 packages.config 的项目中的 HintPath 未正确更新有关

    为了完全避免这个问题,
    将尽可能多的项目迁移到<PackageReference />,而不是使用packages.config。为此,请右键单击每个项目内的References,应该有一个选项。
    某些项目(例如 -> 在我的情况下,我的 asp.net (.net Framework) MVC Web 客户端)无法迁移。
    (..我稍后会添加更多详细信息)
    更新:
    (https://devblogs.microsoft.com/nuget/migrate-packages-config-to-package-reference/)
    (https://github.com/NuGet/Home/issues/5877)


    接下来的步骤:--

    1) 在我拥有 .sln 文件的顶级文件夹中,我创建了以下 nuget.configfile。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      相关资源
      最近更新 更多