Visual Studio: Multiple post-build commands?

后端 未结 10 1233
逝去的感伤
逝去的感伤 2020-12-13 16:47

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 applicat

相关标签:
10条回答
  • 2020-12-13 17:13

    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.

    0 讨论(0)
  • 2020-12-13 17:16

    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
    
    0 讨论(0)
  • 2020-12-13 17:16

    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.

    0 讨论(0)
  • 2020-12-13 17:21

    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.

    0 讨论(0)
  • 2020-12-13 17:26

    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 above example uses "AfterResolveReferences" event but should obviously work for PostBuild event too.

    0 讨论(0)
  • 2020-12-13 17:27

    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.

    Post Build Event Commandline

    0 讨论(0)
提交回复
热议问题