views:

17437

answers:

8

How can I limit my post-build events to running only for one type of build? I'm using the events to copy DLLs to a local IIS virtual directory but I don't want this happening on the build server in release mode.

+1  A: 

You can pass the configuration name to the post-build script and check it in there to see if it should run.

Pass the configuration name with $(ConfigurationName)

Checking it is based on how you are implementing the post-build step -- it will be a command-line argument

Lou Franco
+26  A: 

Add your post build event like normal. Then save you project, open it in Notepad (or your favorite editor) and add condition to the PostBuildEvent property group. Here's an example:

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <PostBuildEvent>start gpedit</PostBuildEvent>
</PropertyGroup>
Franci Penov
This works but it forces you do to all of your design work for the events in the project file source. Other conditional build event declarations are hidden from the the IDE also.
Joseph Daigle
I would have to say this is the better answer for me, the preferred method just didn't work.
Michael L
this was exactly what I was looking for. Thanks!
oz
+58  A: 

Pre- and Post-Build Events run as a batch script. You can do a conditional statement on $(ConfigurationName).

For instance

if $(ConfigurationName) == Debug xcopy something somwhere
Joseph Daigle
strange, maybe its just me but I tried adding the if condition, and now I get this error - error exited with code 255
Michael L
I've found that the entire command needs to be on one line or you'll get "exited with code 255"
Robin M
you can also use gotos / labels for a fuller solution (see my Jul 24 answwer)
GalleySlave
+1  A: 

Like any project setting the buildevents can be configured per Configuration, just select the configuration you want to change in the dropdown of the Property Pages dialog and edit the post build step

Harald Scheirich
Build Events are not specific to any configuration when created in the IDE.
Joseph Daigle
Just tested this here VS 2005, works fine for me
Harald Scheirich
VS 2008, and it doesn't work for me
dr. evil
A: 

@Michael L IF $(ConfigurationName) == Debug $(SolutionDir)reminify.bat

you need to use IF and not if

smnbss
Best to add comments to other peoples answers as comments, just like this one ;)
Ryan
shell commands are case insensitive...
BlueRaja - Danny Pflughoeft
looks like I still cannot add comments to someone else post, I can reply just on mine
smnbss
+25  A: 

alternatively (since the events are put into a batch file & then called), use the following. (in the Build event box, not in a batch file):

if $(ConfigurationName) == Debug goto :debug

:release
signtool.exe ....
xcopy ...

goto :exit

:debug
' debug items in here

:exit

This way you can have events for any configuration, and still manage it with the macros rather than having to pass them into a batch file & remember that %1 is $(OutputPath) etc:

GalleySlave
awesome, thanks!
Michael Haren
thanks microsoft, forcing us to use goto in our C# apps.
gbjbaanb
A: 

@Ryan

smnbss, like me, doesn't have sufficient reputation to add comments. (Either that, or I'm a dummy and can't figure out how to do it.)

Limited Atonement
+8  A: 

FYI, you do not need to use goto. the shell IF command can be used with round brackets:

if $(ConfigurationName) == Debug (
  copy "$(TargetDir)myapp.dll" "c:\delivery\bin" /y
  copy "$(TargetDir)myapp.dll.config" "c:\delivery\bin" /y
) ELSE (
  echo "why, Microsoft, why".
)
gbjbaanb
didn't know this great, goto is so stupid :)
dr. evil