MSBuild Introduction
As a follow up to my previous post on cruise control.
Let's look at what msbuild can do.
Many developers did not reliaze that when they make a project or solution, visual studio create an msbuild script . that is on your ProjectName.Csproj for c#project,as for web it just contain the path of the build on sln, this is what the web deployment project comes in creating additional msbuild for web
So when you click on Build ,Rebuild or Clean, Visual studio actually execute the script using MSbuild.
You can see the csproj using notepad or You might wondering How the hell i can see that xml on my Vs IDE?. the answer is right click on your project ->Click Unload Project -> Rightclick on your disable project -> Edit MyprojectName.CSproj.
Msbuild work on Targets.
Msbuild possessed property , and each target can have items.
You can do Import Targets. Using tag is used for Using your own DLL.
Msbuild can be customized as you wish
Let's Take an Example: I've got a pop3 projects that change the app.config during SIT,UAT and i want to email my others developer everytime i build
Now how can i automated that every time i build the project?
By Default Visual studio already give you the place , now lets edit our project.
- Add an SIT and UAT Configuration in your Configuration Manager
add the extension pack for Msbuild on your csproj ,i'm using the MSBuild Extension Pack , there is another option MSBuild Community extension.
<PropertyGroup>
<TPath>C:\Program Files\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks</TPath>
<TPath Condition="Exists('C:\Program Files\MSBuild\ExtensionPack\Common\MSBuild.ExtensionPack.tasks')">C:\Program Files\MSBuild\ExtensionPack\Common\MSBuild.ExtensionPack.tasks</TPath>
...
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(TPath)" />
...
<Target Name="AfterBuild">
<Message Text="Sending Email To Developers and QA" />
<ItemGroup>
<Attachment Include="C:\demo.txt" />
<Recipient Include="penting@cipto.com" />
<Recipient Include="iqbal@cipto.com" />
</ItemGroup>
<MSBuild.ExtensionPack.Communication.Email Attachments="@(Attachment)" TaskAction="Send" Subject="Pop3 is Build Version 1.0.0.1" SmtpServer="127.0.0.1" MailFrom="ciptoaja@cipto.com" MailTo="@(Recipient)" Body="HUii" />
<Copy Condition="$(Configuration)=='UAT'" SourceFiles=".\uatconfig\Pop3Downloader.exe.config" DestinationFiles="$(OutputPath)Pop3Downloader.exe.config" />
<Copy Condition="$(Configuration)=='SIT'" SourceFiles=".\sitconfig\Pop3Downloader.exe.config" DestinationFiles="$(OutputPath)Pop3Downloader.exe.config" />
</Target>
There you go. Now every time you build Copy the config and email other developer or qa
Now you might wondering VS always create your first schema target="Build" etc comes from? i can't find it anywhere on my projects
well actually it imports them
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> and also
MSBuild ships with several useful tasks. For a complete list, see the
Microsoft.Common.tasks file located in the
%WinDir%\Microsoft.NET\Framework\v2.0.50727 directory
22/10/2008 , You can add an external tool for accessing msbuild.exe from your ide.
click tools->External tools-> Fill TItle with MSBUild -> Fill command with C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe -> Fill arguments and initial directory
check the use output window, and prompt for arguments option.