visual-studio 如何仅为调试版本运行 Visual Studio 生成后事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/150053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Visual Studio post-build events for debug build only
提问by JC.
How can I limit my post-build events to running only for one type of build?
如何将构建后事件限制为仅针对一种构建类型运行?
I'm using the events to copy DLL files to a local IIS virtual directory, but I don't want this happening on the build server in release mode.
我正在使用这些事件将 DLL 文件复制到本地 IIS 虚拟目录,但我不希望在发布模式下在构建服务器上发生这种情况。
回答by Joseph Daigle
Pre- and Post-Build Events run as a batch script. You can do a conditional statement on $(ConfigurationName).
构建前和构建后事件作为批处理脚本运行。您可以在 上执行条件语句$(ConfigurationName)。
For instance
例如
if $(ConfigurationName) == Debug xcopy something somewhere
回答by gbjbaanb
FYI, you do not need to use goto. The shell IFcommand can be used with round brackets:
仅供参考,您不需要使用 goto。shell IF命令可以与圆括号一起使用:
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".
)
回答by Franci Penov
Add your post build event like normal. Then save your project, open it in Notepad(or your favorite editor), and add condition to the PostBuildEvent property group. Here's an example:
像往常一样添加您的后期构建事件。然后保存您的项目,在记事本(或您喜欢的编辑器)中打开它,并将条件添加到 PostBuildEvent 属性组。下面是一个例子:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PostBuildEvent>start gpedit</PostBuildEvent>
</PropertyGroup>
回答by CestLaGalere
Alternatively (since the events are put into a batch file and 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 %1is $(OutputPath), etc.
通过这种方式,您可以为任何配置设置事件,并且仍然使用宏对其进行管理,而不必将它们传递到批处理文件中,记住%1是$(OutputPath),等等。
回答by Eric Bole-Feysot
Visual Studio 2015: The correct syntax is (keep it on one line):
Visual Studio 2015:正确的语法是(保持在一行):
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.
这里没有错误 255。
回答by Daniel Earwicker
As of Visual Studio 2019, the modern .csprojformat supports adding a condition directly on the Targetelement:
从 Visual Studio 2019 开始,现代.csproj格式支持直接在Target元素上添加条件:
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="nswag run nswag.json" />
</Target>
The UI doesn't provide a way to set this up, but it does appear to safely leave the Configurationattribute in place if you make changes via the UI.
UI 没有提供设置方法,但Configuration如果您通过 UI 进行更改,它似乎可以安全地保留该属性。
回答by Lou Franco
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).
使用$(ConfigurationName).
Checking it is based on how you are implementing the post-build step -- it will be a command-line argument.
检查它是基于您如何实施构建后步骤——它将是一个命令行参数。
回答by Jaan Marks
This works for me in Visual Studio 2015.
这在 Visual Studio 2015 中对我有用。
I copy all DLL files from a folder located in a library folder on the same level as my solution folder into the targetdirectory of the project being built.
我将所有 DLL 文件从位于与我的解决方案文件夹相同级别的库文件夹中的文件夹复制到正在构建的项目的目标目录中。
Using a relative path from my project directory and going up the folder structure two steps with..\..\lib
使用我的项目目录中的相对路径,并使用 ..\..\lib 进入文件夹结构两步
MySolutionFolder
....MyProject
Lib
MySolutionFolder
....MyProject
库
if $(ConfigurationName) == Debug (
xcopy /Y "$(ProjectDir)..\..\lib\*.dll" "$(TargetDir)"
) ELSE (echo "Not Debug mode, no file copy from lib")
回答by Harald Scheirich
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.
与任何项目设置一样,构建事件可以按配置进行配置。只需在“属性页”对话框的下拉列表中选择要更改的配置,然后编辑后期构建步骤。
回答by mawl
In Visual Studio 2012 you have to use (I think in Visual Studio 2010, too)
在 Visual Studio 2012 中你必须使用(我认为在 Visual Studio 2010 中也是如此)
if $(Configuration) == Debug xcopy
$(ConfigurationName)was listed as a macro, but it wasn't assigned.
$(ConfigurationName)被列为宏,但未分配。


Compare: Macros for Build Commands and Properties
比较:构建命令和属性的宏

