NuGet 的还原包坚持特定的包版本

NuGet#39;s Restore Package insists on specific package versions(NuGet 的还原包坚持特定的包版本)

本文介绍了NuGet 的还原包坚持特定的包版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下 packages.config 的项目:

I've a project with the following packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Framework.Infrastructure.Core" version="1.4.0.6" />
  <package id="Framework.Infrastructure.Extensions" version="1.4.0.6" />
</packages>

Framework.* 包在我们本地存储库中的位置.

Where the Framework.* packages are sitting in our local repository.

我已启用包还原并将我们的内部存储库添加到源中.但是,当我尝试从 packages.config 恢复包时(实际上是 nuget install packages.config -sources....),我收到以下错误:

I've enabled Package Restore and added our internal repo to the sources. However, when I try restoring the packages from packages.config (which essentially does nuget install packages.config -sources....), I get the following errors:

error : Unable to find version '1.4.0.6' of package 'Framework.Infrastructure.Extensions'
error : Unable to find version '1.4.0.6' of package 'Framework.Infrastructure.Core'.

存储库不再包含包的 1.4.0.6 版本(几个月前相关),而是包含它的新版本(例如,1.5.1.6).

The repository no longer contains the 1.4.0.6 version of the package (which was relevant a couple of months ago), but rather the new version of it (e.g., 1.5.1.6).

为什么 NuGet 找不到新版本的包?我可以在 packages.config 中指定一些语法以确保下载最新版本吗?

Why doesn't NuGet find the new versions of the packages? Is there some syntax I can specify in packages.config to ensure that the latest versions will be downloaded?

简而言之,有没有什么可以写一个自定义脚本来更新我能做的包?

In short, is there anything short of writing a custom script to update the packages that I can do?

谢谢.

推荐答案

我认为有些人误解了包还原的目的.将此功能添加到 NuGet 只是为了不需要将包签入版本控制.很多人抱怨提交二进制文件会爆炸性地增加他们的存储库的大小,使用 git 之类的 DVCS 时更糟,其中整个 repo 都在本地下载并包含包 Foo 的每个版本.

I think some people are misunderstanding what Package Restore is meant to do. This feature was added to NuGet solely for the purpose of not requiring packages to be checked into version control. A lot of people were complaining that commiting binaries were exploding the size of their repositories, and is even worse when using a DVCS like git, where the entire repo is downloaded locally and includes every version of package Foo.

那么包还原到底是做什么的呢?基本上,它在每个项目的 packages.config 中查找,并简单地拉下列出的包的特定版本.这就像删除您的包文件夹,然后执行 git reset --hard 将它们带回(假设该文件夹已签入).

So what exactly does Package Restore do? Basically it looks in packages.config of each project and simply pulls down the specific version of the package listed. It's like deleting your packages folder, then doing git reset --hard to bring them back (assuming the folder was checked in).

为什么这很重要?为什么不升级到最新的软件包版本?如果您考虑包还原最常见的用例,即进行自动构建,那应该会给您一个线索.构建服务器应该只构建由开发人员测试和提交的项目.如果您让构建服务器决定何时更新包,那么您的项目还没有经过任何人的测试.作为开发人员,您应该决定何时进行升级.

Why is this important? Why not upgrade to the latest package version? If you consider the most common use case of Package Restore, which is to do automated builds, that should give you a clue. The build server should only be building the project that was tested and committed by a developer. If you let the build server decide when to update a package, then you have a project that has not been tested by anyone. As a developer, you should be the one to decide when to do the upgrade.

请记住,安装或更新软件包不仅仅是拉下 .nupkg 文件并添加引用.许多软件包都有副作用,例如更新 .config 文件、添加代码等.安装软件包时,所有这些副作用都会发生在本地副本上.您现在可以提交代码并排除包文件.

Remember, installing or updating a package is not simply pulling down a .nupkg file and adding references. A lot of packages have side-effects like updating your .config files, adding code, etc. When you install a package, all of those side-effects happen on your local copy. You can now commit your code and exclude the package files.

当另一个开发人员或构建服务器签出代码时,他将获得与您完全相同的副作用代码,但减去了包文件.包还原只是从 NuGet 存储库中提取这些文件,现在我们拥有处理该项目所需的一切.

When another developer or the build server checks out the code, he'll have the exact same side-effect code you had minus the package files. Package Restore simply pulls these files down from the NuGet repository and now we have everything needed to work on this project.

NuGet 团队已承诺维护所有版本的包,以便您始终能够下载正确的版本.然而,正如我们在几个月前看到的那样,当 NuGet 服务器出现故障时,它几乎削弱了包还原,很多人无法构建.

The NuGet team has promised to maintain all versions of packages so that you will always be able to pull down the correct version. However, as we saw a few months ago, when the NuGet server went down, it pretty much crippled Package Restore and a lot of people were unable to build.

我建议您设置自己的 NuGet 存储库(一个简单的文件共享即可)并保留您在其中使用的所有包的副本.这样,您的构建就不会依赖外部服务器.正如 NuGet 团队所做的那样,您应该保留包的所有版本.这样,如果您必须返回并构建项目的旧版本,您将确保有正确的包版本可用.

I recommend that you setup your own NuGet repository (a simple file share would do) and keep copies of all packages you use there. This way you are not dependent on an external server for your builds. And as the NuGet team does, you should keep ALL versions of a package. This way if you have to go back and build an older version of your project, you will be sure to have the correct package versions available.

我希望这能解释该功能的工作原理以及它为何如此工作.

I hope this explains how the feature works and why it works that way.

这篇关于NuGet 的还原包坚持特定的包版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:NuGet 的还原包坚持特定的包版本

基础教程推荐