Skip to main content

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.
  

Popular posts from this blog

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview, Scrollble gridview with fixed header through javascript, File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview.

We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".