首页 > 解决方案 > nuget restore 无法从本地存储库还原包

问题描述

从全局 nuget 存储库恢复

我有这个 packages.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="jQuery" version="3.1.1" targetFramework="net46" />
  <package id="NLog" version="4.3.10" targetFramework="net46" />
</packages>

使用此命令,我可以恢复上面 packages.config 中列出的 nuget-packages:

nuget restore packages.config -PackagesDirectory NuGetPackages -NoCache

这很好用。

此示例的所有文件都可以在这里找到: https ://github.com/ezeh2/nuget_restore_examples/tree/master/nuget_with_global_nuget_repository

从本地 nuget 存储库恢复

在我的公司,我们必须使用位于网络共享上的存储库。而且我们不允许从标准的全局 nuget-repository 进行包还原。

出于演示目的,我们假设本地包存储库在目录中NuGetPackagesSource ,这是它的内容:

-NuGetPackagesSource
 |-jquery
    |-3.1.1
      |-jquery.3.1.1.nupkg
      |-jquery.3.1.1.nupkg.sha512
      |-jquery.nuspec
 |-nlog.4.3.10.nupkg

使用此 NuGet.Config,我们确保使用本地包存储库而不是全局包存储库:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <disabledPackageSources>
    <add key="nuget.org" value="true" />
  </disabledPackageSources>
  <packageSources>    
    <add key="my" value="NuGetPackagesSource" />
  </packageSources>
</configuration>

以下从本地目录进行还原NuGetPackagesSource

nuget restore packages.config -ConfigFile NuGet.config -PackagesDirectory NuGetPackages -NoCache

不幸的是,出现以下错误:

警告:无法找到包 'jQuery' 的版本 '3.1.1'。C:\software_download\nuget\NuGetPackagesSource:在源“C:\software_download\nuget\NuGetPackagesSource”上找不到包“jQuery.3.1.1”。

将包“NLog.4.3.10”添加到文件夹“C:\software_download\nuget\NuGetPackages” 将包“NLog.4.3.10”添加到文件夹“C:\software_download\nuget\NuGetPackages”

packages.config 项目中的错误无法找到包“jQuery”的版本“3.1.1”。C:\software_download\nuget\NuGetPackagesSource:在源“C:\software_download\nuget\NuGetPackagesSource”上找不到包“jQuery.3.1.1”。

从错误消息包中可以看到 jquery 无法恢复。但是包 NLog 已成功恢复。我没有解释这种行为,因为这两个包jqueryNLog在本地包存储库中。

此示例的所有文件都可以在这里找到: https ://github.com/ezeh2/nuget_restore_examples/tree/master/nuget_with_local_nuget_repository

标签: nuget-packagenuget-package-restore

解决方案


仔细查看您的包存储库。-packageNLog直接在下面NuGetPackagesSource ,而jquery-package在 path 中jquery/3.1.1/jquery.3.1.1.nupkg。您对包NLogJquery. 如果nuget直接在下面看到一个 nupkg 文件,NuGetPackagesSource它会假定所有 nupkg 文件都在同一级别上,并且它不会在下面的文件夹中搜索NuGetPackagesSource。这就是为什么会出现错误Unable to find version '3.1.1' of package 'jQuery',即使jquery-package 在那里。

如何解决此问题

摆脱您的NuGetPackagesSource-folder 并从头开始重新构建它,nuget install如下所示:

nuget install jQuery-Version 3.3.1 -OutputDirectory NuGetPackagesSource

nuget install NLog-Version 4.3.10 -OutputDirectory NuGetPackagesSource

替代解决方案

将所有 nupkg 文件直接放在 NuGetPackagesSource 下,根本不要使用 nuget install。但这仅适用于没有额外文件夹的简单包。不幸的是,这不能用NLogand来完成JQuery


推荐阅读