Galin Iliev's blog

Software Architecture & Development

MSBuild resources

MSBuild is very powerful language for automating build process but as every computer thing it could be confusing.

So for a start this should work as Visual Studio solution files (*.sln/*.proj) files are valid MSBuild files:

MSBuild MySolution.sln

So for more script-like example this can show how complex solution with Web Application project can be build and files (result of publish operation) copied to drop location.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns=http://schemas.microsoft.com/developer/msbuild/2003 
ToolsVersion="3.5">
  <PropertyGroup>
    <OutputFiles>.\OutputFiles\</OutputFiles>
    <ReleseFolder>D:\WcfFileTransfer\Release\</ReleseFolder>
    <PrecompiledWeb>$(OutputFiles)_PublishedWebsites\WebHostApp\</PrecompiledWeb>
  </PropertyGroup>
  <Target Name="Clean">
    <Exec Command="MSBuild.exe WcfFileTransfer.sln /t:Clean" />
  </Target>
 
  <Target Name="Build">
    <Exec Command="MSBuild.exe WcfFileTransfer.sln /t:Rebuild /p:Configuration=Release 
/p:OutDir=..\$(OutputFiles)" ContinueOnError="False" />

    <ItemGroup>
      <!--include needed files-->
      <WebFiles Include="$(PrecompiledWeb)**\*.*"
                Exclude="$(PrecompiledWeb)**\developer.config;$(PrecompiledWeb)**\HOWTO*.*"/>
    </ItemGroup>
     
    <!--show message-->
    <Message Text="Copying to Deployment Dir:@(WebFiles) to $(ReleseFolder) :" />
    
    <!--perform recursive copy-->
    <Copy
            SourceFiles="@(WebFiles)"
            DestinationFiles="@(WebFiles->'$(ReleseFolder)\%(RecursiveDir)\%(Filename)%(Extension)')"  />
  </Target>
</Project>

This script is executed from Visual Studio Command Prompt like this

MSBuild build.proj

For more information  about MSBuild you can take a look at MSDN reference page or Channel9 Wiki:

* Project File Format
* Shipping Tasks
* Loggers
* Tasks
* VS / Integration
* Conversion
* MSBuild.exe Command Line
* Object Model
* Scenarios
* Does Microsoft use to build its own products?
* How to execute CS templates from MSBuild?
* External Links and Resources
* Quick Start Tutorials
* Write a simple project
* Clean my Build
* Specify which Target to Build First
* Build All Files in a Directory
* Build All Files in a Directory Except One
* Build the Same Sources with Different Options

* Use Environment Variables in a Build
* Check whether an Environment Variable has been set
* Build a Project with Resources
* Build Incrementally
* Use the Same Target in Multiple Project Files
* Tell to Ignore Errors in Tasks
* Extend with a New Task
* Build a Set of Dependant Projects
* Run a Custom Tool From my Project
* Specify Several Build Options on the Command Line
* Use Reserved XML Characters in Project Files
* Display an Item List Separated with Commas
* Convert an Item List into a scalar string
* Reference the Name or Location of the Project File in the Project File
* How do I do a recursive copy?
* Batching Examples
* MSBuild Specifications
* How the batching algorithm works
* How to find targets files in a canonical place

Or the MSBuild: By Example tutorial:

  1. Introducing Well-Known Metadata
  2. Formatting Your Output
  3. Editing MSBuild Files with IntelliSense
  4. Integrating MSBuild into Visual Studio
  5. Introducing Custom Metadata
  6. Understanding the Difference Between @ and %
  7. Using Environment Variables in Your Project
  8. Reusing MSBuild Project Elements
  9. Dealing with MSBuild Errors

I hope this is good as a start.

Loading