One of the extremely useful, yet under-utilized features of Visual Studio are the project build events. Build events are commands you can run before and/or after a project build. You can further specify that they run after a successful build or any build. Any command that can be run from the command line can be used as part of a build event. These include:
- sn – strong name utility
- copy and xcopy
- conditional statements
You could even run ipconfig or ping if you wanted to. To open up the build events page, go to your project's property pages, and:
C# – Choose the Build Events tab.
VB – Choose the Compile tab, scroll to the bottom and click the "Build Events…" button.
Bang on the money, you say. Exactly, I say. But you ask, 'So I want to copy my compiled assemblies to the 12 hive, how do I know where my project assemblies are?' Ah, excellent question. A smart one aren't you? (If you're confused, rest assured that this sounded much better in my head in British English. Cheers.) But back on point, if you open up the build event dialog, and click on the Macros button, you will be presented with token variables for project and solution locations, extensions, and much more. Here are the macros available, along with values for a test throwaway project:
These macros make it incredibly easy to build custom event scripts. Here are some common build event scripts I use with SharePoint projects:
Runs the makecab utility for DDF files.
"C:\Source\Utilities\MAKECAB.EXE" /f "WSP.ddf"
Recycle the Application Pool:
%25systemroot%25\system32\iisapp.vbs /a "AppPoolName" /r
Copy file to the 12 hive:
"TEMPLATE" "%25CommonProgramFiles%25\Microsoft Shared\web server extensions\12\TEMPLATE\" /ys
Create a deployment directory:
I use this command to take a bunch of SharePoint projects, and copy the outputs to a single directory.
copy "$(ProjectDir)bin\deploy\" "$(SolutionDir)deploy\"
Register an assembly in the GAC:
@SET gacutil = "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin\gacutil.exe"
%gacutil% /iF MyAssembly.dll
The question of whether conditional statements were possible was posed to me at a local user group I presented at. The answer is yes, sort of.
Here's an example showing how to use the macros to evaluate the build type:
if $(ConfigurationName)==Release goto Release
if $(ConfigurationName)==Debug goto Debug
REM do something
REM do something
Build events provide a wonderful way to reduce or eliminate a lot of the manual steps in building, deploying and testing SharePoint solutions. Have fun!