C# Visual Studio:多个构建后命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1992536/
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
Visual Studio: Multiple post-build commands?
提问by David Veeneman
Visual Studio 2008 lets me declare a command and attach it to the post-build event for a project. Like a lot of developers, I use it regularly to xcopy files to the application output directory.
Visual Studio 2008 允许我声明一个命令并将其附加到项目的生成后事件。像许多开发人员一样,我经常使用它来将文件 xcopy 到应用程序输出目录。
I am working on a project where I need to xcopy files from two different places to two different destinations, all within a single project. In other words, I need to invoke two different xcopy commands from the same post-build event. It looks like the post-build event will only take a single command, and that if I need to invoke multiple commands, I will have to put the commands in a *.bat file and call that from the post-build event.
我正在处理一个项目,我需要将文件从两个不同的地方 xcopy 到两个不同的目的地,所有这些都在一个项目中。换句话说,我需要从同一个构建后事件调用两个不同的 xcopy 命令。看起来 post-build 事件只需要一个命令,如果我需要调用多个命令,我将不得不将这些命令放在 *.bat 文件中,并从 post-build 事件中调用它。
Is that correct, or is there a simpler way to invoke two commands from the post-build event? Thanks in advance for your help.
这是正确的,还是有更简单的方法来从构建后事件调用两个命令?在此先感谢您的帮助。
采纳答案by womp
You can type in as many post build commands as you want. Just separate them by newlines.
您可以根据需要输入任意数量的后期构建命令。只需用换行符将它们分开。
Here's an example from one of my projects.
这是我的一个项目中的一个例子。
回答by Lisa
Each command should be on a separate line. What I found though is that if there's an error executing one of those commands the whole post-build fails and so you'll need to try each post-build command one at a time to debug.
每个命令都应该在一个单独的行上。但我发现,如果在执行其中一个命令时出现错误,整个构建后就会失败,因此您需要一次尝试每个构建后命令进行调试。
回答by huha
Important: When executing a batch file, you must use the "call" statement on order the following lines to be executed. If you don′t use "call", the execution goes into the .bat and doesn′t return to the following lines. Same as on DOS prompt.
重要提示:执行批处理文件时,必须使用“调用”语句来执行以下行。如果您不使用“call”,则执行会进入 .bat 并且不会返回到以下几行。与 DOS 提示符相同。
e.g.:
例如:
call MyBatch1.bat
call MyBatch2.bat
回答by Steed
There is another option: you can separate the commands with &&
.
E.g.
还有另一种选择:您可以使用&&
. 例如
copy $(TargetPath) d:\folder1 && copy $(TargetPath) d:\folder2
copy $(TargetPath) d:\folder1 && copy $(TargetPath) d:\folder2
This is not exactly the same as separating with newlines: with &&
, if the previous command failed, next commant will not run.
这与用换行符分隔并不完全相同:用&&
,如果上一个命令失败,则下一个命令将不会运行。
Separating by newlines is easier to read, so you should prefer it. However I know at least one case when &&
is useful. It is the scenario, when you use property sheets to have different post-build steps on different machines. VS 2008 doesn't allow setting PostBuildStep in property sheets directly, but you can add a user macro with your command and call it from the main project settings. A macro is single line, so you can use &&
to have multiple commands there.
用换行符分隔更容易阅读,所以你应该更喜欢它。但是我知道至少一种情况何时&&
有用。这是一种场景,当您使用属性表在不同的机器上具有不同的构建后步骤时。VS 2008 不允许直接在属性表中设置 PostBuildStep,但您可以使用命令添加用户宏并从主项目设置中调用它。宏是单行的,因此您可以在其中使用&&
多个命令。
回答by msKing
Just prefix "call " to your batch script. So that statements below the Batch script is also executed after returning the call from batch script.
只需在批处理脚本中添加“调用”前缀即可。这样批处理脚本下面的语句也会在从批处理脚本返回调用后执行。
call Script1.cmd
call Script2.bat
回答by Max Truxa
If you have multiple property sheets with something to do on the same build event you can do the following to chain the commands:
如果您有多个属性表可以在同一个构建事件上执行某些操作,则可以执行以下操作来链接命令:
%(Command)
echo foo
where %(Command)
expands to the previous value of the command.
where%(Command)
扩展到命令的前一个值。
Personally I do this for allbuild events, even if I currently do not have inherited commands, because this ensures there will be no problems if I add property sheets later on.
我个人对所有构建事件都这样做,即使我目前没有继承的命令,因为这样可以确保以后添加属性表时不会出现问题。
回答by Crulex
The approach suggested by womp works in Visual Studio 2015/2017 (Windows), but doesn't work in Visual Studio for Mac (Preview), which seems to execute only the first of the commands. The only approach that I found working in both Mac and Windows versions of Visual Studio was chaining 2 MSBuild commands:
womp 建议的方法适用于 Visual Studio 2015/2017 (Windows),但不适用于 Visual Studio for Mac(预览版),它似乎只执行第一个命令。我发现在 Mac 和 Windows 版本的 Visual Studio 中工作的唯一方法是链接 2 个 MSBuild 命令:
<Target Name="AfterResolveReferences">
<Exec Command="path\MyFirstCommand.exe -parameters" />
</Target>
<Target Name="MySecondCommand" AfterTargets="AfterResolveReferences" >
<Exec Command="path\MySecondCommand.exe -parameters" />
</Target>
The above example uses "AfterResolveReferences" event but should obviously work for PostBuild event too.
上面的示例使用“AfterResolveReferences”事件,但显然也适用于 PostBuild 事件。
回答by Richard Meadows
There isn't a good solution to this problem. The call idea does cause other scripts to run. I have noticed that error detection will not work. Put 'exit /b 1' into FailMe.cmd The use 'call FailMe.cmd' in the post build steps. Notice the build does not fail? I am using VS 2017 building a C# project. Now try it with 'FailMe.cmd' The build now reports an error.
这个问题没有很好的解决方案。调用的想法确实会导致其他脚本运行。我注意到错误检测不起作用。将 'exit /b 1' 放入 FailMe.cmd 在后期构建步骤中使用 'call FailMe.cmd'。注意构建没有失败?我正在使用 VS 2017 构建 C# 项目。现在用 'FailMe.cmd' 试试 构建现在报告错误。
So you might be better off just using a single script, if error reporting is important.
因此,如果错误报告很重要,您最好只使用一个脚本。
回答by Indra
Separating the commands with & or && or ; does not work in VS2017. Can't belive such simple functionality is not available in VS2017. Visual studio tries to execute the entire text in the post build event window as one string. Only option for me now is to create a batch script which I do not particularly like.
用 & 或 && 或 ; 分隔命令 在 VS2017 中不起作用。无法相信 VS2017 中没有这么简单的功能。Visual Studio 尝试将构建后事件窗口中的整个文本作为一个字符串执行。我现在唯一的选择是创建一个我不太喜欢的批处理脚本。
回答by Jonathan Weesner
In Visual Studio 2017 you can do this:
在 Visual Studio 2017 中,您可以执行以下操作:
<PostBuildEvent>
<Command>
copy $(TargetPath) $(SolutionDIr)\bin1
copy $(TargetPath) $(SolutionDIr)\bin2
</Command>
</PostBuildEvent>