Visual Studio Build Events
Some projects require that preparatory steps be taken prior to compiling the code. others require processes to be executed after a successful build. visual Studio's build events allow such pre-build and post-Build Steps to be automated.
What are build events?
Visual Studio allows you to define Build Events for each project in a solution. these events contain commands that will be executed automatically when you compile the project. two types of event are available; each containing commands that you cocould normally run at the command line. pre-Build Events run before the compilation of the project begins. post-Build Events are executed after the build is completed, if certain specified conditions are met.
Build Events have policuses. for example, before compiling you may wish to automatically copy up-to-date resource files from a networked location into the project folder for compression sion in the build. following a successful build you may wish to copy the output files to a central server, start an automatic testing procedure or use ilmerge to combine multiple assemblies into a single library.
Macros
to provide flexibility when writing build events, Visual Studio provides a set of simple macros. these are special codes that can be inserted into the commands in the event. when the event is executed, the macros are replaced with project-specific information, such as project or solution file paths, platform information or project configuration details. this is useful when several developers are working on a project but each has a different location for the local copy of the source code.
The key macros are as follows:
-
- $ (Configurationname). The project's configuration details. This is useful if you wish to execute a different event for debug and release mode builds.
- $ (Devenvdir). The installation folder of Visual Studio.
-
- $ (Outdir). This macro is replaced with the relative path to the project's Output Folder. As with all paths, the macro outputs a trailing backslash (\).
- $ (Platformname). The name of the target platform. For example, "Any CPU ".
-
- $ (Projectdir). The absolute path to the project's folder.
-
- $ (Projectext). The extension of the project file.
- $ (Projectfilename). The filename of the project file.
-
- $ (Projectname). The name of the project.
-
- $ (Projectpath). The absolute path to the project file.
- $ (Targetdir). The absolute path to the project's primary Output Folder.
-
- $ (Targetext). The extension of the primary output file, generally ". dll" or ". EXE ".
-
- $ (Targetfilename). The filename of the project's primary output file.
- $ (Targetname). The name of the target assembly.
-
- $ (Targetpath). The absolute path to the project's primary output file, including the name of the executable or dynamic linked library (DLL ).
- $ (Solutiondir). The absolute path to the solution's folder.
-
- $ (Solutionext). The extension of the solution file.
-
- $ (Solutionfilename). The filename of the solution file.
- $ (Solutionname). The name of the solution.
-
- $ (Solutionpath). The absolute path to the solution file.
Limitations
there are some limitations that you shoshould consider when creating build events. possibly the most important of these is that error checking within the event is limited. when using events that require a series of commands to be executed, it is usually advisable to move these commands into a batch file within the project folder. the build event can be used to call the batch file, which shoshould include its own error checking.
Adding Build Events
to demonstrate the use of build events, the remainder of this article will describe the process of creating a post-build event that copies a project's compiled output files to another folder. creating a pre-build event is an almost identical process. the key difference is that pre-build events are not conditional upon the results of the build process. pre-build and post-build events are also available in Visual Studio 2003, though with a slightly difference user interface.
to add a build event, you use your project's property pages. you can open these by right-clicking the project name in the Solution Explorer pane and choosing "properties" from the context-sensitive menu that appears. you can also use the project's properties option from the project menu. once the property pages are visible, click the Build Events tab.
the Build Events page contains two multi-line textboxes where you can type the commands that you wish to execute before and after the compilation process. each textbox has an associated button that can be clicked to open a larger editing window for the event. click the edit post build button to continue with the process. A resizable editor dialog box will be displayed.
If you click the macros button you will see a list of macros that can be used within the event. these can be added to the event by selecting them and clicking the insert button, or by simply double-clicking the macro name. you can also type the macro manually if you wish. to create the post-build event, enter the command below into the main editing area. the macro copies the compiled output of the project to the C: \ Temp folder. once you have added the build event command, click the OK button to return to the project's property pages.
Copy "$ (targetdir) *. *" C: \ Temp
NB: Ensure that you have created a "Temp" folder in the root of your C: disk or use another suitable folder in the build event. the quotation marks (") in the command are used to allow paths to be generated that contain spaces.
The final option when creating a post-build event is to specify the conditions under which it will execute. The drop-down list at the bottom of the page provides three options:
- Always. The post-build event will run whenever the project is compiled. The event will execute even if the build fails.
-
- On successful build. The event will execute only if the build completes without errors.
- When the build updates the project output. The event will run only when the output files are updated by the build. This prevents the event from executing if the project is recompiled with no changes made.
Select the "on successful build" option. You can now compile the project and see that the output files are copied to the Temporary Folder automatically.