This post will explain the process of automating your build / publish (web or windows) with windows Powershell and MSBUILD
What I use ?
1) MSBUILD
2) VS 2008 – The sample that i will provide will depend upon a windows application and a web application
3) Powershell – ease of scripting, next generation script for windows, search for Windows Powershell to learn more
4) SVN – Source code repos (you can alternatively use any source code repos for integrating)
Before you Start(I mean : prerequisites)
1) Install MSBUILD – (if you already don’t have it)
2) open PowerShell and type – MSBUILD, you must see something like this
Microsoft (R) Build Engine Version 3.5.21022.8
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
–if you get this then you have MSBUILD properlly installed
– if not then you probably must add the Env. Variables – to do this – right click my computer –> props –> Env. variables –> select path and add “C:\WINDOWS\Microsoft.NET\Framework\v3.5″ (for vs2008 installations, this might differ for vs2005 as it uses the 2.0 framework)
3) Make sure you have MSBUILD community tasks – its a freely available over the net…
4) SVN – a source code control management
5) I Tunes or any other music player – a good collection of songs – my favorite was OST – Batman, The Dark kinght .
ok, this is how it works
MSBUILD is blah blah blah, there is already enuf said abt this throughout the net…
We need to give a solution file / .proj file with some target specified to msbuild to perform the BUILD. So create a fine named yourproject.proj, copy the following contents to it…
<?xml version=”1.0″ encoding=”utf-8″?>
<Project DefaultTargets=”Build” xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″>
<Import Project=”$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets”/>
<PropertyGroup>
<Major>2</Major>
<Minor>2</Minor>
<Build>0</Build>
<Revision>0</Revision>
</PropertyGroup>
<ItemGroup>
<DefaultExclude Include=”**\.svn\**” />
<DefaultExclude Include=”**\bin\**” />
<DefaultExclude Include=”**\obj\**” />
<DefaultExclude Include=”**\Release\**” />
<DefaultExclude Include=”**\Debug\**” />
<DefaultExclude Include=”**\Test\**” />
<DefaultExclude Include=”**\TestResults\**” />
<DefaultExclude Include=”**\doc\**” />
<DefaultExclude Include=”**\www\**” />
<DefaultExclude Include=”**\*.user” />
<DefaultExclude Include=”**\*.suo” />
<DefaultExclude Include=”**\*.zip” />
<DefaultExclude Include=”**\*.txt” />
<DefaultExclude Include=”**\*.pdb” />
</ItemGroup>
<PropertyGroup>
<SourceFileRootFolder>TODO: YOUR WORKING FOLDER PATH</SourceFileRootFolder>
<WebFolder>TODO: YOUR WEB SITE DIRECTORY FOLDER PATH</WebFolder>
<ReleaseFolder>TODO: GIVE THE PATH OF THE PUBLISH FOLDER(C:\PublishedSource</ReleaseFolder>
</PropertyGroup>
<ItemGroup>
<ZipFiles Include=”../MyProject/bin/Release/*.*;” Exclude=”@(DefaultExclude)” />
</ItemGroup>
<ItemGroup>
<DefaultBinFiles Include=”$(SourceFileRootFolder)\DefaultBinFiles\*.*”/>
<ProjectBinFiles Include=”$(SourceFileRootFolder)\$(WebFolder)\bin\*.*”/>
</ItemGroup>
<Target Name=”Compile”>
<MSBuild Projects=”../MyProject/MyProject.csproj” Properties=”Configuration=Release” />
</Target>
<Target Name=”CleanSource”>
<Message Text=”Removing all source files from $(ReleaseFolder)” />
<RemoveDir Directories=”$(ReleaseFolder)” />
</Target>
<Target Name=”PublishWeb” DependsOnTargets=”Mimify”>
<MSBuild Projects=”$(SourceFileRootFolder)\$(WebFolder)\WebProject.csproj” Targets=”Clean;Build” />
<CallTarget Targets=”CleanSource”/>
<MSBuild Projects=”$(SourceFileRootFolder)\$(WebFolder)\WebProject.csproj” Targets=”_CopyWebApplication;_BuiltWebOutputGroupOutput” Properties=”OutDir=$(ReleaseFolder)\” ></MSBuild>
<Copy SourceFiles=”@(DefaultBinFiles)” DestinationFolder=”$(ReleaseFolder)\$(WebFolder)\bin\”></Copy>
<Copy SourceFiles=”@(ProjectBinFiles)” DestinationFolder=”$(ReleaseFolder)\$(WebFolder)\bin\”></Copy>
</Target>
<Target Name=”BuildWeb” DependsOnTargets=”PublishWeb”>
<Message Text=”Build of web application complete”/>
</Target>
<Target Name=”Zip” DependsOnTargets=”Compile”>
<Zip Files=”@(ZipFiles)” ZipFileName=”SOMENAME.v$(Major).$(Minor).$(Build).$(Revision).zip” />
</Target>
<Target Name=”BuildTool” DependsOnTargets=”Zip”>
<Message Text=”Tool build completed”/>
</Target>
<UsingTask
TaskName=”CompressorTask”
AssemblyFile=”Yahoo.Yui.Compressor.dll” />
<PropertyGroup>
<CssOutputFile Condition=” ‘$(CssOutputFile)’==” “>SylesSheetFinal.css</CssOutputFile>
<JavaScriptOutputFile Condition=” ‘$(JavaScriptOutputFile)’==” “>$(SourceFileRootFolder)\$(WebFolder)\myjs\</JavaScriptOutputFile>
</PropertyGroup>
<Target Name=”Mimify”>
<!–
ItemGroup\CssFiles or ItemGroup\JavaScriptFiles: add zero to many files you wish to include in this compression task.
Don’t forget, you can use the wildcard (eg. *.css, *.js) if you feel up to it.
Finally, at least one item is required – either a css file or a js file.
CssFiles/JavaScriptFiles data format: Please do not touch this.
DeleteCssFiles: [Optional] True | Yes | Yeah | Yep | True | FoSho | Fo Sho. Default is False. Anything else is False. (eg. blah = false, xxxx111 = false, etc)
CssCompressionType: YuiStockCompression | MichaelAshsRegexEnhancements | HaveMyCakeAndEatIt or BestOfBothWorlds or Hybrid; Default is YuiStockCompression.
ObfuscateJavaScript: [Optional] refer to DeleteCssFiles, above.
PreserveAllSemicolons: [Optional] refer to DeleteCssFiles, above.
DisableOptimizations: [Optional] refer to DeleteCssFiles, above.
EncodingType: [Optional] ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8, Default. Default is ‘Default’.
DeleteJavaScriptFiles: [Optional] refer to DeleteCssFiles, above.
LineBreakPosition: [Optional] the position where a line feed is appened when the next semicolon is reached. Default is -1 (never add a line break).
0 (zero) means add a line break after every semicolon. (This might help with debugging troublesome files).
LoggingType: None | ALittleBit | HardcoreBringItOn; Hardcore also lists javascript verbose warnings, if there are any (and there usually is
).
ThreadCulture: [Optional] the culture you want the thread to run under. Default is ‘en-gb’.
IsEvalIgnored: [Optional] compress any functions that contain ‘eval’. Default is False, which means a function that contains
‘eval’ will NOT be compressed. It’s deemed risky to compress a function containing ‘eval’. That said,
if the usages are deemed safe this check can be disabled by setting this value to True.
–>
<ItemGroup>
<JavaScriptFiles Include=”$(SourceFileRootFolder)\$(WebFolder)\Javascript\*.js” />
<CSSFiles Include=”$(SourceFileRootFolder)\$(WebFolder)\Themes\*.css” />
</ItemGroup>
<CompressorTask
DeleteCssFiles=”false”
CssOutputFile=”%(CSSFiles.Identity)”
CssCompressionType=”YuiStockCompression”
JavaScriptFiles=”@(JavaScriptFiles)”
CSSFiles=”@(CSSFiles)”
ObfuscateJavaScript=”True”
PreserveAllSemicolons=”False”
DisableOptimizations=”Nope”
EncodingType=”Default”
DeleteJavaScriptFiles=”false”
LineBreakPosition=”-1″
JavaScriptOutputFile=”%(JavaScriptFiles.Identity)”
LoggingType=”ALittleBit”
ThreadCulture=”en-au”
IsEvalIgnored=”false”
/>
</Target>
</Project>
Change your relevant project details in it – i have placed TODO: to specify what has to be done.
There are lot of targets specified in my .proj fle
Compile – compiles the source files
PublishTool – publishes the windows application
PublishWeb – publishes the web application
Mimify – Obfuscate Javascript and CSS files – uses external dll, freely available on net
I have several things in my build project…like for example all my JAVASCRIPT and CSS will be obfuscated during the publish time so that i don’t allow anyone to re-engineer my JS logic…
Execute the build
To execute the build
1) open powershell / command prompt
2) msbuild.exe c:\whereever\yourproject.proj (remember the target file we created in the previous step)
you can provide a target as
msbuild.exe c:\whereever\yourproject.proj /t:Publishweb
so its pretty much of it…any questions please feel free to email me – consoleart@gmail.com
in my next article we shall see how to automate the build process…
These concepts may be little old, but there are several projects who are struggling with build automation, may be this article will help them to provide a bit of understanding…
thnx to the people who have shown me that there are several tools that exists to make our life easier…