本文介绍了为什么 ant.bat 以编程方式运行时不返回错误状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从命令行运行 ant 时,如果失败,我会得到一个非零退出状态($? 在 UNIX 上,%ERRORLEVEL% 在 Windows 上).但是我们有一个运行 ant 的 Java 程序(通过 ProcessBuilder),当 ant 失败时,在 Windows 上我们无法获得退出状态.

When I run ant from the command-line, if I get a failure, I get a non-zero exit status ($? on UNIX, %ERRORLEVEL% on Windows). But we have a Java program which is running ant (through ProcessBuilder), and when ant fails, on Windows we cannot get the exit status.

我刚刚用这个简单的蚂蚁测试文件验证了这一点:

I just verified this with this simple ant test file:

<project name="x" default="a">
  <target name="a">
    <fail/>
  </target>
</project>

在 UNIX 上,运行 ant 会打印一条失败消息,并回显 $?之后打印 1.在 Windows 上,运行 ant 或 ant.bat 会打印一条失败消息,然后回显 %ERRORLEVEL% 会打印 1.

On UNIX, running ant prints a failure message, and echoing $? afterward prints 1.On Windows, running ant or ant.bat prints a failure message, and echoing %ERRORLEVEL% afterward prints 1.

现在,使用下面的测试程序:在 UNIX 上,java Run ant 打印失败消息,并回显 $?之后打印 1.在 Windows 上,java Run ant 找不到名为 ant 的程序来运行,但是 java Run ant.bat 打印了一条失败消息,但随后回显 %ERRORLEVEL% 打印 0.什么给?

Now, using the test program below:On UNIX, java Run ant prints a failure message, and echoing $? afterward prints 1.On Windows, java Run ant can't find a program named ant to run, but java Run ant.bat prints a failure message, yet echoing %ERRORLEVEL% afterward prints 0. What gives?

我们依赖于在运行 ant 后能够检查退出状态.无论如何,我们是.为什么我们不能以编程方式依赖它?

We're relying on being able to check the exit status after running ant. We were, anyway. Why can't we rely on this, programmatically?

测试程序:

import java.io.*;

public class Run {
  public static void main(String[] args) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process p = pb.start();
    ProcThread stdout = new ProcThread(p.getInputStream(), System.out);
    ProcThread stderr = new ProcThread(p.getErrorStream(), System.err);
    stdout.start();
    stderr.start();
    int errorLevel = p.waitFor();
    stdout.join();
    stderr.join();
    IOException outE = stdout.getException();
    if (outE != null)
      throw(outE);
    IOException errE = stdout.getException();
    if (errE != null)
      throw(errE);
    System.exit(errorLevel);
  }

  static class ProcThread extends Thread {
    BufferedReader input;
    PrintStream out;
    IOException ex;

    ProcThread(InputStream is, PrintStream out) {
      input = new BufferedReader(new InputStreamReader(is));
      this.out = out;
    }

    @Override
    public void run() {
      String line;
      try {
        while ((line = input.readLine()) != null)
          out.println(line);
      } catch (IOException e) {
        setException(e);
      }
    }

    private void setException(IOException e) {
      this.ex = e;
    }

    public IOException getException() {
      return ex;
    }
  }
}

推荐答案

我通过创建一个单个批处理文件解决了这个问题(比上面的好一点,但仍然很神秘):

I solved this problem by creating a single batch file (quite a bit nicer than the above but still mysterious):

文件myant.bat的内容:

@echo off
rem RunAnt simply calls ant -- correctly returns errorlevel for callers
call ant.bat %*

至关重要的是,在调用之后没有任何代码.

crucially, without any code whatsoever after the call.

这篇关于为什么 ant.bat 以编程方式运行时不返回错误状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:59