TeamCity 的“dotnet restore”与“nuget restore”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45897271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:58:14  来源:igfitidea点击:

'dotnet restore' vs. 'nuget restore' with TeamCity

.netmsbuildnugetteamcity

提问by Thomas

I have an ASP.NET Core project that builds properly with Visual Studio, but it doesn't build under MSBuild.

我有一个使用 Visual Studio 正确构建的 ASP.NET Core 项目,但它不是在 MSBuild 下构建的。

It doesn't find all the common libraries (system, etc.).

它没有找到所有的公共库(系统等)。

I'm using TeamCity and part of the build process is a nuget restore.

我正在使用 TeamCity,构建过程的一部分是nuget restore.

I tried to do the same steps as TeamCity, but manually with MSBuild, and it failed, not finding the libraries.

我尝试执行与 TeamCity 相同的步骤,但使用 MSBuild 手动执行,但失败了,未找到库。

I added a dotnet restorestep and then it worked.

我添加了一个dotnet restore步骤,然后它起作用了。

So, what is the difference between a nuget restoreand a dotnet restore?

那么,anuget restore和 a之间有什么区别dotnet restore

回答by Martin Ullrich

Both nuget restoreand dotnet restoreare roughly the same: They perform a NuGet restore operation.

双方nuget restoredotnet restore大致相同:他们执行的NuGet恢复操作。

The only difference: dotnet restoreis a convenience wrapper to invoke dotnet msbuild /t:Restorewhich invokes an MSBuild-integrated restore. This only works on MSBuild distributions that include NuGet, such as Visual Studio 2017 (full Visual Studio, build tools) or Mono 5.2+ (=> msbuild /t:Restore) and the .NET Core SDK which provides this convenience command.

唯一的区别是:dotnet restore是一个方便的包装器,dotnet msbuild /t:Restore用于调用 MSBuild 集成的还原。这仅适用于包含 NuGet 的 MSBuild 发行版,例如 Visual Studio 2017(完整的 Visual Studio,构建工具)或 Mono 5.2+ (=> msbuild /t:Restore) 以及提供此便利命令的 .NET Core SDK。

At the moment, there are two ways of how NuGet packages can be used in projects (three actually, but let's ignore project.jsonon UWP for the moment):

目前,有两种方式可以在项目中使用 NuGet 包(实际上是三种,但我们project.json暂时忽略UWP):

  • packages.config: The "classic" way of referencing NuGet packages. This assumes NuGet is a separate tool and MSBuild doesn't know anything about NuGet. A NuGet client such as nuget.exeor Visual Studio-integrated tooling sees the packages.configfile and downloads the referenced packages into a local folder on restore. A package install modifies the project to reference assets out of this local folder. So a restore for a packages.configproject only downloads the files.
  • PackageReference: The project contains MSBuild items that reference a NuGet package. Unlike packages.config, only the direct dependencies are listed and the project file does not directly reference any assets (DLL files, content files) out of packages. On restore, NuGet figures out the dependency graph by evaluating the direct and transitive dependencies, makes sure all packages are downloaded into the user's global package cache (not solution-local so it is only downloaded once) and write an assets file into the objfolder that contains a list of all packages and assets that the project uses, as well as additional MSBuild targets if any package contains build logic that needs to be added to a project. So a NuGet restore may download packages if they are not already in the global cache and create this assets file. In addition to package references, the project can also reference CLI tools, which are NuGet packages containing additional commands that will be available for the dotnetin the project directory.
  • packages.config:引用 NuGet 包的“经典”方式。这假设 NuGet 是一个单独的工具,并且 MSBuild 对 NuGet 一无所知。NuGet 客户端(例如nuget.exeVisual Studio 集成工具)会查看该packages.config文件并将引用的包下载到本地文件夹中。包安装修改项目以引用此本地文件夹之外的资产。因此,packages.config项目的还原仅下载文件。
  • PackageReference:该项目包含引用 NuGet 包的 MSBuild 项。与 不同的是packages.config,只列出了直接依赖项,项目文件不直接引用包外的任何资产(DLL 文件、内容文件)。还原时,NuGet 通过评估直接依赖和传递依赖来计算依赖图,确保所有包都下载到用户的全局包缓存中(不是本地解决方案,因此只下载一次)并将资产文件写入obj包含项目使用的所有包和资产列表的文件夹,以及其他 MSBuild 目标(如果任何包包含需要添加到项目的构建逻辑)。因此,如果包不在全局缓存中,NuGet 还原可能会下载包并创建此资产文件。除了包引用之外,项目还可以引用 CLI 工具,这些工具是 NuGet 包,其中包含可用于dotnet项目目录中的其他命令。

The msbuild-integrated restore only works for PackageReferencetype projects (.NET Standard, .NET Core by default, but it is opt-in for any .NET project) and not for packages.configprojects. If you use a new version of nuget.exe(e.g. 4.3.0), it is able to restore both project types.

msbuild 集成还原仅适用于PackageReference类型项目(默认情况下为.NET Standard、.NET Core,但它可以选择用于任何 .NET 项目),不适用于packages.config项目。如果您使用新版本nuget.exe(例如 4.3.0),它能够恢复两种项目类型。

Your error about missing types is a bit more interesting: The "reference assemblies" (libraries that are passed as input to the compiler) are not installed on the system but come via NuGet packages. So as long as the NuGet packages are missing from the global package cache or the obj/project.assets.jsonfile has not been generated by a restore operation, fundamental types like System.Objectwill not be available to the compiler.

您关于缺少类型的错误更有趣:“引用程序集”(作为输入传递给编译器的库)未安装在系统上,而是通过 NuGet 包来的。因此,只要全局包缓存中缺少 NuGet 包或obj/project.assets.json文件尚未通过还原操作生成,System.Object编译器将无法使用基本类型(例如)。

回答by hsop

I had similar problems with a .NET Core 2 project that would build just fine on my workstation - both within Visual Studio 2017 and using MSBuild only - but did not build in TeamCity. The error message was:

我在 .NET Core 2 项目上遇到了类似的问题,该项目在我的工作站上构建得很好——无论是在 Visual Studio 2017 中还是仅使用 MSBuild——但没有在 TeamCity 中构建。错误消息是:

C:\Program Files\dotnet\sdk.1.4\Sdks\Microsoft.NET.Sdk\build\Microsoft.PackageDependencyResolution.targets(327, 5):
Assets file 'D:\TeamCity\buildAgent\work6486b1d4e7a8e7\Source\Integrations\SomeAPI\obj\project.assets.json' not found.
Run a NuGet package restore to generate this file.

In my build configuration I already had a NuGet install step before the build step:

在我的构建配置中,我在构建步骤之前已经有一个 NuGet 安装步骤:

  • NuGetversion: 3.4.4
  • Restore Mode: Install
  • NuGet 版本:3.4.4
  • 恢复模式:安装

It turned out I had to use:

结果我不得不使用:

  • NuGet version: 4.0.0 or higher
  • Restore Mode: Restore (Requires NuGet 2.7+)
  • NuGet 版本:4.0.0 或更高
  • 还原模式:还原(需要 NuGet 2.7+)

回答by Exonto

nuget restorewill ensure all of your NuGet dependencies are downloaded and available to your project. Whereas dotnet restoreis a complete restoration of all NuGet dependencies as well as references and project specific tools. Meaning that if you run nuget restore, you are onlyrestoring NuGet packages.

nuget restore将确保您的所有 NuGet 依赖项都已下载并可用于您的项目。而dotnet restore完全恢复所有 NuGet 依赖项以及引用和项目特定工具。这意味着如果您运行nuget restore,您只是在还原 NuGet 包。

According to docs.microsoft.com: Dotnet Restore

根据 docs.microsoft.com:Dotnet还原

The dotnet restorecommand uses NuGet to restore dependencies as well as project-specific tools that are specified in the project file...

dotnet restore命令使用 NuGet 来恢复依赖项以及项目文件中指定的项目特定工具...