|
|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS MSBuild
Refactoring Your MSBuild Scripts
How to make maintainable processes
By: Timothy Stall
Jul. 30, 2006 12:00 PM
Digg This!
Page 1 of 2
next page »
Some things change how you fundamentally program. Automation is one of those things. It is what will save you from wasting your weekend stepping through tedious and error-prone processes like regression testing (unit, integration, performance, functional, etc.), builds, deployment, or even documentation. Automation is one of those buzzwords we all know our projects should have (like "performance," "security," "maintainability," etc...), but the question is how?
MSBuild Background
<PropertyGroup> The rest of this article will focus on the remaining three steps.
Splitting Out Scripts The Import element lets you insert one project file into another. For example, suppose you had a bunch of system-wide variables like application install paths. Getting a new version of an application may require updating its path, so you want to store all paths in a single file so it can be updated just once. You could do this by putting those paths in their own project (like "CommonProperties.proj"), and then importing that project into any script that needed it. This lets you update variables without touching each script.
CommonProperties.proj Note that the properties in the imported script get overridden by any properties of the same name in the parent script. Besides importing an entire project file, you can split your targets into separate project files and call them with the MSBuild task. The purpose of the MSBuild task is to call other targets, so we'll explain it more in our next section.
Ways to Call Targets
<MSBuild Projects="$(rootdir)\MyApp.csproj" This task has three main attributes: the physical Project(s) file, the Target(s) to call within that project, and finally any Property(or Properties) to pass to that target. The power of the MSBuild task is that you can call any MSBuild project file and pass in your own properties (we'll discuss this more in the next section). This essentially lets you abstract out your targets to separate files as if they were class libraries. When you don't need the full power of the MSBuild task, you can use the CallTarget task. Unlike the MSBuild task, it requires that the target be in the current project file (including all imported files), and it doesn't let you pass in your own property values. For example, you may have a build script that calls certain targets in a specific order:
<CallTarget Targets="GetSource" /> Yet another way to call targets is by using the "DependsOnTargets" attribute to specify which other targets the current target depends on. MSBuild is smart enough to automatically order the targets by their dependencies.
<Target Name="GetSource"> MSBuild is also smart enough to call each dependency only once. So if we added the GetSource target to the last line, so that it appears twice, it would still be called just once: <Target Name="UnitTest" DependsOnTargets="Compile;GetSource"> Note that using CallTarget is an explicit approach - you start at the first target and march forward, explicitly calling each target in the order you want. On the contrary, DependsOnTargets is an implicit approach - MSBuild starts at the end of your script and steps backwards to infer the calling order. Page 1 of 2 next page » MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
||||||||||||||||||||||||||||||||||