Visual Studio: Multiple post-build commands?
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.
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.
Is that correct, or is there a simpler way to in开发者_Go百科voke two commands from the post-build event? Thanks in advance for your help.
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.
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.
e.g.:
call MyBatch1.bat
call MyBatch2.bat
There is another option: you can separate the commands with &&
.
E.g.
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.
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.
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.
Adding to womp's answer:
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.
Personally I do this for all build 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.
In Visual Studio 2017, you can do this:
<PostBuildEvent>
<Command>
copy $(TargetPath) $(SolutionDIr)\bin1
copy $(TargetPath) $(SolutionDIr)\bin2
</Command>
</PostBuildEvent>
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:
<Target Name="AfterResolveReferences">
<Exec Command="path\MyFirstCommand.exe -parameters" />
</Target>
<Target Name="MySecondCommand" AfterTargets="AfterResolveReferences" >
<Exec Command="path\MySecondCommand.exe -parameters" />
</Target>
The example above uses "AfterResolveReferences" event but should obviously work for PostBuild event too.
Just a note for idiots like me - there is this "drop down" button so you can edit commands as a multi-line text
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.
So you might be better off just using a single script, if error reporting is important.
Migrating a legacy .NET project to Visual Studio 2022. Found I was not able to run multiple commands for either Pre-Build or Post-Build. The behavior wasn't consistent when switching between Debug/Release even once I got the syntax correct. I anticipate there is possibly a bug here.
In Debug
configuration, it would error out using XCOPY /Y /F
no matter what I tried. Error code 2
and 4
being the most common while Release
worked. I had trouble gauging what was a syntax error and what was just an error. XCOPY /Y /F
worked just fine in CMD/PowerShell.
Executing multiple commands becomes possible (even with conditionals) only after "bubbling" the commands ( ... )
.
i.e. going from this
copy $(TargetDir)x64\SQLite.Interop.dll $(TargetDir)SQLite.Interop_x64.dll
copy $(TargetDir)x86\SQLite.Interop.dll $(TargetDir)SQLite.Interop_x86.dll
if $(ConfigurationName) == Release (
call "C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool\signtool.exe" sign /t http://timestamp.sectigo.com /fd sha256 $(TargetPath)
)
(copy $(TargetDir)x64\SQLite.Interop.dll $(TargetDir)SQLite.Interop_x64.dll)
(copy $(TargetDir)x86\SQLite.Interop.dll $(TargetDir)SQLite.Interop_x86.dll)
(if $(ConfigurationName) == Release (
call "C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool\signtool.exe" sign /t http://timestamp.sectigo.com /fd sha256 $(TargetPath)
))
Additional tidbit, since ( )
are the special characters, if you need to escape them, use caret, i.e. ^)
Stackoverflow: Post-build event with multiple if/copy combinations only execute if first file does not exist
In Visual Studio 2022, the UI for specifying both pre and post build events has changed slightly, but you can still specify multiple commands to run by placing each on its own line:
The above sets the following in your project file:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="echo Post 1
echo Post 2" />
</Target>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="echo Pre 1
echo Pre 2" />
</Target>
Which when built produces something resembling:
1>------ Build started: Project: MyProject, Configuration: Debug Any CPU ------
1>You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
1>Pre 1
1>Pre 2
1>MyProject -> D:\repos\MyProject\bin\Debug\net6.0\MyProject.dll
1>Post 1
1>Post 2
You can run multiple commands/batch files in Visual Studio 2022.
Just take care of do not add
exit %errorlevel%
to your batch file because that will cause cmd to exit and further post build events will not be executed by visual studio.
If you wish to return exit codes use
exit /b
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/exit
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
精彩评论