JumpStart # 39– Build and publish a Visual Studio MVC Project using MSBuild and PowerShell

In this jumpstart, I am going to show how to build and  publish an ASP.Net MVC project created in Visual Studio IDE using MSBuild application (exe) and PowerShell. This approach would help us in automating local/server builds which involves multiple steps of complex targets.

Lets get started by finding whether MSBuild is existing on the machine where we are trying to build the project. Open Windows PowerShell and type msBuild

image

To fix this error, check the following location for MSbuild.exe.

Note: I am running on .Net 4.5

 C:\Windows\Microsoft.NET\Framework\v4.0.30319 

There we should find MSBuild.exe.

image

Now lets make PowerShell understand MSBuild invocation through following steps.

Go to Control Panel => System and Security => System

image

Click on Advanced System Settings.

image

Click on Environment Variables.

image

Select PATH variable (as shown above) and click Edit. Add the path of MSBuild at the end of variable.

Make sure that msbuild path is - ;C:\Windows\Microsoft.NET\Framework\v4.0.30319; (do not forget the starting and trailing semi-colon separators)

image

Once done, click OK (click OK on subsequent opened windows as well).

Now try msbuild in PowerShell (make sure to close and re-open PowerShell window). We should see following output.

image

Now lets create a ASP.Net MVC 4 Project in Visual Studio 2013. Just to introduce a little complexity in the build process, create a class library project and add it as reference to the MVC Project. Similarly add a nuget package – Elmah to the Web project.

image

References –

image

NOTE: We have to re-install Microsoft.AspNet.WebApi nuget to get rid of some missing DLL References problem on publish. Below references will not be copied to bin folder on publish because of Copy Local is set to false  –

  1. System.Web.Http.dll
  2. System.Web.Http.WebHost.dll
  3. System.Net.Http.Formatting.dll

Re-install can be done at Package Manager Console (Visual Studio –> Tools –> Nuget Package Manager –> Package Manager Console) by executing below command –

Update-Package Microsoft.AspNet.WebApi -reinstall

As an alternative, we can also set Copy Local = True for above References.

Now lets clean the Solution using msbuild -

 MSBuild C:\Users\venvemula\Desktop\IntstringsDemo\IntstringsDemo.sln /target:Clean 

image

To build the solution –

MSBuild C:\Users\venvemula\Desktop\IntstringsDemo\IntstringsDemo.sln /property:Configuration=Release 

After a successful build, we get following message (there will be many interim messages to show build progress) –

image

Now we need to create a publish profile. Publish profile creation is a one time activity. Once created we can use the this publish profile to automate build activity using msbuild, Right click solution in Visual Studio, select Publish.

image

Select Custom option. Enter profile name.

image

Click OK and Select File System as Target Location (select a appropriate folder).

image

Click Next and Select following options.

image

Click Publish.  VS will successfully builds and publishes code to the selected folder. now lets automate the publish folder using msbuild by using following command.

msbuild C:\Users\venvemula\Desktop\IntstringsDemo\IntstringsDemo.sln /p:DeployOnBuild=true /p:PublishProfile=Test 

Above command will publish the VS project to the local file system (as stated in publish profile). After a successful build, we get following message (there will be many interim messages to show build progress) –

image

To test the published contents, I am going to quickly deploy the folder to AzureWebsite (to publish to AzureWebsite, check this tutorial).

image

With this we come to an end of this jumpstart. Happy Coding!!!

You may also like...