Reputation: 102893
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>
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.
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?
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?
Test program:
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;
}
}
}
Upvotes: 9
Views: 11412
Reputation: 21
Another issue - if you call ANT within a BAT file and you are interested in saving the errorlevel value, you must keep the call out of an if/else block.
For example, this won't work (...in the middle of some routine):
if "%DUMP_ARGS%"=="no" (
call ant %ANT_TARGETS%
set ANT_RETURN=%errorlevel%
)
You must break out the call as follows (...placed outside that routine):
:call_ant
call ant %ANT_TARGETS%
set ANT_RETURN=%errorlevel%
goto :eof
(...in the middle of some routine)
if "%DUMP_ARGS%"=="no" (
call :call_ant
)
Upvotes: 2
Reputation: 848
i solved replacing IF ERRORLEVEL 1
with IF %ERRORLEVEL% NEQ 0
:
call ant
IF %ERRORLEVEL% NEQ 0 GOTO :build_error
Upvotes: 0
Reputation: 19640
A quick patch is adding the following at the end of ANT.BAT file:
exit /b %ANT_ERROR%
This problem has long existed and fixed recently. Refer to Bug 41039.
It should be ready in ANT release 1.8.2 or later.
Upvotes: 2
Reputation: 26514
I solved this problem by creating a single batch file (quite a bit nicer than the above but still mysterious):
Content of file 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
.
Upvotes: 7
Reputation: 1638
I googled to this thread and find tangens' answer almost solved my problem: the exit coded are always 0 on Windows from ant.bat
, even when I intentionally failed an ant build; I need to get the exit code from a TFS build script, and it showed that even the %ERRORLEVEL%
can't be found in TFS build.
However, I have to remove the /B
from exit
lines, otherwise, it still always shows me exit code 0.
Upvotes: 0
Reputation: 39733
I solved this problem by creating two extra batch files (not very nice, but it works):
Content of file myant.bat
:
call ant2.bat %*
Content of file ant2.bat
:
call ant.bat %*
if errorlevel 1 (goto ERROR_EXIT)
exit /B 0
:ERROR_EXIT
exit /B 1
Now I can call myant.bat
as a Process
from java and I get the correct exit value.
Sorry, I cannot say why this works. It's simply the result of a many many tries.
Upvotes: 2
Reputation: 84058
This is a long-standing issue for older versions of Ant on Windows. I believe it has been fixed in version 1.7.0.
See this bug for details and this discussion for an approach to address it.
Upvotes: 0
Reputation: 403551
Do you need to run Ant via its .bat file? It's just a java program, you could just execute in inside the VM by directly instantiating and executing the Ant runtime. Have a look inside ant.bat, see what its Main class is, and execute it directly.
Upvotes: 1