java - 为什么 Ant Exec 任务返回代码与 ERRORLEVEL 值不匹配?

标签 java ant batch-file

我有一个从 Ant exec 任务调用的批处理脚本来编译一些 CSharp 代码。批处理脚本的结构如下

msbuild.exe %ARGS%
echo %ERRORLEVEL%

现在当任务在 Ant 中运行时,我得到以下结果:

 [exec] Time Elapsed 00:00:09.48
 [exec] 0

 BUILD FAILED
 C:\proj\build.xml:410: exec returned: 2

怎么可能 %ERRORLEVEL% 是 0,而 Ant exec 得到的返回码是 2?如果命令不返回代码,是否设置了一些默认错误代码? Ant 文档显示:

error code 2 means 'no such program',

但显然我的批处理文件正在正确执行。

用 Ant 代码更新

<target name="build.csharp" if="isWindowsPlatform">
    <exec executable="cmd.exe" failOnError="true">
        <arg value="/c"/>
        <arg value="build.csharp.bat" />
    </exec>
</target>

最佳答案

ANT manual状态:

Errors and return codes

By default the return code of a <exec> is ignored; when you set failonerror="true" then any return code signaling failure (OS specific) causes the build to fail. Alternatively, you can set resultproperty to the name of a property and have it assigned to the result code (barring immutability, of course).

If the attempt to start the program fails with an OS dependent error code, then <exec> halts the build unless failifexecutionfails is set to false. You can use that to run a program if it exists, but otherwise do nothing.

What do those error codes mean? Well, they are OS dependent. On Windows boxes you have to look at the documentation; error code 2 means 'no such program', which usually means it is not on the path. Any time you see such an error from any Ant task, it is usually not an Ant bug, but some configuration problem on your machine.

要获取程序的返回码,您需要使用 exec taskresultproperty 属性.

关于java - 为什么 Ant Exec 任务返回代码与 ERRORLEVEL 值不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14971444/

相关文章:

java - junit日志杂乱,为什么会重复语句?

java - 检索Windows批处理文件中文件夹的绝对路径

java - 使用 Junit 进行文件操作

java - 从嵌套在 HashMap 中的 List<String> 中提取条目

android - Android 应用程序的持续交付管道

java - 在 Ubuntu 中设置 Ant home

batch-file - 如何将用户输入的字符串与批处理文件中的特殊字符进行比较?

mysql - 批处理文件时间戳问题

java - 测试我的应用程序时出现此崩溃。我是一个完整的初学者,无法理解如何解决此问题,任何解决方案?

java - 我的 Bitset 的大小是多少?