I’ve been working away at getting the next issue of my newsletter out the door. Last night, I finally finished an 11-step walk-through that I’m happy with. One of the steps discusses a novel feature of MSBuild: its ability to compose targets, and by extension, projects.
There are many cases where you want to call one target from another. Consider a simple example: you have two projects, a library project and a console application project. The console project has a dependency on the library project. Since you likely want to build your library project independently of your console project, you wind up with two separate project files.
Consider what happens when Visual Studio .NET builds the solution that contains these two projects. First, VS.NET compiles your library project. Next, it compiles the console project. But to compile it, VS.NET has to add a reference to your library project’s output assembly. Finally it has to copy the library project’s output assembly into the directory that contains the console project’s output assembly.
This introduces a problem: the console project needs to know what the library project generates. There are two reasons why this is necessary. First, the console project must add the appropriate reference to the library project’s output assembly. Second, it must copy the library project’s output assembly to the console project’s output directory.
Using existing tools, you could simply hard-code the path to the library project’s output assembly into your console project. But this introduces coupling between the two project files, which increases the brittleness of your build system, and limits your ability to compose your targets and projects.
To address the composition problem, MSBuild introduces a novel concept called an Item. An Item is a collection of strings. While this in and of itself is not novel ([N]Ant has the concept of a FileSet), the fact that MSBuild tasks have the ability to consume and emit Items is novel. For example, if you call another MSBuild project using the MSBuild task, you have the ability to capture what the Items emitted by that task are. Consider the following code fragment:
<Task Name="MSBuild"
Projects="..\lib\lib.proj">
<OutputItem TaskParameter="TargetOutputs"
Type="ReferencePath"/>
</Task>
Here, I call another project called lib.proj using the MSBuild task. When the task completes, its TargetOutputs parameter contains a list of all of the outputs created by lib.proj. The OutputItem element assigns the TargetOutputs property to a new Item called ReferencePath, which I will use in the current project file.
This begs the question: how does the MSBuild project know what the output of the lib.proj project is? Let’s examine the lib.proj project:
<Project DefaultTargets="Build">
<Target Name="Build"
Outputs="bin\Debug\app.dll">
...
</Target>
</Project>
As you can see, the default target of lib.proj contains an Outputs Item. It is this value that is passed back to the MSBuild task via its TargetOutputs parameter. Note that I have hard-coded a few things in these two hypothetical project files for clarity. In a real project, all of these things are typically parameterized.
For more details on MSBuild and my 11-step walk-through, stay tuned for the next edition of my newsletter, Practical Eye for the .NET Guy.
2 archived reader comments
Rick Childress (2003-12-04)
Why not just do this… (old school batch build script)
1) Build the library project. 2) Copy the output to a well-known folder 3) build the console project with a /lib: arg pointing to the well-known folder
LarsBerg (2003-12-22)
Because if you don’t make the dependency on the project explicit, MSBuild doesn’t know about it and can’t make sure it’s satisfied. While that’s not a big deal this second because you can strictly order all of your includes and projects, it would make it very difficult to do a multi-threaded build without this dependency information available.