问题描述
我有一个使用退出
命令返回退出code的批处理文件。
I have a batch file that is using the exit
command to return an exit code.
这批处理文件可能在某些情况下,交互地从一个命令行调用,或者在其他情况下,可使用运行作为MSBuild项目的一部分,该 Exec的
任务。
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.
- 如果我用
退出%ERRORLEVEL%
我的批处理文件中这工作得很好,的MSBuild看到错误code,然而的互动谁是从命令窗口中运行批处理文件的用户将获得的cmd.exe一个粗鲁的出口在这种情况下。 - 如果我用
退出/ B%ERRORLEVEL%
交互式场景没有得到一个粗鲁的退出,不过这也意味着CMD
我的Exec的
任务也推出不退出,因此的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 thecmd
launched by myExec
task also does not exit, and therefore MSBuild does not see the return value.
,我试图用退出/ 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 & exit %errorlevel%" />
这个想法是,我明确采取从退出/ B
键,手动调用退出
到传播完成超出这个值的cmd.exe
其中 Exec的
生成任务可以看到它。
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从命令提示符,而不是调用它的方法。
例如,我创建了以下
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:\Data\Development\My Code\Community\MSBuild\BatchFile>test.bat
C:\Data\Development\My Code\Community\MSBuild\BatchFile>ECHO OFF
fromMSBuild:0
Exiting with exit code 101
和MSBuild中的结果是:
And from MSBuild the result is:
C:\Data\Development\My Code\Community\MSBuild\BatchFile>msbuild Wrapper.proj /t:Demo /fl /nologo
Build started 5/18/2009 10:54:52 PM.
Project "C:\Data\Development\My Code\Community\MSBuild\BatchFile\Wrapper.proj" on node 0 (Demo target(s)).
Executing batch file test.bat
fromMSBuild:1
C:\Data\Development\My Code\Community\MSBuild\BatchFile\Wrapper.proj(17,5): error MSB3073: The command "test.bat FromMSBuild" exi
ted with code 101.
Done Building Project "C:\Data\Development\My Code\Community\MSBuild\BatchFile\Wrapper.proj" (Demo target(s)) -- FAILED.
Build FAILED.
"C:\Data\Development\My Code\Community\MSBuild\BatchFile\Wrapper.proj" (Demo target) (1) ->
(Demo target) ->
C:\Data\Development\My Code\Community\MSBuild\BatchFile\Wrapper.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表彰退出codeS批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!