Siddesh BG's Build Release Config mgmt Blog

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 10 November 2011

Learning MSBuild

Posted on 22:24 by Unknown
In my last 7 years of my career, I mostly worked on Unix based builds. Now I got curious to explore Windows builds. I learnt the 2 prominent build assistants in Windows are MSBuild and Nant. In this blog, I will share my learning experience of MSBuild.


What is MSBuild?
MSBuild is the build platform for Microsoft and Visual Studio. Programming languages that target the .NET Framework, for example, Visual C# and Visual Basic, use MSBuild project files to describe and control the application build process. When you use Visual Studio to create an MSBuild project file, the appropriate XML is added to the file automatically. 


Lets start by creating a simple C# file and compiling it through csc compiler. csc is a C# compiler, it comes along with .NET Framework (version 2.0, 3.5, or 4.0).
1) cd D:\HelloWorld
2) Create a file Helloworld.cs , with it's content as
using System;
class HelloWorld
{
static void Main()
{
#if DebugConfig
Console.WriteLine("WE ARE IN THE DEBUG CONFIGURATION");
#endif
Console.WriteLine("Hello, world!");
}
}
3) Very likely csc compiler location  won't be in your path. Set it
    set PATH=%PATH%;%WINDIR%\Microsoft.Net\Framework64\v2.0.50727
    Alternatively, if you have Visual Studio installed, you can use the Visual Studio Command Prompt, which has a path that includes the MSBuild folder.
4)  Compile it
    csc Helloworld.cs
5) Test the application by typing Helloworld.exe at the command prompt
     O/p: Hello, world!
6) Delete the application
    del helloworld.exe 
Now let's do the same thing using MSBuild project file. We are creating a minimal MSBuild project file and it's content will be saved in a file named "Helloworld.csproj".

<!-- root Project node   -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">


  <!--ItemGroup node contain item elements   -->
  <ItemGroup>
    <!-- An item element that refers to the application source file. -->
    <Compile Include="helloworld.cs" />
  </ItemGroup>


  <!-- A Target node to contain tasks that are required to build the application.  -->
  <Target Name="Build">
    <!-- A Task element to start the Visual C# compiler to build the application.  -->
    <Csc Sources="@(Compile)"/>
  </Target>
</Project>

Building the application using MSBuild project file
Command: msbuild Helloworld.csproj /t:Build 
This builds the Build target of the Helloworld project file by invoking the Visual C# compiler to create the Helloworld application.
Output:

Microsoft (R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.
Build started 2011-11-11 12:05:00 PM.
Project "D:\siddesh\learning-msbuild\HelloWorld\Helloworld.csproj" (Build target(s)):
Target Build:
    C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\Csc.exe /out:helloworld.exe helloworld.cs
Build succeeded.
    0 Warning(s)
    0 Error(s)
Time Elapsed 00:00:01.12

For detailed build, add flag
   msbuild Helloworld.csproj /t:Build /verbosity:detailed


Improving MSBuild project file - Properties

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- PropertyGroup element is used to define variables/properties -->
  <PropertyGroup>
    <AssemblyName>MSBuildSample</AssemblyName>
    <OutputPath>Bin\</OutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="helloworld.cs" />
  </ItemGroup>
  <Target Name="Build">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <!-- The MakeDir task creates a folder that is named by the OutputPath property, provided that no folder by that name currently exists. -->
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
    <!--This instructs the Visual C# compiler to produce an assembly that is named by the AssemblyName property and to put it in the folder that is named by the OutputPath property. -->
  </Target>
</Project>

Improving MSBuild project file - Adding build targets


<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This sets the Build target as the default target. -->
  <PropertyGroup>
    <AssemblyName>MSBuildSample</AssemblyName>
    <OutputPath>Bin\</OutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="helloworld.cs" />
  </ItemGroup>
  <Target Name="Build">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
  </Target>
  <!-- The Clean target invokes the Delete task to delete the application. -->
  <Target Name="Clean" >
    <Delete Files="$(OutputPath)$(AssemblyName).exe" />
  </Target>
  <!-- The Rebuild target does not run until both the Clean target and the Build target have run. -->
  <Target Name="Rebuild" DependsOnTargets="Clean;Build" />
</Project>

Testing:
1) msbuild helloworld.csproj /p:AssemblyName=Greetings
    MSBuild runs the default Build target. The /p switch overrides the AssemblyNameproperty and gives it the new value, Greetings. This causes a new application, Greetings.exe, to be created in the \Bin\ folder.
2) msbuild helloworld.csproj /t:clean
  This runs the Clean task to remove the application that has the default AssemblyName property value, MSBuildSample.
3) msbuild helloworld.csproj /t:clean /p:AssemblyName=Greetings.
This runs the Clean task to remove the application that has the given AssemblyName property value, Greetings.
4) msbuild
Although a project file is not specified, MSBuild builds the helloworld.csproj file because there is only one project file in the current folder. 
Reference: http://msdn.microsoft.com/en-us/library/dd576348.aspx
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home
View mobile version

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Solution to Project Euler Problem 10 - Find the sum of all the primes below two million
    http://projecteuler.net/problem=10 Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two mi...
  • Fortify scan automation steps for analyzing c/c++ code (Makefiles)
    I wrote in my previous blog about installing and configuring Fortify client. This blog presents standard steps to automate fortify scan for ...
  • Posting a JIRA bug using Perl Mechanize
    Perl provides modules which can be used as command line browser to automate tasks dependent on web pages. Among them LWP and mechanize are i...
  • jenkins error: java.io.IOException: Authentication method password not supported by the server at this stage
    When I tried to add a node to jenkins/hudson using ssh as launch method, the authentication keeps on failing with the below error. [12/15/11...
  • Unable to resolve target system name - a DNS problem
    I was not able to ping to any machines from my Windows 2003 server. I did following steps to debug & resolve the issue, which was relate...
  • Installing and configuring Fortify on Linux and Windows machines
    Installing Fortify on Linux (RHEL 5 32 bit) Download Fortify archive Fortify-360-2.6.5-Analyzers_and_Apps-Linux-x86.tar.gz and extract it to...
  • Perforce - can't edit exclusive file already opened
    In perforce, whenever a binary file like doc, xls or ppt files are checked out, it is opened in exclusive lock mode. So no other person can ...
  • Perforce and cygwin
    Are you a command-line freak ? Do you want your automated shell scripts to run on Windows ? Do you wish to work with Perforce commands on Cy...
  • Using BUILD_LOG_REGEX in jenkins email notification
    Jenkins provide 'Email-ext' plugin, which  allows to configure every aspect of email notifications. One of my requirement is to send...
  • 0509-036 Cannot load program p4 because of the following errors
    Here is the full description of error ............ bash-3.00# p4 info exec(): 0509-036 Cannot load program p4 because of the following error...

Categories

  • AIX
  • AIX ssh
  • ANT
  • apache
  • appliance
  • awk
  • branching
  • build-failures
  • cgi-perl
  • code-signing
  • commands
  • continuous Integration
  • cvs
  • cygwin
  • DNS
  • Drupal
  • EPM
  • euler
  • Fortify
  • hadoop
  • hpux
  • html
  • InstallShield
  • iptables
  • iso
  • jenkins-hudson
  • Jira
  • kiwi
  • linux
  • Makefile
  • maven
  • Miscellaneous
  • mysql
  • nexus
  • NFS
  • package
  • Perforce
  • Perl
  • php
  • rbuilder
  • rpath
  • rpm
  • rsync
  • Solaris
  • ssh
  • SuseStudio
  • tinderbox
  • unix
  • Visual studio 2008
  • vmware
  • war
  • webserver
  • wget
  • windows
  • xterm

Blog Archive

  • ►  2013 (12)
    • ►  December (1)
    • ►  July (2)
    • ►  April (2)
    • ►  March (2)
    • ►  February (3)
    • ►  January (2)
  • ►  2012 (43)
    • ►  December (2)
    • ►  November (1)
    • ►  October (4)
    • ►  September (7)
    • ►  August (5)
    • ►  July (4)
    • ►  June (2)
    • ►  May (3)
    • ►  April (4)
    • ►  March (3)
    • ►  February (1)
    • ►  January (7)
  • ▼  2011 (23)
    • ►  December (4)
    • ▼  November (9)
      • Learning MSBuild
      • Solution to Project Euler Problem 9 - Finding Pyt...
      • Solution to Project Euler Problem 8 - greatest pro...
      • Solution to Project Euler Problem 7 - 10001th pri...
      • Solution to Project Euler Problem 6 - sum of the ...
      • BUILD FAILED Unable to delete file hsqldb.out
      • Solution to Project Euler Problem 5 - the smalles...
      • Solution to Project Euler Problem 4 - The largest...
      • Solution to Project Euler Problem 3 - Finding pri...
    • ►  October (4)
    • ►  September (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
  • ►  2010 (15)
    • ►  December (2)
    • ►  November (1)
    • ►  September (3)
    • ►  April (1)
    • ►  February (6)
    • ►  January (2)
  • ►  2009 (28)
    • ►  November (5)
    • ►  October (3)
    • ►  September (2)
    • ►  August (1)
    • ►  July (1)
    • ►  June (5)
    • ►  May (3)
    • ►  April (1)
    • ►  February (2)
    • ►  January (5)
  • ►  2008 (20)
    • ►  December (6)
    • ►  November (3)
    • ►  October (1)
    • ►  September (1)
    • ►  July (8)
    • ►  June (1)
Powered by Blogger.

About Me

Unknown
View my complete profile