首页 > 解决方案 > msbuild 没有从恢复的位置挑选包

问题描述

我想将我的 nugets 恢复一级。我的 repo 目录指向 ex。c:\repo\s 并且我的解决方案在 c:\repo\s\src 中,当我使用 nuget restore 恢复包时,它在 C:\repo\s\src\packages 恢复包,我希望是 C:\repo\s\packages。感谢你的帮助。

我在 C:\repo\s\src 目录中有以下 nuget.config 文件。

$<configuration>
  <config>
    <add key="repositoryPath" value="..\..\packages" />
  </config>
</configuration>

我的 Yaml 工作看起来像这样

$steps:
- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.3.0'

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: src/myproject.sln
    vstsFeed: '4448b1e2-8ac8-45ef-870c-1ebab90f3348'
    restoreDirectory: '$(Build.SourcesDirectory)'


    - task: VSBuild@1
      displayName: 'Build solution src/myproject.sln'
      inputs:
    solution: src/myproject.sln
    vsVersion: 15.0
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

标签: yamlazure-pipelinesado

解决方案


msbuild 没有从恢复的位置挑选包

无论是使用nuget.config文件还是直接restoreDirectory: '$(Build.SourcesDirectory)'nuget restore任务中指定,nuget都会将包还原到文件夹C:\repo\s\packages中。

但是,NuGet 还原仅将包还原到还原目录,而不会修改您的项目文件

当我们将nuget包添加到项目中时,它会在项目文件中添加以下代码来指定dll位置:

  <ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.12.0.3-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
  </ItemGroup>

有节点HintPath指定dll文件的位置。

当我们使用nuget.configrestoreDirectory: '$(Build.SourcesDirectory)'更改包还原的位置时,MSBuild 不会拾取基于 HintPath. 正确的 HintPath 应该是:

<HintPath>..\..\packages\Newtonsoft.Json.12.0.3-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>

这就是为什么 msbuild 没有从恢复的位置挑选包的原因。

要解决此问题,您需要在包管理器控制台中使用 NuGet 命令行(在本地 VS 上):

Update-Package -reinstall

要强制将包引用重新安装到项目中,它将更新HintPath. 将更改文件上传到 Azure devops 并构建它。

希望这可以帮助。


推荐阅读