Click here to Skip to main content
12,698,024 members (24,265 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked

MsBuild Notes

, 14 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
MsBuild notes

MSBuild MSDN reference

Script everything but compile 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 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)' 

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 the item group compared to the $ to access properties.
See stack overflow for more information.

Exlcuding Files with WildCards

    <ReportFiles Include="MaxCut.Reports.*.dll" 

**Be aware of when the files are 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 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 information on this, read this link.

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


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

You may also be interested in...

Comments and Discussions

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