MsBuild cheat sheet
Target = the smallest unit of execution in msbuild
Examples use a target to print stuff to the console. Use msbuild myFile.proj /t:PrintStuff
These are your scalar variables. They behave in the same way as in imperative languages (C like ones at least).
<PropertyGroup> <Foo>Bar</Foo> </PropertyGroup> <Target Name="PrintStuff"> <Message Text="The value of Foo is: $(Foo)" /> <!-- this is how you access the var --> </Target>
All properties and items are parsed before targets are executed.
Env variables can be called in the same way.
To edit a property, redefine it inside a task.
These are your arrays. They work best when holding file paths, since you get the * wildcard and the ** which means “all subdirectories”.
<ItemGroup> <MyFiles Include="pics/sunny.png;pics/rainy.png" /> <MyFiles Include="pics/others/car.png" /> <MyFiles Include="pics/**/*.png" /> <!-- use wildcards to define the same thing --> </ItemGroup> <Target Name="PrintStuff"> MyFiles has: @(MyFiles) /> <!-- all 3 elements will be here --> </Target>
Include is like the Add method in C#. You can also use Remove to, well.., remove an element. Makes sense since you can use them inside targets.
These are your objects with a twist…
<ItemGroup> <Person Include="Person1" > <FName>Joe <Age>42</Age> </Person> <Person Include="Person2" > <FName>Mark</FName> <Age>37</Age> </Person> </ItemGroup> <Target Name="PrintStuff" Outputs="%(Person.Identity)"> <!-- this is executed in batches, i.e. a for loop is at the top--> <Message Text="We have these people: @(Person)" /> <Message Text="Details: %(Person.Fname) is %(Person.Age)" /> </Target>
Msbuild does give you some metadata to play with. Again this is oriented towards files. Details here. You can see that Identity is the value specified in the Include attribute of the item.
<!-- define an item group named Src with some files and a property Dest for the destination dir --> <Target Name="CopyFiles"> <Copy SourceFiles="@(Src)" DestinationFiles="@(Src->'$(Dest)%(Filename)%(Extension)')" /> </Target>
The example is pretty self explanatory – the destination files is defined by transforming the Src item group. Filename and Extension are well-known metadata.