I always wanted to have ability build or deploy my website with one run of the .bat or .cmd file. Of course there is software for this like Cruise Control or AntHill for example. But just for one small blog or website you don’t want to spend a lot of time for installing and configuring of these heavy tools.

So I always wanted to have "Publish" function from Visual Studio as a command line. And I found it! So let me share with you my experience how to build command line automated build for MVC web app.

Then Edit project file (again right click on project name -> Edit ProjectName.csproj)

Find “Target” tag end replace it with code:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'" DependsOnTargets="CleanWebsitesPackage;CleanWebsitesTransformParametersFiles;">
	<AspNetCompiler VirtualPath="temp" PhysicalPath="$(MSBuildProjectDirectory)" />

As I understand this command will cleanup temporary folders from precompiled views.
Save modifications and reload the project (right click on project name -> Reload Project).

Now I’m able to build solution with msbuild from command line without any errors. But when I built it and tried to run, I got another problem: connection strings in web.config file was replaced. Small investigation tells that msbuild has a “configuration transformation” feature enabled by default. Again, it is good feature if you have big solution with different connection strings for different environments. In my case I decided to switch it off. To switch off configuration transformation or prevent your web.config connection strings from being tokenized we need to create ProjectName.wpp.targets file in the root of web project (in my case this is kitsula.com.wpp.targets) with content:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

That’s it. Now full source of build.cmd file:

rem Remobe obj folder
rd /S /Q kitsula.com\obj

// Build solution
c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild kitsula.com.sln /p:Configuration=Release;DeployOnBuild=true;DeployTarget=Package;_PackageTempDir=..\web_deploy

rem Compress Static Resources (JS & CSS)

In CompressStaticResources.cmd I’m making minifying and compression of static files (with help of YUICompressor)

rem Compress JS
java -jar YUICompressor\yuicompressor-2.4.6.jar --nomunge web_deploy\Scripts\jquery.blockUI.js -o web_deploy\Scripts\jquery.blockUI.js
java -jar YUICompressor\yuicompressor-2.4.6.jar --nomunge web_deploy\Scripts\mySyntaxHighlighter.js -o web_deploy\Scripts\mySyntaxHighlighter.js
java -jar YUICompressor\yuicompressor-2.4.6.jar --nomunge web_deploy\Scripts\JSErrorHandler.js -o web_deploy\Scripts\JSErrorHandler.js
java -jar YUICompressor\yuicompressor-2.4.6.jar --nomunge web_deploy\Scripts\Site.js -o web_deploy\Scripts\Site.js
java -jar YUICompressor\yuicompressor-2.4.6.jar --nomunge web_deploy\Scripts\Maps.js -o web_deploy\Scripts\Maps.js

rem Compress CSS
for /f %%f in ('dir /b web_deploy\Content\CSS\') do (java -jar YUICompressor\yuicompressor-2.4.6.jar web_deploy\Content\CSS\%%f -o web_deploy\Content\CSS\%%f)