TeamCity的"dotnet恢复"与"nuget恢复" [英] 'dotnet restore' vs. 'nuget restore' with TeamCity
问题描述
我有一个可以使用Visual Studio正确构建的ASP.NET Core项目,但是它不在MSBuild下构建.
它没有找到所有常见的库(系统等).
我正在使用TeamCity,构建过程的一部分是nuget restore
.
我尝试执行与TeamCity相同的步骤,但是使用MSBuild手动进行,但失败了,没有找到库.
我添加了dotnet restore
步骤,然后它起作用了.
那么nuget restore
和dotnet restore
有什么区别?
nuget restore
和dotnet restore
大致相同:它们执行NuGet还原操作.
唯一的区别:dotnet restore
是调用dotnet msbuild /t:Restore
的便捷包装,该包装调用了MSBuild集成的还原.这仅适用于包含NuGet的MSBuild发行版,例如Visual Studio 2017(完整的Visual Studio,构建工具)或Mono 5.2+(=> msbuild /t:Restore
)以及提供此便捷命令的.NET Core SDK.>
目前,有两种方法可以在项目中使用NuGet软件包(实际上有3种方法,但是暂时让我们忽略UWP上的project.json
):
-
packages.config
:引用NuGet包的经典"方式.假设NuGet是一个单独的工具,而MSBuild对NuGet一无所知. NuGet客户端(例如nuget.exe
或与Visual Studio集成的工具)会看到packages.config
文件,并在还原时将引用的程序包下载到本地文件夹中.软件包安装会修改项目以引用此本地文件夹中的资产.因此,对packages.config
项目进行的还原只会下载文件. -
PackageReference
:该项目包含引用NuGet包的MSBuild项目.与packages.config
不同,仅列出直接依赖项,并且项目文件不直接引用包中的任何资产(DLL文件,内容文件).还原时,NuGet通过评估直接和传递依赖关系来找出依赖关系图,确保将所有软件包下载到用户的全局软件包缓存中(不是本地解决方案,因此仅下载一次),并将资产文件写入文件夹,其中包含项目使用的所有软件包和资产的列表,以及其他MSBuild目标(如果任何软件包包含需要添加到项目的构建逻辑).因此,如果NuGet还原尚未在全局缓存中,它们可能会下载它们并创建此资产文件.除了程序包引用之外,该项目还可以引用CLI工具,它们是NuGet程序包,其中包含其他命令,这些命令可用于项目目录中的 dotnet
.
msbuild集成的还原仅适用于PackageReference
类型的项目(默认情况下为.NET Standard,.NET Core,但对于任何.NET项目都是选择加入),不适用于packages.config
项目.如果您使用的是nuget.exe
的新版本(例如4.3.0),则可以还原两种项目类型.
您关于丢失类型的错误更有趣:参考程序集"(作为输入传递给编译器的库)未安装在系统上,而是通过NuGet包来提供的.因此,只要全局软件包高速缓存中缺少NuGet软件包或还原操作未生成obj/project.assets.json
文件,编译器将无法使用System.Object
之类的基本类型.
I have an ASP.NET Core project that builds properly with Visual Studio, but it doesn't build under 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
.
I tried to do the same steps as TeamCity, but manually with MSBuild, and it failed, not finding the libraries.
I added a dotnet restore
step and then it worked.
So, what is the difference between a nuget restore
and a dotnet restore
?
Both nuget restore
and dotnet restore
are roughly the same: They perform a NuGet restore operation.
The only difference: dotnet restore
is a convenience wrapper to invoke dotnet msbuild /t:Restore
which 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.
At the moment, there are two ways of how NuGet packages can be used in projects (three actually, but let's ignore project.json
on UWP for the moment):
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 asnuget.exe
or Visual Studio-integrated tooling sees thepackages.config
file 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 apackages.config
project only downloads the files.PackageReference
: The project contains MSBuild items that reference a NuGet package. Unlikepackages.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 theobj
folder 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 thedotnet
in the project directory.
The msbuild-integrated restore only works for PackageReference
type projects (.NET Standard, .NET Core by default, but it is opt-in for any .NET project) and not for packages.config
projects. If you use a new version of nuget.exe
(e.g. 4.3.0), it is able to restore both project types.
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.json
file has not been generated by a restore operation, fundamental types like System.Object
will not be available to the compiler.
这篇关于TeamCity的"dotnet恢复"与"nuget恢复"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!