Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

MsBuild Notes

, 14 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
MSBuild msdn reference Script everything but complie with MsBuildFile extension .build Empty Build Script Setting the default target Add a DefaultsTargets attribute to the project element with the TargetName being the name of the target.Depenencies Compiling a VS

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

Refactoring MsBuild Scripts

Great article on refactoring here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Instructor / Trainer
South Africa South Africa
No Biography provided
Follow on   Twitter   LinkedIn

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150123.1 | Last Updated 15 Jul 2014
Article Copyright 2014 by MarkPearlCoZa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid