简体   繁体   English

如何自动化Visual Studio构建?

[英]How do you automate a Visual Studio build?

如何将在IDE中执行的Visual Studio构建转换为可以从命令行运行的脚本?

\Windows\Microsoft.NET\Framework\[YOUR .NET VERSION]\msbuild.exe

很多命令行参数,但最简单的只是:

msbuild.exe yoursln.sln

使用VS2008,您可以这样做:

devenv solution.sln /build configuration

Simplest way: navigate to the directory containing the solution or project file, and run msbuild (assuming you have Visual Studio 2005 or newer). 最简单的方法:导航到包含解决方案或项目文件的目录,然后运行msbuild (假设您有Visual Studio 2005或更新版本)。

More flexible ways: 更灵活的方式:

  • Read up on the MSBuild reference . 阅读MSBuild参考资料 There are tons of customization, especially once you've installed the MSBuild Community Tasks Project . 有大量的自定义,特别是一旦安装了MSBuild社区任务项目
  • Use NAnt . 使用NAnt It has existed for longer than MSBuild and has more community support, but requires you to start a project file from scratch, rather than extending the existing, Visual Studio-created one. 它存在的时间比MSBuild长,并且有更多的社区支持,但要求您从头开始创建项目文件,而不是扩展Visual Studio创建的现有文件。

NAntMSBuild是自动化.NET构建的最流行的工具,你可以在Stack Overflow问题Best .NET构建工具中找到关于每个的优点/缺点的讨论。

Look into build tool NAnt or MSBuild . 查看构建工具NAntMSBuild I believe MSBuild is the build tool for Visual Studio 2005 and later. 我相信MSBuild是Visual Studio 2005及更高版本的构建工具。 I am, however, a fan of NAnt... 然而,我是NAnt的粉丝......

Here is the script I'm using to completely automate the command line build of x86 AND x64 configurations for the same solution through batch scripts. 这是我用来通过批处理脚本为同一解决方案完全自动化x86和x64配置的命令行构建的脚本。

This is based on DevEnv.exe as it works if you have a Setup project in your build (msbuild doesn't support building Setup projects). 这是基于DevEnv.exe,因为它在您的构建中有一个安装项目(msbuild不支持构建安装项目)。

I'm assuming your setup is 32bit Windows 7 with Visual Studio 2010 setup using the x86 native compiler and x64 cross compiler. 我假设您的设置是32位Windows 7,使用x86本机编译器和x64交叉编译器进行Visual Studio 2010安装。 If you're running 64bit windows you may need to change x86_amd64 to amd64 in the batch script depending on your setup. 如果您运行的是64位Windows,则可能需要在批处理脚本中将x86_amd64更改为amd64 ,具体取决于您的设置。 This is assuming Visual Studio is installed in Program Files and your solution is located in D:\\MySoln 这假设Visual Studio安装在Program Files中 ,您的解决方案位于D:\\ MySoln中

Create a file called buildall.bat and add this to it: 创建一个名为buildall.bat的文件并将其添加到其中:

D:
cd "D:\MySoln"

if "%1" == "" goto all
if %1 == x86 goto x86
if %1 == x64 goto x64

:x86
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86 < crosscompilex86.bat
goto eof

:x64
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86_amd64 < crosscompilex64.bat
goto eof

:all
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86 < crosscompilex86.bat
if %ERRORLEVEL% NEQ 0 goto eof
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86_amd64 < crosscompilex64.bat
goto eof

:eof
pause

Now create 2 more batch scripts: 现在再创建2个批处理脚本:

crosscompilex86.bat to build the Release version of a x86 build and include this crosscompilex86.bat用于构建x86版本的Release版本并包含此内容

devenv MySoln.sln /clean "Release|x86"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
devenv MySoln.sln /rebuild "Release|x86"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%

crosscompilex64.bat to build the Release version of the x64 build and include this crosscompilex64.bat用于构建x64版本的Release版本并包含此内容

devenv MySoln.sln /clean "Release|x64"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
devenv MySoln.sln /rebuild "Release|x64"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%

Now place all 3 batch files along in your solution folder along with MySoln.sln. 现在将所有3个批处理文件与MySoln.sln一起放在解决方案文件夹中。 You can build both x86 and x64 Release versions by creating a Shortcut on your desktop which run the following commands: 您可以通过在桌面上创建一个运行以下命令的快捷方式来构建x86和x64 Release版本:

  • Build All -> D:\\MySoln\\buildall.bat 全部构建 - > D:\\ MySoln \\ buildall.bat
  • Build x86 Release Only -> D:\\MySoln\\buildall.bat x86 仅构建x86版本 - > D:\\ MySoln \\ buildall.bat x86
  • Build x64 Release Only -> D:\\MySoln\\buildall.bat x64 仅构建x64版本 - > D:\\ MySoln \\ buildall.bat x64

If you're using another configuration like AnyCPU etc you would need to customize the above scripts accordingly. 如果您正在使用AnyCPU等其他配置,则需要相应地自定义上述脚本。

Here is my batch file using msbuild for VS 2010 Debug configuration: 这是我使用msbuild for VS 2010 Debug配置的批处理文件:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" 
iTegra.Web.sln /p:Configuration=Debug /clp:Summary /nologo

As of Visual Studio 2005 , all of the project files (at least for .NET based projects) are actual MSBuild files, so you can call MSBuild on the command line and pass it the project file. Visual Studio 2005开始 ,所有项目文件(至少对于基于.NET的项目)都是实际的MSBuild文件,因此您可以在命令行上调用MSBuild并将其传递给项目文件。

The bottom line is that you need to use a "build scripting language" like NAnt or MSBuild (there are others, but these are the mainstream ones right now) if you want to have any real control over your build process. 最重要的是,如果你想对你的构建过程有任何真正的控制,你需要使用像NAnt或MSBuild这样的“构建脚本语言”(还有其他语言,但现在这些是主流的)。

Take a look at UppercuT. 看看UppercuT。 It has a lot of bang for your buck and it does what you are looking for and much more. 它有很大的优势,它可以满足您的需求,也可以满足您的需求。

UppercuT uses NAnt to build and it is the insanely easy to use Build Framework. UppercuT使用NAnt构建,它是一个非常容易使用的Build Framework。

Automated Builds as easy as (1) solution name, (2) source control path, (3) company name for most projects! 自动构建与(1)解决方案名称,(2)源控制路径,(3)大多数项目的公司名称一样简单!

http://projectuppercut.org/ http://projectuppercut.org/

Some good explanations here: UppercuT 这里有一些很好的解释: UppercuT

I had to do this for a C++ project in Visual Studio 2003 so I don't know how relevant this is to later version of visual studio: 我不得不在Visual Studio 2003中为C ++项目执行此操作,因此我不知道这对于Visual Studio的更高版本有多相关:

In the directory where your executable is created there will be a BuildLog.htm file. 在创建可执行文件的目录中,将有一个BuildLog.htm文件。 Open that file in your browser and then for each section such as: 在浏览器中打开该文件,然后为每个部分打开,例如:

Creating temporary file "c:\some\path\RSP00003C.rsp" with contents
[
/D "WIN32" /D "_WINDOWS" /D "STRICT" /D "NDEBUG" ..... (lots of other switches)
.\Project.cpp
.\Another.cpp
.\AndAnother.cpp
".\And Yet Another.cpp"
]
Creating command line "cl.exe @c:\some\path\RSP00003C.rsp /nologo"

create a .rsp file with the content between the square brackets (but not including the square brackets) and call it whatever you like. 创建一个.rsp文件,其中包含方括号之间的内容(但不包括方括号),并随意调用它。 I seem to remember having problems with absolute paths so you may have to make sure all the paths are relative. 我似乎记得绝对路径有问题所以你可能必须确保所有路径都是相对的。

Then in your build script add the command line from the BuildLog.htm file but with your .rsp filename: 然后在构建脚本中添加BuildLog.htm文件中的命令行,但使用.rsp文件名:

cl.exe @autobuild01.rsp /nologo

(note there will also be a link.exe section as well as cl.exe) (注意还会有一个link.exe部分以及cl.exe)

A more simple way is to change VS 2015 Projects & Solutions configuration: Go to the Tools tab -> Options -> Projects and Solutions -> Build and Run -> On Run, when projects are out of date (choose Always build). 更简单的方法是更改​​VS 2015项目和解决方案配置:当项目过期时(选择始终构建),转到工具选项卡 - >选项 - >项目和解决方案 - >构建和运行 - >运行时。 VOILA! 瞧!

Now your IDE will automatically build your project when you run (F5) it. 现在,您的IDE将在您运行(F5)时自动构建项目。 Hope this helps, any feedback are welcome. 希望这有帮助,欢迎任何反馈。

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

相关问题 如何在Visual Studio 2010中自动化构建过程 - How to automate build process in Visual Studio 2010 Visual Studio - 如何自动化构建过程? - Visual Studio - how to automate build process? 如何使用visual studio构建可替换的DLL? - How do you build replaceable DLLs with visual studio? 如何在Visual Studio 2015的构建中排除文件夹? - How do you exclude a folder from build in Visual Studio 2015? 你如何构建像UI这样的Visual Studio? - How do you build a Visual Studio like UI? 如何通过TFS构建管道自动上传Visual Studio画廊 - how to automate upload Visual studio gallery through TFS build pipeline 如何在Visual Studio 2015中自动执行数据流任务? - How do I automate a Data Flow task in Visual Studio 2015? 如何在Visual Studio 2013的每个内部版本中生成新的PDB文件? - How do you generate new PDB files every build in Visual Studio 2013? 您如何仅使用Visual Studio的“命令窗口”构建文件的项目? - How do you only build a file's project using Visual Studio's Command Window? 如何配置Visual Studio自定义构建工具以依赖于大量文件? - How do you a configure a Visual Studio custom build tool to depend on a lot of files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM