I received this email from a reader of my MSBuild MSDN Article, here is his question:
I have multiple projects in multiple directories on my machine, I need to compile all of those projects and copy the resulting DLLs into my application folder which id different from the projects. Can i do all this in one MSBuild file? Most the MSBuild file samples i find work only on a single project and assume that the build file is in the same directory as the .CS files.
To answer this question you can create an MSBuild project file which uses the MSBuild task to build other project files. By using this you can extract out the files that were built during the process and place them into an item to be copied to the location of your choice. To demonstrate this I have created the following directory structure:
│ │ └───WindowsApplication1
│ │ └───Properties
│ │ ├───ClassLibrary1
│ │ │ └───Properties
│ │ └───ConsoleApplication1
│ │ └───Properties
The App1 and App2 folders each represent a product that is to be built. App1 contains a C# Win Forms project and App2 contains a C# Class Library and a C# Console App. The Deploy folder is the location that we would like to copy all of the output files to. In the BuildSeveralExample folder I have created a projects file, BuildAll.proj, which can be used to fulfill this need.
The first thing we need to do is to have a way to locate all of the project files, in my solution I created an Item and included all of the project files as such:
<ProjectsToBuild Include="**\*proj" Exclude="$(MSBuildProjectFile)"/>
I include all project files in the current directory or any subdirectory, with the exception of the current project file. The MSBuildProjectFile is an MSBuild Reserved property, for a complete list see: http://msdn2.microsoft.com/en-us/library/ms164309.aspx.
In your situation you may need to specify what files to include and exclude, to do this you can simply enter their locations separated with a semi-colon. Now that we have all the project files we’d like to build we need to actually build them. Do this by:
<MSBuild Projects ="@(ProjectsToBuild)"
<Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
This is using the MSBuild Task to each of the project files. Also note the use of the Output element here, the MSBuild Task has an output of TargetOutputs which contains a list of files which were produced by the project files built. These files are placed in the OutputFiles item. Since we didn’t specify a target to be executed on each project file, the default target will be executed. This is how Visual Studio builds your project files as well. By using the MSBuild task we can also specify properties to be passed to the project file(s) in the Properties attribute. If you need to pass different properties for different project files, then you may have to call the MSBuild Task more than once.
Now all we have to do is copy these files over to another location. This is achieved like:
The location to copy the files is specified in the DestFolder property shown above.
As well as performing these actions, we’d also like to create a fresh build when this process is invoked. To do this we can either call the Rebuild target on the project files or call Clean before we build them. I prefer the second approach, but both are equally good. It also serves for a better demonstration for me to implement the second approach.
To clean the projects all we have to do is call the Clean target on each of the project files. I have written about this in more detail in my previous entry Clean many projects and Extending the Clean. As well as that we have to remove any files that have been created by a previous invocation of this process, have a look at my clean target:
This target can be used to clean all of the projects before you build them.
It will also delete any dll & exe files located in the the Deploy folder. This could be accomplished
much better than this, but this is quick and easy.
<!-- Delete any files this process may have created from a previous execution -->
<Output ItemName="GeneratedFiles" TaskParameter="Include"/>
<MSBuild Projects="@(ProjectsToBuild)" Targets="Clean" Properties="Configuration=$(Configuration);"/>
To tie it all together I’ve created a BuildAll target which is responsible for managing all of this. Here it is:
<Target Name="BuildAll" DependsOnTargets="$(BuildAllDependsOn)"/>
I took the Visual Studio approach and defined targets to perform individual steps, created one target to call them in the correct order by using a depends list derived from a property. This is a good approach because this depends on list can be modified outside of this project file to inject other required steps. For more detailed info on that see my other entry Extending the clean.
To see this in action you have to execute the BuildAll target on the BuildAll.proj file. I’ve made the project file available as well as my sample applications that go along with it. The BuildSeveralExample.zip file contains all the necessary files.
Sayed Ibrahim Hashimi