MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA...
-
Upload
britney-kelly -
Category
Documents
-
view
235 -
download
1
Transcript of MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA...
![Page 1: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/1.jpg)
MSBuild Unveiled
Peter SchneiderMVP Visual Developer – Visual C#MCT, MCSD.NET, MCAD.NET, [email protected]
![Page 2: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/2.jpg)
In This Session…
MSBuild Architecture
MSBuild – File Format
MSBuild – Tips & Tricks
![Page 3: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/3.jpg)
Au
tho
rs
PROJECT FILE - $%#^$&% - @$#%$^#
Abracadabra
Feed
s
Visual Studio .NET 2002/2003
VS Build System
Produces
Final Product
Pre build step0011010101111001011011011001110010100111Post build step
![Page 4: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/4.jpg)
Final Product
Produces
Feeds
Authors
DEVELOPER
Authors
MSBuild Design Goals
MSBuild
PROJECT FILE<Project> <Property … /> <Item … /> <Target … /></Project>
![Page 5: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/5.jpg)
MSBuild in 5 minutes The underlying build engine in Visual
Studio 2005 Fully open and published XML file
format for describing build Visual Studio 2005 build is fully
customizable You extend the build by writing
managed code (tasks and loggers) You don’t need the IDE to build Visual
Studio projects
![Page 6: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/6.jpg)
MSBuild File Format<Project xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”> <PropertyGroup> <AppName>MyCoolApp</AppName> <DebugSymbols>true</DebugSymbols> <OutputAssembly>$(AppName).exe</OutputAssembly> </PropertyGroup> <ItemGroup> <Compile Include=“Hello.cs” /> <Compile Include=“Program.cs” /> </ItemGroup> <Target Name=“Build”> <Message Text=“Executing Build Target for App $(AppName)” /> <Csc Sources=“@(Compile)” EmitDebugInformation=“$(DebugSymbols)”
OutputAssembly=“$(OutputAssembly)”/> </Target> <Import Project=“Microsoft.CSharp.targets” />
</Project>
![Page 7: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/7.jpg)
MSBuild Key Components
Items
Properties
Targets
Tasks
![Page 8: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/8.jpg)
Build Items Represent Input to the build system Grouped into Item Collections
Use userdefined CollectionNames Are used as Parameters for Tasks
Use Include and Exclude Attributes, Wildcards (**,*,?)
<ItemGroup> <Compile Include=“Hello.cs” /> <Compile Include=“Program.cs” /> </ItemGroup>
![Page 9: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/9.jpg)
Build Items
Reference ItemGroups with@(ItemCollectionName)
Example:
<Csc Sources=„@(Compile)“/>
<ItemGroup> <Compile Include=“Hello.cs” /> <Compile Include=“Program.cs” /> </ItemGroup>
![Page 10: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/10.jpg)
Build Item Metadata
Items may contain MetaData
<ItemGroup> <Compile Include=“Hello.cs”> <Group>1</Group> </Compile> <Compile Include=“Program.cs”> <Group>2</Group> </Compile></ItemGroup>
Used for batching<Target Name=“Testbatch”> <Message Text=“@(Compile)” Condition=“ ’%(Group)’ == ‘1’ ”/></Target>
![Page 11: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/11.jpg)
Well Known Item Meta Data
%(FullPath)%(RootDir)%(Filename)%(Extension)%(RelativeDir)%(Directory)
%(RecursiveDir)%(Identity)%(ModifiedTime)%(CreationTime)%(AccessedTime)
<ItemGroup> <Compile Include=“*.cs” /></ItemGroup>
<Target Name=“BackupSources”> <Copy SourceFiles=“@(Compile)”
DestinationFiles=“%(Compile.Filename).bak” /></Target>
![Page 12: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/12.jpg)
Build Properties
Properties are key/value pairs used to configure builds
Defined in PropertyGroups
<PropertyGroup> <Configuration>Debug</Configuration></PropertyGroup>
Reference Properties with$(Propertyname)
e.g. $(Configuration)
![Page 13: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/13.jpg)
Reserved Properties
MSBuildProjectDirectory MSBuildProjectFile MSBuildProjectExtension MSBuildProjectFullPath MSBuildProjectName MSBuildBinPath MSBuildProjectDefaultTargets MSBuildExtensionsPath
Some PropertyNames are reserved:
Environment Variables are accessible as Properties
![Page 14: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/14.jpg)
Setting Properties
You can set Properties from the commandlineMSBuild.exe MyProject.proj /p:Configuration=Debug
MSBuild.exe MyProject.proj /p:Configuration=Debug;Another=Test
You can set Properties depending on other properties
<DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>
![Page 15: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/15.jpg)
Build Targets
Targets group tasks together in a particular order
Allows sections of the build process to be called individually (Commandline, CallTarget Task)
<Target Name=“Compile”> <Csc Sources=“@(SourceFiles)”/></Target>
MSBuild.exe MyProject.proj /t:CompileMSBuild.exe MyProject.proj /t:Clean;Compile
![Page 16: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/16.jpg)
Default Targets
Use DefaultTargets Attribute
<Project DefaultTargets=“Compile”>...</Project>
InitialTargets are executed first
<Project InitialTargets=“Clean”>...</Project>
![Page 17: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/17.jpg)
Incremental Builds
Reduce build time Only builds targets which are out-of-
date or not yet built Specify Inputs and Outputs of the
target
<Target Name=“Compile” Inputs = “@(SourceFiles)” Outputs = “HelloWorld.exe” >
<Csc Sources=“@(SourceFiles)” OutputAssembly=“HelloWorld.exe”/>
</Target>
![Page 18: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/18.jpg)
Build Tasks
A Task is a unit of executable code which performs atomic build operations<Target Name=“MakePublishDirectory”>
<Message Text=“Creating Publish Directory”/> <MakeDir Directories=“$(PublishDir)”/>
</Target>
![Page 19: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/19.jpg)
Build Task Outputs
Task can return Output mapped to Items or Properties
<Target Name="CopyFiles"> <Copy SourceFiles="@(MySourceFiles)" DestinationFolder="@(MyDestFolder)">
<Output TaskParameter="CopiedFiles" ItemName="SuccessfullyCopiedFiles"/>
</Copy> <Message Text=“@(SuccessfullyCopiedFiles,”,”)”></Target>
![Page 20: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/20.jpg)
Build Task ContinueOnError
Specify the ContinueOnError Attribute to succeed target for non-critical tasks
Other possibilities:
<Target Name=“SampleTarget"> <TaskOne ContinueOnError="false"> </TaskOne> <TaskTwo> </TaskTwo> <OnError ExecuteTargets="OtherTarget" /> </Target>
![Page 21: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/21.jpg)
Project Elements Use Choose, When, Otherwise
Elements<Project xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”>
<PropertyGroup> <SampleProperty>Test</SampleProperty></PropertyGroup>
<Choose> <When Condition=“’$(SampleProperty)’ ==‘Test’”> <PropertyGroup> </PropertyGroup> </When> <Otherwise> </Otherwise></Choose>
</Project>
![Page 22: MSBuild Unveiled Peter Schneider MVP Visual Developer – Visual C# MCT, MCSD.NET, MCAD.NET, MCDBA ps@ugwa.net.](https://reader035.fdocuments.net/reader035/viewer/2022062309/5697c02c1a28abf838cd9016/html5/thumbnails/22.jpg)
Summary
Properties Items Targets Tasks Projects