Conditional Build Events in Visual Studio .NET

January 13, 2006

I ran across an situation today when I wanted to do a build event, but only if my solution was in a specific configuration. Specifically, when the project was in the UnitTest configuration, I wanted to copy a unit test assembly into the main projects output folder so that a number of plugin classes provided by the unit test assembly could be used by the main project. I also wanted to copy in some unit test based configuration.

Unfortunately Visual Studio .NET does not keep Pre and Post Build Events within a configuration’s settings, they are stored globally across all configurations. Thankfully, given that build events simply get written to a batch file, then invoked you can use your 3L337 batch file skillz to perform conditional build events.

IF NOT $(ConfigurationName) == UnitTest GOTO end</p>

:: copy unit test assembly to the service folder.
echo Copying “$(TargetDir)UnitTests.dll” to “$(ProjectDir)..\MyProject\bin\debug\”
copy “$(TargetDir)UnitTests.dll” “$(ProjectDir)..\MyProject\bin\debug\”

:end
echo Finished Post Build Event

So, what does this bit of script do? Firstly it compares the current configuration to the configuration that I want to perform my build event for. Secondly, it negates this comparison, so that if the configuration is not what i’m looking for the branch jumps to the end without executing the build event. Lastly, if the configurations match, it performs the real actions of the build event.

It is a bit of a pain having to write this within a build event. It would be less of a pain if they pointed out a way of doing it within the help file for the build event editor. Configuration-based build events would be perfect, so the logic is performed by Visual Studio!