|
Recently I wrote a blog entry about building applications in Visual Studio .Net. I was especially impressed by Microsoft's introduction of Assemblies as a specific mechanism for configuration and deployment. I also said that we would start to examine the features in MSBuild and ClickOnce deployment. In this article we will introduce MSBuild and examine some of its cool features. Taming the Wild Dependencies in your application As I mentioned in my blog, Microsoft has introduced Assemblies as a specific mechanism for configuration and deployment. Assemblies are a response to the difficulties of using DLLs in earlier Microsoft frameworks. Assemblies also have built-in version identification and store key metadata, describing dependencies, which can be used to control the deployment of all components (or as we call them Configuration Items - CIs). Assemblies contain their own Manifest which includes a comprehensive list of CIs and their dependencies. Assemblies can also be signed using cryptographic keys - which has become an essential aspect of both web based and client application deployment. Really, all Configuration Items should be signed using a public/private key pair to prevent an unauthorized deployment that could easily provide the hacker with a convenient back door. Finally, Assemblies also have built-in dynamic version numbers including a C# method called GetExecutingAssembly which returns the Version in a four part token including the <Major Version Id>.<Minor Version Id>.<Build Id>.<Revision Number>. My first build using MSBuild For this introduction I used Visual Studio 2008 and the Microsoft .net Framework Version 3.5. I created a simple project by selecting File -> New -> Project to create a Visual C# Windows Forms Application (which I called CMBasicsTest1). Using the Visual Studio interface I created a neat little application to enter a name and comments. This created a file called CMBasicsTest1.CSPROJ which is used by Visual Studio. It's very easy to build the application (just hit F6), but building from within an Integrated Development Environment (IDE), such as Visual Studio, does not provide you with a repeatable build. The reason is that it's very hard to track runtime dependencies from within an IDE. The good news is that the CMBasicsTest1.CSPROJ file can also be used to build the application from the command line. Getting MSBuild to run I did not find any notes on setting the Path to run MSBuild and there were several different locations that I searched looking for where the executable was located. Finally, a google search helped me ascertain that I needed to add "C:\Windows\Microsoft.NET\Framework\v3.5" to my path. This allowed me to run MSBuild as follows C:\Users\bob\Documents\Visual Studio 2008\Projects\CMBasicsTest1\CMBasicsTest1>msbuild Microsoft (R) Build Engine Version 3.5.21022.8 [Microsoft .NET Framework, Version 2.0.50727.1434] Copyright (C) Microsoft Corporation 2007. All rights reserved. Build started 11/2/2008 6:19:26 PM. Project "C:\Users\bob\Documents\Visual Studio 2008\Projects\CMBasicsTest1\CMBasicsTest1\CMBasicsTest1.csproj" on node 0 (default targets). Processing resource file "Form1.resx" into "obj\Debug\CMBasicsTest1.Form1.resources". Processing resource file "Properties\Resources.resx" into "obj\Debug\CMBasicsTest1.Properties.Resources.resources". CopyFilesToOutputDirectory: Copying file from "obj\Debug\CMBasicsTest1.exe" to "bin\Debug\CMBasicsTest1.exe". CMBasicsTest1 -> C:\Users\bob\Documents\Visual Studio 2008\Projects\CMBasicsTest1\CMBasicsTest1\bin\Debug\CMBasicsTest1.exe Copying file from "obj\Debug\CMBasicsTest1.pdb" to "bin\Debug\CMBasicsTest1.pdb". Done Building Project "C:\Users\bob\Documents\Visual Studio 2008\Projects\CMBasicsTest1\CMBasicsTest1\CMBasicsTest1.csproj" (default targets). Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.51 Examining the CSPROJ file The key to understanding MSBuild is the CSPROJ file created by Visual Studio (mine is called CMBasicsTest1.csproj). This file functions in a similar way to the Makefile used by Make (and GNU Make) and the build.xml used by Ant. There are four sections in the CSPROJ file. 1. PropertyGroup - used to define custom properties (e.g. .net Framework version and the all important Assembly name mentioned earlier and also in my blog) 2. ItemGroup - used to define the forms and source code files used by the project 3. Import - used to define predefined Microsoft CSharp targets as shown Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" 4. Customizable targets - used to define a variety of custom written targets including actions to occur before or immediately after the build. The XML is pretty straight forward although different than the XML that you find in other build tools such as Ant and Maven. Here is an CSPROJ file which I edited down just to show you the outline <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> </PropertyGroup> <ItemGroup> <Compile Include="Form1.cs"> <SubType>Form</SubType> </Compile> <Compile Include="Form1.Designer.cs"> <DependentUpon>Form1.cs</DependentUpon> </Compile> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild"> </Target> <Target Name="AfterBuild"> </Target> --> </Project> Command line help You can easily access help at the command line by typing msbuild /? C:\Users\bob\Documents\Visual Studio 2008\Projects\CMBasicsTest1\CMBasicsTest1>msbuild /? Microsoft (R) Build Engine Version 3.5.21022.8 [Microsoft .NET Framework, Version 2.0.50727.1434] Copyright (C) Microsoft Corporation 2007. All rights reserved. Syntax: MSBuild.exe [options] [project file] Description: Builds the specified targets in the project file. If a project file is not specified, MSBuild searches the current working directory for a file that has a file extension that ends in "proj" and uses that file. Switches: /target:<targets> Build these targets in this project. Use a semicolon or a comma to separate multiple targets, or specify target separately. (Short form: /t Example: /target:Resources;CompileSxxr. There are many more switches that we will cover in the future. Examples (from the online help) You can specify the .sln (which contains a list of items with their locations and any build configurations that are required) or a specific CSPROJ file along with targets such as Clean (to remove anything previously built) and also command line properties. MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug;TargetFrameworkVersion=v3.5xxs. Conclusion I am getting involved with a project where I will need to take a very deep dive into C#, .net, and MSBuild. I am looking forward to sharing what I learn and I hope that you will drop me a line and share your experiences as well! Books I used to learn C#, .net, MSBuild and ClickOnce Deployment For this article I used Apress's book on Deploying .NET Applications using MSBuild and ClickOnce by Sayed Y. Hashimi and Sayed Ibrahim Hashimi and also Wrox's book called Professional C# 2005 by Christian Nagel, Bill Evjen et al, as well as (Drop me a line if you need the ISBN numbers) Bob Aiello is the Editor-in-Chief for CM Crossroads and an independent consultant specializing in Software Process Improvement including Software Configuration and Release Management. Mr. Aiello has over 25 years experience as a technical manager in several top NYC Financial Services firms where he had had company-wide responsibility for CM, often providing hands-on technical support for enterprise Source Code Management tools, SOX/Cobit compliance, build engineering, continuous integration and automated application deployment. Bob is a long standing member of the Steering Committee of the NYC Software Process Improvement Network (CitySPIN), where he serves as the chair of the CM SIG. Mr. Aiello holds a Masters in Industrial Psychology from NYU and a B.S. in Computer Science and Math from Hofstra University. You may contact Mr. Aiello at raiello@acm.org or link with him at http://www.linkedin.com/in/bobaiello .
Set as favorite
Bookmark
Email this
Hits: 3908 Trackback(0)Comments (1)
|
|
... I'll be starting with msbuild as well and just completed our internal C# training here in the office... Nice to hear that someone like you is sharing your experiences on this Thanks!!! |
|
Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.


Recently I wrote a blog entry about building applications in Visual Studio .Net. I was especially impressed by Microsoft's introduction of Assemblies as a specific mechanism for configuration and deployment. I also said that we would start to examine the features in MSBuild and ClickOnce deployment. In this article we will introduce MSBuild and examine some of its cool features. 
