Paul
Paul

Reputation: 20061

In Ant, how do I suppress the exec error "CreateProcess error=2"?

I'm attempting to execute a program, and if that fails I have a fallback method to get the information required. Not everyone who uses this build script will have the program installed. My task has the following:

<exec executable="runMe"
      failonerror="false"
      failifexecutionfails="false"
      outputproperty="my.answer"
      errorproperty="my.error"
      error="myerror.txt" />

Apparently I misunderstand the Ant manual because I thought by setting error or errorproperty the error would be redirected and not shown on the screen.

Is it possible to hide the message "Execute failed: java.io.IOException: Cannot run program "runMe"..."?

Alternately, is there a way to determine if that program can be run without checking for its existence? If the program is on the user's system it won't be in the same place from user to user.

Thank you,

Paul

Upvotes: 1

Views: 1729

Answers (2)

Paul
Paul

Reputation: 20061

The goal for doing this was to get the Subversion revision for my project in Ant. Some developers have command line Subversion installed and others don't so I needed a way to test for the presence of svnversion.

In the end I used a batch file to test the command:

REM File checkCommand.bat
@echo off
%1 >NUL 2 >NUL
if errorlevel 1 goto fail

REM command succeeded 
exit 0

:fail
REM command failed
exit 1

In my Ant target I run this like so:

<target name="checkForSvnversion">
  <local name="cmdresult" />
  <exec dir="." executable="com"
        resultproperty="cmdresult"
        failonerror="false"
        failifexecutionfails="false">
      <arg line="/c checkCommand.bat svnversion" />
  </exec>

  <condition property="exec.failed">
    <equals arg1="${cmdresult}" arg2="1" trim="true" />
  </condition>
</target>

I have two targets that depend on this result:

<target name="getRevisionFromSvnversion" depends="checkForSvnversion"
        unless="exec.failed">
  etc etc
</target>

and

<target name="getRevisionFromEntries" depends="checkForSvnversion"
        if="exec.failed">
  etc etc
</target>

Finally, the task I call to get the revision is:

<target name="getRevision"
        depends="getRevisionFromSvnversion,getRevisionFromEntries">
  <echo>My rev is ${svn.revision}</echo>
</target>

Upvotes: 0

karoberts
karoberts

Reputation: 9938

Try ant-contrib's try/catch/finally commands.

http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html

Upvotes: 1

Related Questions