There are many occasions that I'd like to clean a large amount of projects at once. Before MSBuild it seems that you would have to do a search/replace to delete all the files/folders that you considered to be "cleanable". Even though this approach would work, you may miss files, or even worse you may delete files that truly shouldn't be deleted. Using MSBuild you can easily implement this, and you don't have to be worried about deleting important files. Let's see how you can do this, if you have a large source tree and you want to delete the "cleanable" then you can use the following MSBuild project file to achieve this.
<
Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><ItemGroup>
<Projects Include="**\*.*proj" />
ItemGroup>
<Target Name="CleanAllProjects">
<MSBuild Projects="@(Projects)" Targets="Clean" StopOnFirstFailure="false" ContinueOnError="true">
MSBuild>
Target>
Project>
Place this file at the top of the hierarchy of projects and lets name it for CleanAll.proj. This is about as simple as it gets for a usable MSBuild file. I'll explain this one, in case you're not familiar with MSBuild.
There is a lone Item, Projects, defined in this file and all project files in the current and directories below it will be included in the list. It achieves this by using the **\*.proj include declaration. Then in the CleanAllProjects target the MSBuild task is invoked. We invoke it with the Project = @(Projects). Since the @(Projects) contains many different projects the MSBuild task will be invoked once for each of those projects. The MSBuild task will invoke the Clean target on these project, and since we specified StopOnFirstFailure="false", and ContinueOnError="true" because we want the clean to continue regardless of whether there were any "errors". We can invoke this at the Visual Studio Command Line using >msbuild.exe CleanAll.proj /t:CleanAllprojects. If you are writing MSBuild projects by hand then you'll probably get some "errors" from this process because you probably have project files that don't import Microsoft.Common.targets at some point, so there will be no Clean target defined. You can ignore these "errors".
I think that this simple example demonstrates how the project being the MSBuild file is very important and very convenient. In order to get this sample to work, you don't have to plan for it ahead of time, you don't have to make any modifications to your project. You simply write the code in the same ways that you always have, but now you have another tool available to you for free. This is one thing that makes MSBuild stand out from any other build tools. The fact that they are integrated into the projects enables the developers and engineers to make things happen that typically would be complicated or very in-convenient. With this sample I can be assured that all files are removed correctly, and I don't have to worry about removing files that should remain present. Also if the projects have customizations to the clean those will be included as well. What do you think of this?
Sayed Ibrahim Hashimi
Comments are closed.