dotnet 生成dotnet build

本文适用于:✓ .NET Core 1.x SDK 及更高版本This article applies to: ✓ .NET Core 1.x SDK and later versions

nameName

dotnet build - 生成项目及其所有依赖项。dotnet build - Builds a project and all of its dependencies.

摘要Synopsis

dotnet build [<PROJECT>|<SOLUTION>] [-c|--configuration] [-f|--framework] [--force] [--interactive] [--no-dependencies]
    [--no-incremental] [--nologo] [--no-restore] [-o|--output] [-r|--runtime] [-v|--verbosity] [--version-suffix]

dotnet build [-h|--help]

说明Description

dotnet build 命令将项目及其依赖项生成为一组二进制文件。The dotnet build command builds the project and its dependencies into a set of binaries. 二进制文件包括中间语言 (IL) 文件(带 .dll 扩展名)和用于调试的符号文件(带 .pdb 扩展名)中的项目的代码。The binaries include the project's code in Intermediate Language (IL) files with a .dll extension and symbol files used for debugging with a .pdb extension. 生成依赖项 JSON 文件 (.deps.json ),该文件列出了应用程序的依赖项。A dependencies JSON file (.deps.json) is produced that lists the dependencies of the application. 生成 .runtimeconfig.json 文件,该文件指定应用程序的共享运行时及其版本。A .runtimeconfig.json file is produced, which specifies the shared runtime and its version for the application.

如果项目有第三方依赖项(如来自 NuGet 的库),将从 NuGet 缓存解析这些依赖项,并且它们不适用于项目的生成输出。If the project has third-party dependencies, such as libraries from NuGet, they're resolved from the NuGet cache and aren't available with the project's built output. 考虑到这一点,dotnet build 的产品还未准备好转移到另一台计算机进行运行。With that in mind, the product of dotnet build isn't ready to be transferred to another machine to run. 这与 .NET Framework 的行为相反,后者在构建可执行项目(一个应用程序)时,将生成可在任何已安装 .NET Framework 的计算机上运行的输出。This is in contrast to the behavior of the .NET Framework in which building an executable project (an application) produces output that's runnable on any machine where the .NET Framework is installed. 为了能够获得相似的 .NET Core 使用体验,需要使用 dotnet publish 命令。To have a similar experience with .NET Core, you need to use the dotnet publish command. 有关详细信息,请参阅 .NET Core 应用程序部署For more information, see .NET Core Application Deployment.

构建需要 project.assets.json 文件,该文件列出了你的应用程序的依赖项。Building requires the project.assets.json file, which lists the dependencies of your application. 此文件在 dotnet restore 执行时创建。The file is created when dotnet restore is executed. 如果资产文件未就位,那么工具将无法解析引用程序集,进而导致错误生成。Without the assets file in place, the tooling can't resolve reference assemblies, which results in errors. 使用 .NET Core 1.x SDK,需要在运行 dotnet build 之前显式运行 dotnet restoreWith .NET Core 1.x SDK, you needed to explicitly run the dotnet restore before running dotnet build. 自 .NET Core 2.0 SDK 起,dotnet restoredotnet build 运行时隐式运行。Starting with .NET Core 2.0 SDK, dotnet restore runs implicitly when you run dotnet build. 若要在运行 build 命令时禁用隐式还原,可以传递 --no-restore 选项。If you want to disable implicit restore when running the build command, you can pass the --no-restore option.

备注

从 .NET Core 2.0 开始,无需运行 dotnet restore,因为它由需有还原的所有命令隐式运行,如 dotnet builddotnet runStarting with .NET Core 2.0, you don't have to run dotnet restore because it's run implicitly by all commands, such as dotnet build and dotnet run, that require a restore to occur. 在执行显式还原有意义的某些情况下,例如 Azure DevOps Services 中的持续集成生成中,或在需要显式控制还原发生时间的生成系统中,它仍然是有效的命令。It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.

以长格式传递命令时,该命令也支持 dotnet restore 选项(例如,--source)。This command also supports the dotnet restore options when passed in the long form (for example, --source). 不支持缩写选项,例如 -sShort form options, such as -s, are not supported.

项目是否可执行由项目文件中的 <OutputType> 属性决定。Whether the project is executable or not is determined by the <OutputType> property in the project file. 以下示例显示生成可执行代码的项目:The following example shows a project that produces executable code:

<PropertyGroup>
  <OutputType>Exe</OutputType>
</PropertyGroup>

若要生成库,请省略 <OutputType> 属性。To produce a library, omit the <OutputType> property. 生成输出中的主要区别在于,针对某个库的 IL DLL 不包含入口点,并且不能执行。The main difference in built output is that the IL DLL for a library doesn't contain entry points and can't be executed.

MSBuildMSBuild

dotnet build 使用 MSBuild 生成项目,因此它支持并行生成和增量生成。dotnet build uses MSBuild to build the project, so it supports both parallel and incremental builds. 有关详细信息,请参阅增量生成For more information, see Incremental Builds.

除其自己的选项外,dotnet build 命令也接受 MSBuild 选项,如用来设置属性的 -p 或用来定义记录器的 -lIn addition to its options, the dotnet build command accepts MSBuild options, such as -p for setting properties or -l to define a logger. 有关这些选项的详细信息,请参阅 MSBuild 命令行参考For more information about these options, see the MSBuild Command-Line Reference. 或者也可以使用 dotnet msbuild 命令。Or you can also use the dotnet msbuild command.

运行 dotnet build 相当于 dotnet msbuild -restore -target:BuildRunning dotnet build is equivalent to dotnet msbuild -restore -target:Build.

自变量Arguments

PROJECT | SOLUTION

要生成的项目或解决方案文件。The project or solution file to build. 如果未指定项目或解决方案文件,MSBuild 会在当前工作目录中搜索文件扩展名以 projsln 结尾的文件并使用该文件。If a project or solution file isn't specified, MSBuild searches the current working directory for a file that has a file extension that ends in either proj or sln and uses that file.

选项Options

  • -c|--configuration {Debug|Release}

    定义生成配置。Defines the build configuration. 默认值为 DebugThe default value is Debug.

  • -f|--framework <FRAMEWORK>

    编译特定框架Compiles for a specific framework. 必须在项目文件中定义该框架。The framework must be defined in the project file.

  • --force

    强制解析所有依赖项,即使上次还原已成功,也不例外。Forces all dependencies to be resolved even if the last restore was successful. 指定此标记等同于删除 project.assets.json 文件 。Specifying this flag is the same as deleting the project.assets.json file. 自 .NET Core 2.0 SDK 起可用。Available since .NET Core 2.0 SDK.

  • -h|--help

    打印出有关命令的简短帮助。Prints out a short help for the command.

  • --interactive

    允许命令停止并等待用户输入或操作。Allows the command to stop and wait for user input or action. 例如,完成身份验证。For example, to complete authentication. 自 .NET Core 3.0 SDK 起可用。Available since .NET Core 3.0 SDK.

  • --no-dependencies

    忽略项目到项目 (P2P) 引用,并仅生成指定的根项目。Ignores project-to-project (P2P) references and only builds the specified root project.

  • --no-incremental

    将生成标记为对增量生成不安全。Marks the build as unsafe for incremental build. 此标记关闭增量编译,并强制完全重新生成项目依赖项关系图。This flag turns off incremental compilation and forces a clean rebuild of the project's dependency graph.

  • --no-logo

    不显示启动版权标志或版权消息。Doesn't display the startup banner or the copyright message. 自 .NET Core 3.0 SDK 起可用。Available since .NET Core 3.0 SDK.

  • --no-restore

    在生成期间不执行隐式还原。Doesn't execute an implicit restore during build. 自 .NET Core 2.0 SDK 起可用。Available since .NET Core 2.0 SDK.

  • -o|--output <OUTPUT_DIRECTORY>

    放置生成二进制文件的目录。Directory in which to place the built binaries. 指定此选项时还需要定义 --frameworkYou also need to define --framework when you specify this option. 如果未指定,则默认路径为 ./bin/<configuration>/<framework>/If not specified, the default path is ./bin/<configuration>/<framework>/.

  • -r|--runtime <RUNTIME_IDENTIFIER>

    指定目标运行时。Specifies the target runtime. 有关运行时标识符 (RID) 的列表,请参阅 RID 目录For a list of Runtime Identifiers (RIDs), see the RID catalog.

  • -v|--verbosity <LEVEL>

    设置 MSBuild 详细级别。Sets the MSBuild verbosity level. 允许使用的值为 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. 默认值为 minimalThe default is minimal.

  • --version-suffix <VERSION_SUFFIX>

    设置生成项目时使用的 $(VersionSuffix) 属性的值。Sets the value of the $(VersionSuffix) property to use when building the project. 这仅在未设置 $(Version) 属性时有效。This only works if the $(Version) property isn't set. 然后,$(Version) 设置为 $(VersionPrefix)$(VersionSuffix) 组合,并用短划线分隔。Then, $(Version) is set to the $(VersionPrefix) combined with the $(VersionSuffix), separated by a dash.

示例Examples

  • 生成项目及其依赖项:Build a project and its dependencies:

    dotnet build
    
  • 使用“发布”配置生成项目及其依赖项:Build a project and its dependencies using Release configuration:

    dotnet build --configuration Release
    
  • 针对特定运行时(本例中为 Ubuntu 18.04)生成项目及其依赖项:Build a project and its dependencies for a specific runtime (in this example, Ubuntu 18.04):

    dotnet build --runtime ubuntu.18.04-x64
    
  • 生成项目,并在还原操作过程中使用指定的 NuGet 包源(.NET Core 2.0 SDK 及更高版本):Build the project and use the specified NuGet package source during the restore operation (.NET Core 2.0 SDK and later versions):

    dotnet build --source c:\packages\mypackages
    
  • 生成项目并设置版本 1.2.3.4 作为使用 -p MSBuild 选项的生成参数:Build the project and set version 1.2.3.4 as a build parameter using the -p MSBuild option:

    dotnet build -p:Version=1.2.3.4