How to run Visual Studio post-build events for debug build only

孤者浪人 提交于 2019-11-26 12:34:38
Joseph Daigle

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 somewhere

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".
)
Franci Penov

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>

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:

Visual studio 2015: The correct syntax is (keep it on one line):

if "$(ConfigurationName)"=="My Debug CFG" ( xcopy "$(TargetDir)test1.tmp" "$(TargetDir)test.xml" /y) else ( xcopy "$(TargetDir)test2.tmp" "$(TargetDir)test.xml" /y)

No error 255 here.

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

This works for me in Visual Studio 2015.
I copy all dll-files from a folder located in a lib folder on the same level as my solution folder into the targetdirectory of the project being built.
Using a relative path from my project directory and going up the folder structure two steps with..\..\lib

MySolutionFolder
....MyProject
Lib

if $(ConfigurationName) == Debug (
xcopy /Y "$(ProjectDir)..\..\lib\*.dll" "$(TargetDir)"
) ELSE  (echo "Not Debug mode, no file copy from lib")

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

In VS 2012 you have to use (I think in VS 2010, too)

if $(Configuration) == Debug xcopy

$(ConfigurationName) was listed as a macro, but wasn't assigned.

Compare: http://msdn.microsoft.com/en-us/library/c02as0cs(v=vs.110).aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!