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
To fix this error, check the following location for MSbuild.exe.
Note: I am running on .Net 4.5
There we should find MSBuild.exe.
Now lets make PowerShell understand MSBuild invocation through following steps.
Go to Control Panel => System and Security => System
Click on Advanced System Settings.
Click on Environment Variables.
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)
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.
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.
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 –
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
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) –
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.
Select Custom option. Enter profile name.
Click OK and Select File System as Target Location (select a appropriate folder).
Click Next and Select following options.
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) –
To test the published contents, I am going to quickly deploy the folder to AzureWebsite (to publish to AzureWebsite, check this tutorial).
With this we come to an end of this jumpstart. Happy Coding!!!