Visual Studio 生成任务Visual Studio Build task

Azure Pipelines | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2015Azure Pipelines | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2015

备注

在 Microsoft Team Foundation Server (TFS) 2018 和更低版本中,生成和发布管道被称为“定义”,运行被称为“生成”,服务连接被称为“服务终结点”,阶段被称为“环境”,而作业被称为“阶段” 。In Microsoft Team Foundation Server (TFS) 2018 and previous versions, build and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

使用此任务通过 MSBuild 进行生成,并设置 Visual Studio 版本属性。Use this task to build with MSBuild and set the Visual Studio version property.

需求Demands

msbuild,visualstudiomsbuild, visualstudio

Azure Pipelines: 如果你的团队想要将 Visual Studio 2017 与 Microsoft 托管的代理配合使用,请选择 " 托管 VS2017 " 作为默认的生成池。Azure Pipelines: If your team wants to use Visual Studio 2017 with the Microsoft-hosted agents, select Hosted VS2017 as your default build pool. 请参阅 Microsoft 托管的代理See Microsoft-hosted agents.

YAML 代码段YAML snippet

# Visual Studio build
# Build with MSBuild and set the Visual Studio version property
- task: VSBuild@1
  inputs:
    #solution: '**\*.sln' 
    #vsVersion: 'latest' # Optional. Options: latest, 16.0, 15.0, 14.0, 12.0, 11.0
    #msbuildArgs: # Optional
    #platform: # Optional
    #configuration: # Optional
    #clean: false # Optional
    #maximumCpuCount: false # Optional
    #restoreNugetPackages: false # Optional
    #msbuildArchitecture: 'x86' # Optional. Options: x86, x64
    #logProjectEvents: true # Optional
    #createLogFile: false # Optional
    #logFileVerbosity: 'normal' # Optional. Options: quiet, minimal, normal, detailed, diagnostic

参数Arguments

参数Argument 说明Description
solution
解决方案Solution

(必需的) 如果要生成单个解决方案,请单击 " ... " 按钮,然后选择解决方案。(Required) If you want to build a single solution, click the ... button and select the solution.

如果要生成多个解决方案,请指定搜索条件。If you want to build multiple solutions, specify search criteria. 你可以使用 () 的单文件夹通配符 `*` 和递归通配符 (`**`) 。You can use a single-folder wildcard (`*`) and recursive wildcards (`**`). 例如, `**.sln` 搜索所有子目录中的所有 .sln 文件。For example, `**.sln` searches for all .sln files in all subdirectories.

请确保此生成管道下载了你指定的解决方案。Make sure the solutions you specify are downloaded by this build pipeline. 在 "存储库" 选项卡上:On the Repository tab:

  • 如果使用 TFVC,请确保解决方案是 "存储库" 选项卡上某个映射的子解决方案。If you use TFVC, make sure that the solution is a child of one of the mappings on the Repository tab.
  • 如果使用 Git,请确保项目或解决方案位于你的 Git 存储库中,并且位于你'重新生成的分支中。If you use Git, make sure that the project or solution is in your Git repo, and in a branch that you're building.

提示:Tips:

  • 还可以 *proj) 文件生成 MSBuild 项目 (。You can also build MSBuild project (.*proj) files.
  • 如果要生成自定义的 MSBuild 项目文件,建议使用 MSBuild 任务 而不是 Visual Studio 生成任务。If you are building a customized MSBuild project file, we recommend you use the MSBuild task instead of the Visual Studio Build task.

默认值: \* \* \* .slnDefault value: \*\*\*.sln
vsVersion
Visual Studio 版本Visual Studio Version

若要避免整体出现问题,必须确保此值与用于创建解决方案的 Visual Studio 的版本相匹配。To avoid problems overall, you must make sure this value matches the version of Visual Studio used to create your solution.

此处选择的值会将 /p:VisualStudioVersion={numeric_visual_studio_version} 参数添加到生成运行的 MSBuild 命令。The value you select here adds the /p:VisualStudioVersion={numeric_visual_studio_version} argument to the MSBuild command run by the build. 例如,如果选择 " Visual Studio 2015", /p:VisualStudioVersion=14.0 则会将添加到 MSBuild 命令。For example, if you select Visual Studio 2015, /p:VisualStudioVersion=14.0 is added to the MSBuild command.

Azure Pipelines:如果你的团队想要将 Visual Studio 2017 与 Microsoft 托管的代理配合使用,请选择 " 托管 VS2017 " 作为默认的生成池。Azure Pipelines:If your team wants to use Visual Studio 2017 with the Microsoft-hosted agents, select Hosted VS2017 as your default build pool. 请参阅 Microsoft 托管的代理See Microsoft-hosted agents.


默认值:最新Default value: latest
msbuildArgs
MSBuild 参数MSBuild Arguments
(可选) 可以将其他参数传递给 MSBuild。(Optional) You can pass additional arguments to MSBuild. 有关语法,请参阅 MSBuild Command-Line 引用For syntax, see MSBuild Command-Line Reference.
platform
平台Platform

(可选) 指定要生成的平台 Win32 ,例如、 x86 x64any cpu(Optional) Specify the platform you want to build such as Win32, x86, x64 or any cpu.

提示:Tips:

  • 如果以 MSBuild 项目为目标 ( *proj) 文件而不是解决方案,请指定 AnyCPU (无空格) 。If you are targeting an MSBuild project (.*proj) file instead of a solution, specify AnyCPU (no whitespace).
  • BuildPlatform在 "变量" 选项卡上声明一个生成变量, (选择 "在队列时间允许") 并在此处引用为 $(BuildPlatform)Declare a build variable such as BuildPlatform on the Variables tab (selecting Allow at Queue Time) and reference it here as $(BuildPlatform). 这样一来,您可以在对生成进行排队时修改平台并启用生成多个配置。This way you can modify the platform when you queue the build and enable building multiple configurations.
configuration
配置Configuration

(可选) 指定要生成的配置,例如 debugrelease(Optional) Specify the configuration you want to build such as debug or release.

提示: BuildConfiguration 在 "变量" 选项卡上声明生成变量, (选择 "在队列时间允许") 并在此处引用为 $(BuildConfiguration)Tip: Declare a build variable such as BuildConfiguration on the Variables tab (selecting Allow at Queue Time) and reference it here as $(BuildConfiguration). 这样一来,您可以在对生成进行排队时修改平台并启用生成多个配置。This way you can modify the platform when you queue the build and enable building multiple configurations.

clean
cleanClean

如果要使其成为增量生成, (可选) 设置为 False。(Optional) Set to False if you want to make this an incremental build. 此设置可能会缩短生成时间,尤其是在基本代码较大的情况下。This setting might reduce your build time, especially if your codebase is large. 除非您还将“干净的存储库”设置为“False”,否则此选项没有实用效果。This option has no practical effect unless you also set Clean repository to False.

若要重新生成代码项目中的所有代码,请将其设置为“True”。Set to True if you want to rebuild all the code in the code projects. 这等效于 MSBuild /target:clean 参数。This is equivalent to the MSBuild /target:clean argument.

高级Advanced
maximumCpuCount
并行生成Build in Parallel
(可选) 如果 MSBuild 目标配置与并行生成兼容,则可以选择选中此输入以将/m 开关传递到 MSBuild (仅) 限 Windows。(Optional) If your MSBuild target configuration is compatible with building in parallel, you can optionally check this input to pass the /m switch to MSBuild (Windows only). 如果目标配置与并行生成不兼容,则选中此选项可能会导致您的生成出现文件使用中的错误,或者间歇或不一致的生成失败。If your target configuration is not compatible with building in parallel, checking this option may cause your build to result in file-in-use errors, or intermittent or inconsistent build failures.
默认值: falseDefault value: false
restoreNugetPackages
还原 NuGet 包Restore NuGet Packages
(重要) 不推荐使用此选项。(Important) This option is deprecated. 请确保清除此复选框,而改用 NuGet 安装程序 生成任务。Make sure to clear this checkbox and instead use the NuGet Installer build task.
默认值: falseDefault value: false
msbuildArchitecture
MSBuild 体系结构MSBuild Architecture

根据需要提供要运行的 MSBuild (x86、x64) Optionally supply the architecture (x86, x64) of MSBuild to run

提示:由于 Visual Studio 作为32位应用程序运行,因此在运行 Team Foundation 生成服务的64位版本的生成代理处理您的生成时,您可能会遇到问题。Tip: Because Visual Studio runs as a 32-bit application, you could experience problems when your build is processed by a build agent that is running the 64-bit version of Team Foundation Build Service. 通过选择 MSBuild x86,可以解决这些类型的问题。By selecting MSBuild x86, you might resolve these kinds of problems.


默认值: x86Default value: x86
logProjectEvents
记录项目详细信息Record Project Details
选择性地记录每个项目的时间线详细信息Optionally record timeline details for each project
默认值: trueDefault value: true
createLogFile
创建日志文件Create Log File
(可选)创建日志文件 (仅限 Windows) Optionally create a log file (Windows only)
默认值: falseDefault value: false
logFileVerbosity
日志文件详细级别Log File Verbosity
可选的日志文件详细级别Optional log file verbosity
默认值:正常Default value: normal
控制选项Control options

开源Open source

此任务是 GitHub 上的开放源代码。This task is open source on GitHub. 欢迎提供反馈和发布内容。Feedback and contributions are welcome.

常见问题解答FAQ

我应该使用 Visual Studio 生成任务还是 MSBuild 任务?Should I use the Visual Studio Build task or the MSBuild task?

如果你正在构建解决方案,则在大多数情况下,你应该使用 Visual Studio 生成任务If you are building a solution, in most cases you should use the Visual Studio Build task. 此任务自动:This task automatically:

  • /p:VisualStudioVersion为您设置属性。Sets the /p:VisualStudioVersion property for you. 这会强制 MSBuild 使用一组特定目标,从而增加成功生成的可能性。This forces MSBuild to use a particular set of targets that increase the likelihood of a successful build.

  • 指定 MSBuild 版本参数。Specifies the MSBuild version argument.

在某些情况下,可能需要使用 MSBuild 任务In some cases you might need to use the MSBuild task. 例如,如果您要构建除解决方案外的代码项目,则应使用此方法。For example, you should use it if you are building code projects apart from a solution.

在哪里可以了解有关 MSBuild 的更多信息?Where can I learn more about MSBuild?

MSBuild 任务MSBuild task

MSBuild 参考MSBuild reference

MSBuild 命令行参考MSBuild command-line reference

如何实现为多个平台生成多个配置?How do I build multiple configurations for multiple platforms?

  1. 在 "变量" 选项卡上,确保've 为配置和平台定义了变量。On the Variables tab, make sure you've got variables defined for your configurations and platforms. 若要指定多个值,请用逗号分隔它们。To specify multiple values, separate them with commas.

    例如,对于 .NET 应用程序,可以指定:For example, for a .NET app you could specify:

    名称Name Value
    BuildConfigurationBuildConfiguration 调试、发布debug, release
    BuildPlatformBuildPlatform 任何 cpuany cpu

    例如,对于 c + + 应用程序,可以指定:For example, for a C++ app you could specify:

    名称Name Value
    BuildConfigurationBuildConfiguration 调试、发布debug, release
    BuildPlatformBuildPlatform x86、x64x86, x64
  2. 如果要将作业 (一个) 值组合分配给多个代理(如果有),请在 "选项" 选项卡上选择 " 并行 "。On the Options tab select Parallel if you want to distribute the jobs (one for each combination of values) to multiple agents in parallel if they are available.

  3. 在 "生成" 选项卡上,选择此步骤并指定平台和配置参数。On the Build tab, select this step and specify the Platform and Configuration arguments. 例如:For example:

    • 平台 $(BuildPlatform)Platform: $(BuildPlatform)
    • Configuration $(BuildConfiguration)Configuration: $(BuildConfiguration)
  4. 在分配任务的代理作业下,在 "并行度" 选项卡上,选择 " 多配置 " 并指定用逗号分隔的乘数。Under the agent job of the assigned task, on the Parallelism tab, select Multi-configuration and specify the Multipliers separated by commas. 例如: BuildConfiguration, BuildPlatformFor example: BuildConfiguration, BuildPlatform

是否可以生成 Tfsbuild.proj 文件?Can I build TFSBuild.proj files?

不能生成 Tfsbuild.proj 文件。You cannot build TFSBuild.proj files. 这些类型的文件由 TFS 2005 和2008生成。These kinds of files are generated by TFS 2005 and 2008. 这些文件包含仅使用 XAML 生成支持的任务和目标。These files contain tasks and targets are supported only using XAML builds.

需要使用代理吗?Do I need an agent?

至少需要一个代理才能运行生成或发布。You need at least one agent to run your build or release.

我遇到了问题。I'm having problems. 如何对其进行故障排除?How can I troubleshoot them?

请查看排查生成和发布问题See Troubleshoot Build and Release.

我无法选择默认代理池,无法将生成或发布排队。I can't select a default agent pool and I can't queue my build or release. 如何修复此问题?How do I fix this?

请查看代理池See Agent pools.

我的 NuGet 推送任务失败,出现“错误: 无法获取本地颁发者证书”错误。My NuGet push task is failing with the following error: "Error: unable to get local issuer certificate". 如何解决此问题?How can I fix this?

可通过添加受信任的根证书来解决此问题。This can be fixed by adding a trusted root certificate. 可将环境变量 NODE_EXTRA_CA_CERTS=file 添加到生成代理,也可在管道中添加任务变量 NODE.EXTRA.CA.CERTS=fileYou can either add the NODE_EXTRA_CA_CERTS=file environment variable to your build agent, or you can add the NODE.EXTRA.CA.CERTS=file task variable in your pipeline. 有关更多详细信息,请查看环境变量See Environment variables for more details.

我在本地使用 TFS,但没有看到其中某些功能。I use TFS on-premises and I don't see some of these features. 为什么看不到?Why not?

其中某些功能仅在 Azure Pipelines 上可用,在本地尚不可用。Some of these features are available only on Azure Pipelines and not yet available on-premises. 如果你已升级到最新版本的 TFS,则可在本地使用这些功能。Some features are available on-premises if you have upgraded to the latest version of TFS.