Mark Pearl

MSBuild msdn reference

Script everything but complie with MsBuild
File extension .build

Empty Build Script

<?xml version="1.0" encoding="utf-8"?>  
<project xmlns=""  

Setting the default target

<?xml version="1.0" encoding="utf-8"?>  
<project xmlns=""  

Add a DefaultsTargets attribute to the project element with the TargetName being the name of the target.


  <Target Name="Clean">  
    <RemoveDir Directories=".\buildartifacts"/>  

  <Target Name="Init" DependsOnTargets="Clean">  
    <MakeDir Directories=".\buildartifacts"/>  

Compiling a VS Project

<Target Name="Compile">  
  <MSBuild Projects=".\HelloCI.sln" Targets="Rebuild" Properties="OutDir=C:\temp\"/>  

*Important note that the outdir cannot be a relative path, it has to be an absolute path with a trailing /



Reference a the above property by using the following syntax : $(BuildDir)

You can have properties based off other properties.



You can show values of parameters by using the Message Text

<Target Name="ShowReservedProperties">  
    <Message Text=" MSBuildProjectDirectory  = $(MSBuildProjectDirectory)" />   
    <Message Text=" MSBuildProjectFile  = $(MSBuildProjectFile)" />     
    <Message Text=" MSBuildProjectExtension  = $(MSBuildProjectExtension)" />   
    <Message Text=" MSBuildProjectFullPath  = $(MSBuildProjectFullPath)" />     
    <Message Text=" MSBuildProjectName  = $(MSBuildProjectName)" />     
    <Message Text=" MSBuildBinPath  = $(MSBuildBinPath)" />     
    <Message Text=" MSBuildProjectDefaultTargets  = $(MSBuildProjectDefaultTargets)" />     
    <Message Text=" MSBuildExtensionsPath  = $(MSBuildExtensionsPath)" />   
    <Message Text=" MSBuildStartupDirectory  = $(MSBuildStartupDirectory)" />

Copying Files with Directory Structure

  <out_files Include='output_dir/**/*'/>

<Target Name='copy_files'>
  <Copy SourceFiles='@(out_files)' DestinationFolder='deployment_dir/%(out_files.RecursiveDir)'/>

Moving Files

<Target Name="MoveSomeFiles">

Moving Files with Wildcards

You cannot use regular expression directly in task parameters. You need to create an item containing list of files to move and pass its content to the task:

    <FilesToMove Include="c:\source\App_Web_*.dll"/>
<Target Name="Build">

Note the @ symbol is used to reference to item group compared to the $ to access properties.
see stack overflow for more info

Exlcuding Files with WildCards

    <ReportFiles Include="MaxCut.Reports.*.dll" Exclude="*.Interfaces.*;*.Tests.*"/>

**Be aware of when the files of created that you are including in your Item Group - if they are being created dynamically, place the ItemGroup as a child element to the element that is generating the files Read here for a further explanation

Overriding Parameters

There is a bug/feature in MSBuild which means that if you call CreateProperty and CallTarget in the same Target, your new property will not be globally available to other targets.


<Target Name="SetDeployPath-TEST">
  <CreateProperty Value="$(DeployPath_TEST)">
    <Output TaskParameter="Value" PropertyName="DeployPath"/>

<Target Name="Deploy-TEST">
   <CallTarget Targets="SetDeployPath-TEST"/>
   <CallTarget Targets="Deploy-Sub"/>

See the stack overflow notes

Including Sub Files

<Import Project=".\Base.targets" />

Imports a base project, if you do imports multiple times of the same file you will get a warning, to avoid this do the following:

<Import Project=".\Base.targets"  Condition=" '$(BaseImported)' == '' "/>

And then in the base file add a property that is recognized


For more info on this see

Executing a Build Script

Assume we had a build script called with a target inside it called compile.
To execute this specific target you would use the following command:

msbuild /target:Compile

Conditionally evaluate ItemGroup in MSBuild file

 <ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <Content Include="EnvironmentSettings\Settings.xml">

For more info

Refactoring MsBuild Scripts

Great article on refactoring here.

blog comments powered by Disqus

Want to get my personal insights on what I learn as I learn it? Subscribe now!