How do you automate a Visual Studio build?

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?


Solution 1:

With VS2008 you can do this:

devenv solution.sln /build configuration

Solution 2:

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

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

msbuild.exe yoursln.sln

Solution 3:

Simplest way: navigate to the directory containing the solution or project file, and run msbuild (assuming you have Visual Studio 2005 or newer).

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.

Solution 4:

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.

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).

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_amd64 to amd64 in the batch script depending on your setup. This is assuming Visual Studio is installed in Program Files and your solution is located in D:\MySoln

Create a file called buildall.bat and add this to it:

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:

crosscompilex86.bat to build the Release version of a x86 build and include this

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

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:

  • Build All -> 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.

Solution 5:

NAnt and MSBuild are 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.