Build and publish web application from command line using MSBuild.exe

In this article i will explain how to restore NuGet package, build and publish web application (asp.net c#, Mvc) from command line using "MsBuild.exe".

First of all change directory to your solution directory as given below -
SET BuildPath=C:\MyProjects\BatchFileDemo

CD %BuildPath%


Then by calling below command, all NuGet packages get restored of the solution, although NuGet integrated with MsBuild command and restores missing packages when a build begins. This feature is enabled by default, but we can opt out if desired and it is good to restore all NuGet packages of  the solution before building the solution.

Command to restoring NuGet packages of the solution

.nuget\nuget restore "%BuildPath%\DotnetMsbuildProject.sln"


"MsBuild" command and some essential parameters -
  • To build use /t:Build
  • To clean and build use /t:Clean;Build 
  • To rebuild solution use /t:Rebuild
  • To change configuration like Release/Debug use /p:Configuration=Release or /p:Configuration=Debug
  • To change target framework use /p:TargetFramework=v4.0 (as per the targeted framework)There are some more additional parameters for that please check what is your project need (you can check .csproj file to get an idea of project parameters) .
e.g. - To skip postsharp on build use /p:SkipPostSharp=True And for not to run code analysis during MsBuild use /p:RunCodeAnalysis=False etc.

I am using MsBuild 14.0v, You can find MsBuild command path of your system by checking registry key (MSBuildToolsPath) in the given location -
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions" {Version}, Or download the latest MsBuild tool.

Command to build a solution using MsBuild.exe

SET MSBuildPath=C:\Program Files (x86)\MSBuild\14.0\bin\amd64\

"%MSBuildPath%\MSBuild.exe" "%BuildPath%\DotnetMsbuildProject.sln" /t:Clean;Build /p:Configuration=Release /p:TargetFramework=v4.0  /p:SkipPostSharp=True /p:RunCodeAnalysis=False /p:VisualStudioVersion=11.0


In the above command we build the whole solution now to publish specific web project we have to use "PublishProfile" parameter, to use publish profile parameter you have to create publish profile first then pass the profile name in this parameter.

Command to publish a project

"%MSBuildPath%\MSBuild.exe" "%BuildPath%\DotnetMsbuildProject.csproj" /t:Build /p:DeployOnBuild=true /p:Configuration=Release /p:TargetFramework=v4.0 /p:VisualStudioVersion=11.0 /p:PublishProfile=OnRoot_Output /p:RestorePackages=false /p:SkipPostSharp=true


Restore, Build And Publish commands all together -

SET MSBuildPath=C:\Program Files (x86)\MSBuild\14.0\bin\amd64\
SET BuildPath=C:\MyProjects\BatchFileDemo

CD %BuildPath%

.nuget\nuget restore "%BuildPath%\DotnetMsbuildProject.sln"

"%MSBuildPath%\MSBuild.exe" "%BuildPath%\DotnetMsbuildProject.sln" /t:Clean;Build /p:Configuration=Release /p:TargetFramework=v4.0  /p:SkipPostSharp=True /p:RunCodeAnalysis=False /p:VisualStudioVersion=11.0

"%MSBuildPath%\MSBuild.exe" "%BuildPath%\DotnetMsbuildProject.csproj" /t:Build /p:DeployOnBuild=true /p:Configuration=Release /p:TargetFramework=v4.0 /p:VisualStudioVersion=11.0 /p:PublishProfile=OnRoot_Output /p:RestorePackages=false /p:SkipPostSharp=true


If you are facing any issue building the project using MsBuild first of all try to run these commands by run as administrator option then go for the further troubleshoot.
  

Recent Posts