visual-studio 如何自动化 Visual Studio 构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:23:29  来源:igfitidea点击:

How do you automate a Visual Studio build?

visual-studiobuild-automation

提问by Sam Hasler

How do you turn a Visual Studio build that you'd perform in the IDE into a script that you can run from the command line?

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

采纳答案by Agnel Kurian

With VS2008 you can do this:

使用 VS2008 你可以这样做:

devenv solution.sln /build configuration

回答by FlySwat

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

Lots of command line parameters, but the simplest is just:

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

msbuild.exe yoursln.sln

回答by alanl

NAntand MSBuildare the most popular tools to automate your build in .NET, and you can find a discussion on there of the pros/cons of each in the Stack Overflow question Best .NET build tool.

NAntMSBuild是在.NET 中自动化构建的最流行工具,您可以在 Stack Overflow 问题最佳 .NET 构建工具中找到有关每种工具优缺点的讨论。

回答by S?ren Kuklau

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. There are tons of customization, especially once you've installed the MSBuild Community Tasks Project.
  • Use 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 参考。有大量的自定义,尤其是在您安装了MSBuild Community Tasks Project 之后
  • 使用NAnt。它比 MSBuild 存在的时间更长,并且拥有更多的社区支持,但需要您从头开始创建一个项目文件,而不是扩展现有的、由 Visual Studio 创建的文件。

回答by mmattax

Look into build tool NAntor MSBuild. I believe MSBuild is the build tool for Visual Studio 2005 and later. I am, however, a fan of NAnt...

查看构建工具NAntMSBuild。我相信 MSBuild 是 Visual Studio 2005 及更高版本的构建工具。然而,我是 NAnt 的粉丝......

回答by rboy

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. If you're running 64bit windows you may need to change x86_amd64to amd64in the batch script depending on your setup. This is assuming Visual Studio is installed in Program Filesand your solution is located in D:\MySoln

我假设您的设置是使用 x86 本机编译器和 x64 交叉编译器的 Visual Studio 2010 设置的 32 位 Windows 7。如果您运行的是 64 位 Windows,您可能需要根据您的设置在批处理脚本中将 x86_amd64更改为amd64。这假设 Visual Studio 安装在Program Files 中并且您的解决方案位于D:\MySoln

Create a file called buildall.batand 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.batto build the Release version of a x86 build and include this

crosscompilex86.bat来构建 x86 构建的发布版本并包含这个

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.batto build the Release version of the x64 build and include this

crosscompilex64.bat来构建 x64 版本的发布版本并包含这个

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. You can build both x86 and x64 Release versions by creating a Shortcut on your desktop which run the following commands:

现在将所有 3 个批处理文件与 MySoln.sln 一起放在您的解决方案文件夹中。您可以通过在桌面上创建一个运行以下命令的快捷方式来构建 x86 和 x64 发行版:

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

If you're using another configuration like AnyCPU etc you would need to customize the above scripts accordingly.

如果您使用其他配置,如 AnyCPU 等,您需要相应地自定义上述脚本。

回答by Scott Dorman

As of Visual Studio 2005, all of the project files (at least for .NET based projects) are actual MSBuildfiles, 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 NAntor 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(还有其他语言,但这些是目前的主流)。

回答by ferventcoder

Take a look at UppercuT. It has a lot of bang for your buck and it does what you are looking for and much more.

看看 UppercuT。它物超所值,可以满足您的需求,而且还有更多。

UppercuT uses NAnt to build and it is the insanely easy to use Build Framework.

UppercuT 使用 NAnt 构建,它是非常容易使用的构建框架。

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

回答by GSoft Consulting

Here is my batch file using msbuildfor VS 2010Debug configuration:

这是我使用msbuild进行 VS 2010调试配置的批处理文件:

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

回答by Sam Hasler

I had to do this for a C++ project in Visual Studio 2003so 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.htmfile. Open that file in your browser and then for each section such as:

在创建可执行文件的目录中会有一个BuildLog.htm文件。在浏览器中打开该文件,然后打开每个部分,例如:

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. I seem to remember having problems with absolute paths so you may have to make sure all the paths are relative.

创建一个包含方括号(但不包括方括号)之间的内容的 .rsp 文件,并随意命名。我似乎记得绝对路径有问题,因此您可能必须确保所有路径都是相对的。

Then in your build script add the command line from the BuildLog.htmfile 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)