On Github ArturDorochowicz / presentation-introduction-to-msbuild
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Foo; Bar" InitialTargets="Baz"> <Import Project="MyCustom.targets" /> </Project>
<Target Name="MyTarget" AfterTargets="Foo" BeforeTargets="Bar" DependsOnTargets="Baz; Qux"> <CallTargets Targets="Foobar" /> </Target>Targets group tasks together in a particular order and allow the build process to be factored into smaller units.
<PropertyGroup> <FirstName>John</FirstName> <LastName>Doe<LastName> <FullName>$(FirstName) $(LastName)<FullName> </PropertyGroup>Property names are not case sensitive.
http://msdn.microsoft.com/en-us/library/vstudio/ms164309.aspx
Reserved properties - cannot be overwritten. Well-known properties - can be overwritten. Common VS projects properties: http://msdn.microsoft.com/en-us/library/vstudio/bb629394.aspxSystem.String instance methods
<PropertyGroup> <Sample>FooBar</Sample> <Sub>$(Sample.Substring(0, 3))</Sub> <Contains>$(Sample.Contains("Bar")</Contains> </PropertyGroup>
All static methods and properties on selected .NET types
<PropertyGroup> <NewGuid>$([System.Guid]::NewGuid())</NewGuid> <Today>$([System.DateTime]::Now.ToString("yyyy-MM-dd"))</Today> <Power>$([System.Math]::Pow(2,8))</Power> <TempFile>$([System.IO.Path]::GetTempFileName())</TempFile> </PropertyGroup>
Selected methods and properties on: System.Environment, System.IO.Directory, System.IO.File
<PropertyGroup> <Value> $([MSBuild]::MakeRelative( $(FileOrFolderPath1), $(FileOrFolderPath2))) $([MSBuild]::GetRegistryValue( 'HKEY_LOCAL_MACHINE\SOFTWARE\(SampleName)', '(SampleValue)')) $([MSBuild]::Add($(NumberOne), $(NumberTwo))) $([MSBuild]::BitwiseAnd( 32, $([System.IO.File]::GetAttributes(tempFile)))) </Value> </PropertyGroup>
Lists of values, usually files
<ItemGroup> <File Include="path\to\file.txt" /> <Files Include="path\to\file.txt; path\to\another.txt" /> <FilesWildcard Include="path\to\*.txt" /> <FilesRecursiveWildcard Include="path\**\*.txt" /> <SomeFilesButNotOthers Include="path\to\*.txt" Exclude="path\to\not-this-one.txt" /> <FilesAdd Include="path\to\fileA.txt" /> <FilesAdd Include="path\to\fileB.txt; fileC.xml" Exclude="**\*.txt" /> </ItemGroup>
<!-- C:\MyProject\Source\Program.cs --> <ItemGroup> <MyItem Include="Source\Program.cs" /> </ItemGroup>
http://msdn.microsoft.com/en-us/library/vstudio/ms164313.aspx
<ItemGroup> <Reference Include="MyAssembly"> <Private>True</Private> <HintPath>..\lib\MyAssembly.dll</HintPath> </Reference> </ItemGroup> <ItemGroup> <Compile Include="Global.asax.cs"> <DependentUpon>Global.asax</DependentUpon> </Compile> </ItemGroup>
<ItemDefinitionGroup> <MyItem> <Custom1>1</Custom1> <Custom2>2</Custom2> </MyItem> </ItemDefinitionGroup> <ItemGroup> <MyItem Include="file.txt"> <Custom2>other</Custom2> <Custom3>3</Custom3> </MyItem> </ItemGroup>
<ItemGroup> <MySourceFiles Include="c:\MySourceTree\**\*.*" /> </ItemGroup> <Target Name="CopyFiles"> <Copy SourceFiles="@(MySourceFiles)" DestinationFiles="@(MySourceFiles-> 'c:\MyDestinationTree\%(RecursiveDir)%(Filename)%(Extension)')" /> </Target>Tasks may only be used within targets.
http://msdn.microsoft.com/en-us/library/7z253716(v=vs.100).aspx
<UsingTask TaskName="Microsoft.TeamFoundation.Build.Tasks.GetBuildProperties" AssemblyFile="$(TeamBuildRefPath)\Microsoft.TeamFoundation.Build.ProcessComponents.dll" /> <Target Name="MyTarget"> <GetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)"> <Output TaskParameter="BuildNumber" PropertyName="BuildNumber" /> <Output TaskParameter="SourceGetVersion" PropertyName="SourceGetVersion" /> </GetBuildProperties> </Target>
<UsingTask TaskName="FilterList" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> <ParameterGroup> <ListToFilter ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> <Filter Required="true" /> <FilteredList ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" /> </ParameterGroup> <Task> <Using Namespace="System.Text.RegularExpressions" /> <Code Type="Fragment" Language="cs"> <![CDATA[ var results = ListToFilter .Where(x => Regex.IsMatch(x.ItemSpec, Filter)); FilteredList = results.ToArray(); ]]> </Code> </Task> </UsingTask>
<ItemGroup> <Source Include="file.txt; file.cs" /> </ItemGroup> <Target Name="MyTarget"> <FilterList ListToFilter="@(Source)" Filter="\.cs$"> <Output ItemName="_FilteredList" TaskParameter="FilteredList" /> </FilterList> </Target>
<PropertyGroup Condition="'$(Configuration)' == 'Release'"> <MvcBuildViews>true</MvcBuildViews> </PropertyGroup> <PropertyGroup> <OutDir Condition="'$(OutDir)' == ''"> $(MSBuildProjectDirectory)\bin</OutDir> </PropertyGroup>
Operators
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" InitialTargets="ValidateSolutionConfiguration;ValidateToolsVersions;ValidateProjects"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\SolutionFile\ImportBefore\*" Condition="'$(ImportByWildcardBeforeSolution)' != 'false' and exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\SolutionFile\ImportBefore')" /> <Import Project="$(MSBuildProjectDirectory)\before.MySln.sln.targets" Condition="exists('before.MySln.sln.targets')" /> <!-- Properties/Items/Targets here --> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\SolutionFile\ImportAfter\*" Condition="'$(ImportByWildcardBeforeSolution)' != 'false' and exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\SolutionFile\ImportAfter')" /> <Import Project="$(MSBuildProjectDirectory)\after.MySln.sln.targets" Condition="exists('after.MySln.sln.targets')" /> </Project>http://sedodream.com/2010/10/22/MSBuildExtendingTheSolutionBuild.aspx
msbuild.exe MySolution.sln /target:Rebuild /property:Configuration=Release /property:DeployOnBuild=True /verbosity:diagnostic /maxcpucount /nodereuse:false
Properties set on the command line become constants
http://msdn.microsoft.com/en-us/library/vstudio/ms164311(v=vs.100).aspx
Properties set from command line are constants. Percent encoding.C:\Windows\system32\reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\4.0" /v DebuggerEnabled /d true C:\Windows\SysWOW64\reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\4.0" /v DebuggerEnabled /d true
and Turn on Enable Just My Code in VS Options/Debugging/General
Then
msbuild.exe ... /debug
or
SET MSBUILDDEBUGGING=1http://blogs.msdn.com/b/visualstudio/archive/2010/07/06/debugging-msbuild-script-with-visual-studio.aspx http://blogs.msdn.com/b/visualstudio/archive/2010/07/09/debugging-msbuild-script-with-visual-studio-2.aspx http://blogs.msdn.com/b/visualstudio/archive/2010/07/09/debugging-msbuild-script-with-visual-studio-3.aspx
https://github.com/ArturDorochowicz/presentation-introduction-to-msbuild