遵守 msbuild 调用的批处理文件中的退出代码 [英] Honoring exit codes from batch files invoked by msbuild

查看:15
本文介绍了遵守 msbuild 调用的批处理文件中的退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,它使用 exit 命令返回退出代码.

I have a batch file that is using the exit command to return an exit code.

在某些情况下,可以从命令行以交互方式调用此批处理文件,或者在其他情况下,可以使用 Exec 任务作为 MSBuild 项目的一部分运行.

This batch file may, in some cases, be invoked interactively from a commandline, or in other cases, may be run as part of an MSBuild project, using the Exec task.

  • 如果我在批处理文件中使用 exit %errorlevel% 效果很好,并且 MSBuild 会看到错误代码,然而一个交互式用户正在从在这种情况下,命令窗口会粗鲁地退出 cmd.exe.
  • 如果我使用 exit/b %errorlevel% 交互式场景不会粗鲁地退出,但这也意味着我的 Exec 启动的 cmd 任务也不会退出,因此 MSBuild 看不到返回值.
  • If I use exit %errorlevel% within my batch file this works well and MSBuild sees the error code, however an interactive user who is running the batch file from a command window will get a rude exit of cmd.exe in this case.
  • If I use exit /b %errorlevel% the interactive scenario does not get a rude exit, but this also means that the cmd launched by my Exec task also does not exit, and therefore MSBuild does not see the return value.

作为这两个问题的解决方案,我尝试使用 exit/b 但从我的构建脚本中启动批处理文件,如下所示:

As a solution to both problems, I am trying to use exit /b but launch the batch file from my build script like this:

<Exec Command="Batch.cmd params &amp; exit %errorlevel%" />

我的想法是我明确地从 exit/b 获取非终端"返回并手动调用 exit 以在 cmd 之外传播此值.exe Exec Build Task 可以看到的地方.

The idea being that I explicitly take the 'non-terminal' return from exit /b and manually call exit to propogate this value outside of cmd.exe where the Exec Build Task can see it.

这似乎是完美的解决方案,但它不起作用.Exec 仍然没有得到正确的错误值.

This seems like the perfect solution, however it isn't working. Exec still doesn't get the correct error value.

推荐答案

处理此问题的一种方法是让 MSBuild 将参数传递给批处理文件,以便它知道 MSBuild 正在调用它而不是从命令提示符处调用它.例如,我创建了如下所示的示例文件 test.bat

One way to handle this could be to have MSBuild pass a parameter to the batch file so that it knows that MSBuild is calling it instead of from a command prompt. For example I have created the sample file test.bat shown below

ECHO OFF

IF (%1)==() goto Start
SET fromMSBuild=1

:Start

ECHO fromMSBuild:%fromMSBuild%


REM ***** Perform your actions here *****

set theExitCode=101
GOTO End



:End
IF %fromMSBuild%==1 exit %theExitCode%


REM **** Not from MSBuild ****

ECHO Exiting with exit code %theExitCode%
exit /b %theExitCode%

我已经创建了 MSBuild 文件 wrapper.proj,它是:

And I've created the MSBuild file wrapper.proj which is:

<Project DefaultTargets="Demo" ToolsVersion="3.5"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <BatchFile>test.bat</BatchFile>
    <FromMSBuild>FromMSBuild</FromMSBuild>
  </PropertyGroup>

  <Target Name="Demo">

    <Message Text="Executing batch file $(BatchFile)" Importance="high"/>

    <PropertyGroup>
      <_Command>$(BatchFile) $(FromMSBuild)</_Command>
    </PropertyGroup>

    <Exec Command="$(_Command)">
      <Output PropertyName="CommandExitCode" TaskParameter="ExitCode"/>
    </Exec>

    <Message Text="CommandExitCode: $(CommandExitCode)"/>
    
  </Target>
</Project>

如果您从命令提示符执行文件 test.bat,结果是

If you execute the file test.bat from the command prompt the result is

C:DataDevelopmentMy CodeCommunityMSBuildBatchFile>test.bat

C:DataDevelopmentMy CodeCommunityMSBuildBatchFile>ECHO OFF
fromMSBuild:0
Exiting with exit code 101

MSBuild 的结果是:

And from MSBuild the result is:

C:DataDevelopmentMy CodeCommunityMSBuildBatchFile>msbuild Wrapper.proj /t:Demo /fl /nologo
Build started 5/18/2009 10:54:52 PM.
Project "C:DataDevelopmentMy CodeCommunityMSBuildBatchFileWrapper.proj" on node 0 (Demo target(s)).
  Executing batch file test.bat
  fromMSBuild:1
C:DataDevelopmentMy CodeCommunityMSBuildBatchFileWrapper.proj(17,5): error MSB3073: The command "test.bat FromMSBuild" exi
ted with code 101.
Done Building Project "C:DataDevelopmentMy CodeCommunityMSBuildBatchFileWrapper.proj" (Demo target(s)) -- FAILED.


Build FAILED.

"C:DataDevelopmentMy CodeCommunityMSBuildBatchFileWrapper.proj" (Demo target) (1) ->
(Demo target) ->
  C:DataDevelopmentMy CodeCommunityMSBuildBatchFileWrapper.proj(17,5): error MSB3073: The command "test.bat FromMSBuild" e
xited with code 101.

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.06

这篇关于遵守 msbuild 调用的批处理文件中的退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆