18
Tips & Tricks: Extending Tips & Tricks: Extending MSBuild with Tasks, Loggers, MSBuild with Tasks, Loggers, and Targets and Targets Faisal Mohamood Faisal Mohamood ([email protected]) ([email protected]) TLNL01 TLNL01 Program Manager - MSBuild Program Manager - MSBuild Microsoft Corporation Microsoft Corporation

Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood ([email protected]) TLNL01 Program Manager - MSBuild Microsoft Corporation

Embed Size (px)

Citation preview

Page 1: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

Tips & Tricks: Extending MSBuild Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targetswith Tasks, Loggers, and Targets

Faisal Mohamood ([email protected])Faisal Mohamood ([email protected])TLNL01TLNL01Program Manager - MSBuildProgram Manager - MSBuildMicrosoft CorporationMicrosoft Corporation

Page 2: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

2

MSBuild – Tips & TricksMSBuild – Tips & Tricks

MSBuild in 5 minutesMSBuild in 5 minutesThe underlying build engine in Visual The underlying build engine in Visual Studio 2005Studio 2005

Fully open and published XML file Fully open and published XML file format for describing buildformat for describing build

Visual Studio 2005 build is fully Visual Studio 2005 build is fully customizablecustomizable

You extend the build by writing You extend the build by writing managed code (tasks and loggers)managed code (tasks and loggers)

You don’t need the IDE to build Visual You don’t need the IDE to build Visual Studio projectsStudio projects

Page 3: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

3

MSBuild – Tips & TricksMSBuild – Tips & Tricks

MSBuild in 5 minutesMSBuild in 5 minutes<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 4: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

4

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#1: Editing a project using #1: Editing a project using Visual Studio 2005Visual Studio 2005

Opening a project file for edit is 4 Opening a project file for edit is 4 clicks away from within Visual Studio!clicks away from within Visual Studio!

When editing, you get full Intellisense When editing, you get full Intellisense based on the Project File Format based on the Project File Format SchemaSchema

1.1. Right Click on the project name in Right Click on the project name in Solution Explorer and “Unload Solution Explorer and “Unload Project”Project”

2.2. Right Click on the unloaded project Right Click on the unloaded project in Solution Explorer and “Edit in Solution Explorer and “Edit Project”Project”

Page 5: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

5

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#2: Customizing the Build #2: Customizing the Build

<ItemGroup> <PublishDir Include=“c:\temp\publish” /></ItemGroup>

<Target Name=“BeforeBuild”> <RemoveDir Directories=“@(PublishDir)” /></Target>

<Target Name=“AfterBuild”> <MakeDir Condition=“!Exists(‘@(PublishDir)’)” Directories=“@(PublishDir)” />

<Copy SourceFiles=“$(TargetPath)” DestinationFolder=“@(PublishDir)” /></Target>

How do I run pre-build and post-build How do I run pre-build and post-build steps using built-in MSBuild features?steps using built-in MSBuild features?

Page 6: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

6

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#3: Customizing with more #3: Customizing with more granularitygranularity

How do I run custom steps during How do I run custom steps during arbitrary points in the build process – arbitrary points in the build process – for example, what if I wanted to for example, what if I wanted to delete all files from c:\temp\publish delete all files from c:\temp\publish as a part of as a part of Clean Clean target?target?<ItemGroup> <PublishDir Include=“c:\temp\publish” /></ItemGroup>

<PropertyGroup> <CleanDependsOn>$(CleanDependsOn);MyClean</PropertyGroup>

<Target Name=“MyClean”> <RemoveDir Directories=“@(PublishDir)” /></Target>

Page 7: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

7

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#4: Using built-in metadata#4: Using built-in metadata

%(FullPath)%(FullPath)

%(RootDir)%(RootDir)

%(Filename)%(Filename)

%(Extension)%(Extension)

%(RelativeDir)%(RelativeDir)

%(Directory)%(Directory)

%(RecursiveDir)%(Identity)%(ModifiedTime)%(CreationTime)%(AccessedTime)

<ItemGroup> <Compile Include=“*.cs” /></ItemGroup>

<Target Name=“BackupSources”> <Copy SourceFiles=“@(Compile)”

DestinationFiles=“%(Compile.Filename).bak” /></Target>

Page 8: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

8

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#5: Copying files recursively#5: Copying files recursively

How can I use MSBuild constructs to How can I use MSBuild constructs to copy all the contents of a folder from copy all the contents of a folder from one directory to another?one directory to another?

<Project xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”>

<ItemGroup> <Files Include=“C:\foo\**\*.*” /> </ItemGroup>

<Target Name=“CopyFilesRecursively”>

<!– How do I copy everything into C:\foocopy ? --> <Copy SourceFiles=“@(Files)” DestinationFolder=“C:\foocopy\%(RecursiveDir)” />

</Target>

</Project>

Page 9: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

9

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#6: Building Incrementally#6: Building Incrementally

<ItemGroup> <XmlFile Include=“*.xml” /></ItemGroup>

<Target Name=“TransformXmlToHtml” >

<Xml2Html XmlFiles=“@(XmlFile)” StyleSheet=“stylesheet.xslt” />

</Target>

How can I run the target only to How can I run the target only to transform those XML files that have transform those XML files that have been added (or changed) since the been added (or changed) since the last build?last build?

Page 10: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

10

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#6: Building Incrementally#6: Building Incrementally

<ItemGroup> <XmlFile Include=“*.xml” /></ItemGroup>

<Target Name=“TransformXmlToHtml” Inputs=“@(XmlFile)” Outputs=“@(XmlFile->’%(Filename).html’)”>

<Xml2Html FilesToTransform=“@(XmlFile)” StyleSheet=“stylesheet.xslt” />

</Target>

For items that have a 1:1 mapping For items that have a 1:1 mapping between inputs and outputs, you can between inputs and outputs, you can use use Target Level Dependency Target Level Dependency AnalysisAnalysis by explicitly defining your by explicitly defining your inputs and outputsinputs and outputs

Page 11: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

11

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#7: Invoking one target from #7: Invoking one target from anotheranother

<Target Name=“Build” DependsOnTargets=“PreBuild”> <!– Do the build --></Target>

A target that has built once will not A target that has built once will not build again during the same build build again during the same build sessionsession

<Target Name=“PostBuild”> <!– Some post-build stuff --> <CallTarget Targets=“RunUnitTests” /></Target>

<Target Name=“PostBuild”> <!– Some post-build stuff --> <MSBuild Projects=“myproject.proj” Targets=“RunUnitTests” /></Target>

Page 12: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

12

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#8: Executing the same #8: Executing the same target multiple timestarget multiple times

How can I invoke the same target How can I invoke the same target more than once?more than once?

<Target Name=“CalculateUnitTestCoverage”> <Message Text=“Calculating Unit Test Coverage…” /> <!– Code Coverage Logic here --></Target>

<Target Name="BeforeBuild"> <CallTarget Targets="CalculateUnitTestCoverage" /></Target>

<Target Name="AfterBuild"> <CallTarget Targets="CalculateUnitTestCoverage" /></Target>

Page 13: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

13

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#8: Executing the same #8: Executing the same target multiple timestarget multiple times

Invoke the target using the Invoke the target using the MSBuildMSBuild task but with different “properties”task but with different “properties”<Target Name=“CalculateUnitTestCoverage”> <Message Text=“Calculating Unit Test Coverage for Stage $(Stage)” /> <!– Code Coverage Logic here --></Target>

<Target Name="BeforeBuild"> <MSBuild Projects=“$(MSBuildProjectFile)” Targets=“CalculateUnitTestCoverage” Properties=“Stage=BeforeBuild” /></Target>

<Target Name="AfterBuild"> <MSBuild Projects=“$(MSBuildProjectFile)” Targets=“CalculateUnitTestCoverage” Properties=“Stage=AfterBuild” /></Target>

Page 14: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

14

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#9: Gathering performance #9: Gathering performance summarysummary

““Somehow, my build has slowed Somehow, my build has slowed down tremendously. I need to find down tremendously. I need to find out what’s going on!”out what’s going on!”

C:\project> msbuild build.proj /ConsoleLoggerParameters:PerformanceSummary

C:\project> msbuild build.proj /verbosity:Diagnostic

Use the Use the /ConsoleLoggerParameters/ConsoleLoggerParameters switch or the switch or the DiagnosticDiagnostic verbosity to gather performance statistics.verbosity to gather performance statistics.

Page 15: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

15

MSBuild – Tips & TricksMSBuild – Tips & Tricks

How do I do this?How do I do this?

I’ve got a bunch of code that I’d like I’ve got a bunch of code that I’d like to open in Visual Studio, but all I have to open in Visual Studio, but all I have is a ton of source code with no Visual is a ton of source code with no Visual Studio project or solutionStudio project or solution

I’d like to open for edit and browse I’d like to open for edit and browse using Visual Studiousing Visual Studio

Is this possible? Is this possible?

Page 16: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

16

MSBuild – Tips & TricksMSBuild – Tips & Tricks

#10: Coolest trick of the #10: Coolest trick of the day!day!

<Project xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”> <ItemGroup> <Compile Include=“$(AppPath)\**\*.vb” /> </ItemGroup>

<Import Project=“$(MSBuildBinPath)\Microsoft.VisualBasic.targets” /></Project>

Simply create a .csproj or .vbproj file that Simply create a .csproj or .vbproj file that recursively adds all files to a recursively adds all files to a CompileCompile item, and additionally importsitem, and additionally imports Microsoft.CSharp.targets Microsoft.CSharp.targets or or Microsoft.VisualBasic.targetsMicrosoft.VisualBasic.targets

Page 17: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

17

MSBuild – Tips & TricksMSBuild – Tips & Tricks

Additional ResourcesAdditional ResourcesGo to the Advanced MSBuild Breakout Go to the Advanced MSBuild Breakout Session by Rajeev Goel – today @5pmSession by Rajeev Goel – today @5pm

Stop by the MSBuild table at Stop by the MSBuild table at Ask the Ask the ExpertsExperts – Thursday 6:30pm @ The Big – Thursday 6:30pm @ The Big RoomRoom

Visit the Visit the Tools & LanguagesTools & Languages Track Track Lounge (Big Room) Lounge (Big Room)

Try out the Try out the MSBuild Hands On LabMSBuild Hands On Lab here here at the PDC if you are new to MSBuildat the PDC if you are new to MSBuild

Visit the MSBuild Wiki Visit the MSBuild Wiki http://channel9.msdn.com/wiki/default.aspx/MSBuild.HomePagehttp://channel9.msdn.com/wiki/default.aspx/MSBuild.HomePage

Post a question on the MSDN Forums – Post a question on the MSDN Forums – http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=27http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=27

Send mail to [email protected] mail to [email protected]

Page 18: Tips & Tricks: Extending MSBuild with Tasks, Loggers, and Targets Faisal Mohamood (faisalmo@microsoft.com) TLNL01 Program Manager - MSBuild Microsoft Corporation

© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.