Monday, April 9, 2012

How to build Solution or Project from command line (Batch File)?


Sometimes you might need to build multiple solutions or complex project in particular sequence. In such case you can build your project or solutions using command line or batch file. In this post I will explain simple way to build project or solution using command line tool or creating batch file.

Devenv allows you to build your solution or project using command line. It also allows related multiple operations such as clean solution, rebuild solution or deploy solution etc. To execute commands, you need to open Visual Studio command prompt available inside visual studio tools under all programs of start menu. You can find multiple options available with devenv using below command.

Devenv /?

Let’s start with simple example which builds solution using command prompt.

Devenv  TestConsoleApplication.sln /Build Debug



The above command builds TestConsoleApplication solution in Debug configuration mode. To build this solution with Release mode you just need to write Release instead of Debug in above code.

Similarly you can clean and rebuild solution using below code.

Devenv  TestConsoleApplication.sln /Clean

Devenv  TestConsoleApplication.sln /Rebuild Debug

We can also build specific project with project build configuration along with solution.

Devenv  TestConsoleApplication.sln /Build Debug /Project TestConsoleApplication/TestConsoleApplication.csproj /ProjectConfig Debug


Below command opens solution from command prompt and runs the application. If any exception occurred while running application it will log the same in MyErrorLog.txt file.

Devenv /Run TestConsoleApplication.sln /out D:/Temp/MyErrorLog.txt

The above command runs the application and log exception if any into separate file mentioned with /out switch.

We can also reset Visual Studio’s setting using below line of code. This is nice and great feature which can be useful when something is missing from IDE or IDE is not working property. This will restore back all the default settings of Visual Studio’s IDE.

Devenv /ResetSettings

MSBuild.exe

For build related task, it is recommended by Microsoft to use Msbuild.exe. Msbuild.exe builds project or solution with specified options. You can find multiple options using below help command.

msbuild.exe /?

To build solution using msbuild you can use below line of code.

MSBuild TestConsoleApplication.sln /p:Configuration=Release /p:Platform=”Any CPU”


You can also perform multiple task like Rebuild, Clean etc using MsBuild.

You can also create batch file to build your application. Below is the sample code of batch file to build your application using devenv.

BuildSolution.Bat
@Echo OFF
Echo "Building solution/project file using batch file"
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\
SET SolutionPath=d:\Temp\TestConsoleApplication\TestConsoleApplication.sln
Echo Start Time - %Time%
Call Devenv %SolutionPath% /Build Debug
Echo End Time - %Time%
Set /p Wait=Build Process Completed...


You can also create batch file to build solution using MSBuild.exe file similar above mentioned batch file with just little change.

BuildSolution.Bat
@Echo OFF
Echo "Building solution/project file using batch file"
SET PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319\
SET SolutionPath=d:\Temp\TestConsoleApplication\TestConsoleApplication.sln
Echo Start Time - %Time%
MSbuild %SolutionPath% /p:Configuration=Release /p:Platform="Any CPU"
Echo End Time - %Time%
Set /p Wait=Build Process Completed...


See Also - 

4 comments:

  1. really good piece of information, I had come to know about your site from my friend shubodh, kolkatta,i have read atleast nine posts of yours by now, and let me tell you, your site gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a lot once again, Regards, Marathi Jokes in Marathi Language

    ReplyDelete
  2. How to get build dll path i.e output directory path using batch script?

    Please help it is Urgent.

    ReplyDelete
    Replies
    1. Hello,

      As per my understanding from your question, i don't think we can get VS solution dll PATH in MS Dos batch file. You need to manually set that path to DOS variable and than you can use it with MSBuild command.

      Hope this answers your question.

      ~Mitesh

      Delete
  3. Thanks for your post, it helps a lot for newbies like me.

    Can we also use '/out D:/Temp/MyErrorLog.txt' with MS Build to log any exception occurred while running application?like
    MSbuild %SolutionPath% /p:Configuration=Release /p:Platform="Any CPU" /out D:/Temp/MyErrorLog.txt

    Please suggest.

    ReplyDelete