简体   繁体   English

Visual Studio - 如何自动化构建过程?

[英]Visual Studio - how to automate build process?

I have a C++ solution with many projects. 我有一个包含许多项目的C ++解决方案。 During compilation I need to build part of projects in Win32 mode. 在编译期间,我需要在Win32模式下构建部分项目。 Then execute several shell commands. 然后执行几个shell命令。 Then build several projects in x64 mode. 然后在x64模式下构建几个项目。 Then execute several more shell commands. 然后再执行几个shell命令。 Then, finally, build one more project in Win32 mode. 然后,最后,在Win32模式下再构建一个项目。

I'm tired to do this again and again manually ) How I could automate all these steps? 我已经厌倦了一次又一次地手动执行此操作)如何自动完成所有这些步骤?

I use Visual Studio 2015 我使用Visual Studio 2015

I would suggest a combination of MSBuild XML automation and Jenkins. 我建议将MSBuild XML自动化和Jenkins结合使用。 In detail : 详细地 :

MSBuild : It comes with Visual Studio. MSBuild:它附带Visual Studio。 You need to access VS command prompt to access it. 您需要访问VS命令提示符才能访问它。 In VS command prompt, if you type "MSBuild your_project.vcproj" it will build it for you. 在VS命令提示符下,如果您键入“MSBuild your_project.vcproj”,它将为您构建它。

MSBuild XML Files : You can create a simple XML file and pass it to MSBuild. MSBuild XML文件:您可以创建一个简单的XML文件并将其传递给MSBuild。 This will greatly help you to organize your build process. 这将极大地帮助您组织构建过程。 It is a large topic , however, you can think of it as Microsoft style makefile/build system ( even though Microsoft has nmake ) and much more convenient. 这是一个很大的主题,但是,您可以将其视为Microsoft样式的makefile / build系统(即使Microsoft具有nmake)并且更加方便。 You can define targets, set variables , put conditions , parallelize builds, set actions in order to call shell commands. 您可以定义目标,设置变量,放置条件,并行化构建,设置操作以调用shell命令。 A very simple one looks like : 一个非常简单的看起来像:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <CPPProject>your_cpp_project.sln</CPPProject>
    <powershell_execute>powershell.exe -executionpolicy bypass -command</powershell_execute>
    <UtilityScript>utility.ps1</UtilityScript>
  </PropertyGroup>

  <Target Name="Build">
    <Exec Command="msbuild.exe $(CPPProject) /p:Configuration=Release /p:Platform=Win32" />
    <Exec Command="msbuild.exe $(CPPProject) /p:Configuration=Release_x64 /p:Platform=x64" />
    <Exec Command="$(powershell_execute) $(UtilityScript) args" />
  </Target>

    <Target Name="Clean">
        <!-- DO YOUR CLEANING HERE-->
  </Target>

</Project>

Jenkins : Once you install it , it will run as a Windows service which has its embedded HTTP server. Jenkins:安装后,它将作为Windows服务运行,该服务具有嵌入式HTTP服务器。 You can quickly access it with your browser via localhost:8080. 您可以通过localhost:8080快速访问它。 Jenkins is another topic on its own , but I find it very convenient to automate commands and organize them as job definitions. Jenkins本身就是另一个话题,但我发现自动化命令并将它们组织为作业定义非常方便。

Extra sauce ( Powershell & MSBuild extensions ) : As you can call commands from either MSBuild or Jenkins, you can always Powershell ( in which you can also inline C# code if necessary) I have found this useful where I needed to implement custom logics quickly and easily such as incrementing minor versions before each release etc. Note that there are also extension packs for enriching MSBuild XMLs` functionality. 额外的酱(Powershell和MSBuild扩展):你可以从MSBuild或Jenkins调用命令,你可以随时使用Powershell(如果需要你也可以在其中内联C#代码)我发现这很有用,我需要快速实现自定义逻辑和很容易,例如在每次发布之前递增次要版本等。请注意,还有用于丰富MSBuild XMLs功能的扩展包。

Downsides of Jenkins and TFS as an alternative : Jenkins is generally very convenient. Jenkins和TFS作为替代方案的缺点: Jenkins通常非常方便。 The main downside on Windows is that it was not straightforward to colorize console outputs , however, it can be dealt with. Windows的主要缺点是控制台输出的颜色并不简单,但可以处理它。 In general, I have found it very stable. 总的来说,我发现它非常稳定。 On the other hand, TFS already is a Microsoft solution , I am not sure about its licensing options but it can also be an alternative to Jenkins. 另一方面,TFS已经是微软的解决方案,我不确定它的许可选项,但它也可以替代Jenkins。 In that case , you will be relying more on MSBuild XML file. 在这种情况下,您将更多地依赖于MSBuild XML文件。 One of the downsides of TFS 2013 for me was , it was not always working accurately in over network operations due to some conflicts with Microsoft`s http.sys , which was quite disappointing. TFS 2013对我来说的一个缺点是,由于与微软的http.sys存在一些冲突,因此在网络运营中并不总能正常工作,这令人非常失望。 However, I have not checked TFS2015 但是,我还没有检查过TFS2015

General suggestion : As it is a central solution , I`d highly recommend implementing most of your logic in an MSBuild XML file as this will make your build system also easily invokable via command line and also easy to switch between different systems like Jenkins , TFS or others 一般建议:由于它是一个核心解决方案,我强烈建议在MSBuild XML文件中实现大部分逻辑,因为这将使您的构建系统也可以通过命令行轻松调用,并且可以在Jenkins,TFS等不同系统之间轻松切换或其他人

Other notes : 其他说明:

  1. If it is only MSVC 2015 , you might avoid installing VisualStudio as MSVC2015 has a standalone build : https://blogs.msdn.microsoft.com/vcblog/2015/11/02/announcing-visual-c-build-tools-2015-standalone-c-tools-for-build-environments/ 如果它只是MSVC 2015,您可能会避免安装VisualStudio,因为MSVC2015具有独立版本: https ://blogs.msdn.microsoft.com/vcblog/2015/11/02/announcing-visual-c-build-tools-2015 -standalone-C-工具换集结环境/

  2. If you go Jenkins way, the very first thing you need to handle is accessing to Visual Studio command prompt. 如果你采用Jenkins方式,首先需要处理的是访问Visual Studio命令提示符。 In that case see this : How do I write a build batch script that runs vcvars32.bat, and then continues with the build? 在这种情况下,请参阅: 如何编写运行vcvars32.bat的构建批处理脚本,然后继续构建?

For the same task I've configured local build server with use of TeamCity . 对于相同的任务,我使用TeamCity配置了本地构建服务器。 It perfectly fulfills your requirements. 它完全符合您的要求。 Here is the instruction for how to create build configuration. 下面是说明如何创建构建配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM