Running Ant exec task without "Result:" output - ant

When the Ant exec task runs a command that completes with non-zero exit status, the exec task outputs the status, such as:
[exec] Result: 1
This is printed without context, and so it is confusing to users.
It is printed even if the exec task is invoked with outputproperty="someproperty" failonerror="false".
I use such an invocation for commands such as grep, where a non-zero output may be the desired, expected result.
Is there a way to suppress the "Result: " output, without making any other changes to the output of the Ant process?

Take a look at the following example which uses JavaScript to disable and restore logging.
build.xml
<project name="ant-exec-intentional-fail" default="run">
<target name="run">
<echo>Show that exec logs "Result: 1" by default...</echo>
<exec executable="sh" failonerror="false">
<arg value="-c"/>
<arg value="exit 1"/>
</exec>
<echo>Disabling logging...</echo>
<script language="javascript"><![CDATA[
var savedLoggers = project.getBuildListeners();
for( var i = 0; i < savedLoggers.length; i++ ) {
var logger = savedLoggers[i];
project.removeBuildListener( logger );
}
project.addReference( "savedLoggers", savedLoggers );
]]></script>
<exec executable="sh" failonerror="false" resultproperty="middle-sh-result">
<arg value="-c"/>
<arg value="exit 42"/>
</exec>
<!-- Restore logging. -->
<script language="javascript"><![CDATA[
var savedLoggers = project.getReference( "savedLoggers" );
for( var i = 0; i < savedLoggers.length; i++ ) {
var logger = savedLoggers[i];
project.addBuildListener( logger );
}
]]></script>
<echo>Verify logging works again...</echo>
<exec executable="sh" failonerror="false">
<arg value="-c"/>
<arg value="exit 1"/>
</exec>
<echo>By the way, the middle exec returned: ${middle-sh-result}</echo>
</target>
</project>
Output
run:
[echo] Show that exec logs "Result: 1" by default...
[exec] Result: 1
[echo] Disabling logging...
[echo] Verify logging works again...
[exec] Result: 1
[echo] By the way, the middle exec returned: 42

Late answer - but perhaps it will help someone that stumbles across this question searching the internet and does not want to completely disable logging (like Chad Nouis proposed): If one starts the program in a separate shell, the exit code can be controlled.
Ignore/Overwrite with exit code 0:
<exec executable="sh">
<arg value="-c" />
<arg value="cmd-with-non-0-exit.sh || exit 0" />
</exec>
Store exit code <> 0 to property:
<exec executable="sh" outputproperty="cmdOutput">
<arg value="-c" />
<arg value="cmd-with-non-0-exit.sh || echo $?" />
</exec>
The exit code then can be parsed from $cmdOutput.

Related

How to run multiple commands with arguments from single Ant exec task

On windows, I am trying to execute two commands (.cmd and .exe) later requiring parameters,in one exec() task. This is to avoid using two shell ,however only first command is getting executed.
Following is the Ant snippet
<exec executable="cmd" dir="C:\PROGRA~1\IBM\IIB\10.0.0.7\server\bin\">
<arg value="/c mqsiprofile.cmd & C:\PROGRA~1\IBM\IIB\10.0.0.7\server\bin\mqsideploy.exe" />
<arg value="IIBNODE1" />
<arg value="-e" />
<arg value="default" />
<arg value="-a" />
<arg value="${bar.name}" />
</exec>
I also ran it without &amp and replacing "PROGRA~1" with "Program Files", still the same issue. Please suggest.
You can include both in a single target:
<target name="execute.this">
<exec dir="${testworkspace}\${moduleName}"
executable="cmd" failonerror="true"
output="${testworkspace}/${moduleName}/BuildConsole_TC${tc_num}.log"
resultproperty="execrc">
<arg value="/c echo Download Status is ${DownloadStatus}"/>
<exec dir="${testworkspace}\${moduleName}"
executable="cmd" failonerror="true"
output="${testworkspace}/${moduleName}/BuildConsole_TC${tc_num}.log"
resultproperty="execrc">
<arg value="/c Load.bat ${moduleName} ${Intapp} ${CcvStatus}"/>
</exec>
Or better yet, just use the <echo> task:
<echo message="/c echo Download Status is ${DownloadStatus}"/>
<exec dir="${testworkspace}\${moduleName}"
executable="cmd"
failonerror="true"
output="${testworkspace}/${moduleName}/BuildConsole_TC${tc_num}.log"
resultproperty="execrc">
<arg value="/c Load.bat ${moduleName} ${Intapp} ${CcvStatus}"/>
</exec>
If you need the output of the echo task in the same file, you can use the file parameter in the echo command, and the append parameter in the exec task.
Ref: How to run multiple commands from ant exec task

Ant exec, How to arg

So I have the below build file:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\Abc\Desktop\cmd.bat" >
<arg value="exit"/>
<arg value="mkdir C:\Users\Abc\Desktop\ant\test"/>
</exec>
</target>
I thought it would create a test folder there but its not. It pops out another command prompt window, I thought the exit would close that but it doesn't. I'm not sure what I am doing wrong.
The content of bath script test1.bat:
#echo off
echo %1
And ant's build.xml:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\AAA\Desktop\ant\test1.cmd">
<arg value="hello"/>
</exec>
</target>
As per the official documentation:
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch
Follow this format instead:
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
Source: https://ant.apache.org/manual/Tasks/exec.html

Executing command line code in an ant build file

How can I execute following command line code in an ant build file?
cd backend/doctrine/
export PC_ZEND_ENV=testing
php doctrine migrations:migrate << EOF
y
EOF
The solution
With the feedback I've got I figured out the following working exec command.
<exec dir="backend/doctrine" executable="php">
<env key="PC_ZEND_ENV" value="development" />
<arg line="doctrine migrations:migrate" />
<arg value="<< Y" />
</exec>
Use the exec task. The result should be something like the following (untested):
<exec dir="backend/doctrine" executable="./doctrine">
<arg line="migrations:migrate << EOF"/>
<env key="PC_ZEND_ENV" value="testing"/>
</exec>

How can I execute a VBScript file from Ant script?

I want to run a VBScript file from an Ant script. How can I do that?
Have a look at the exec task. It allows you to execute a system command from your Ant script.
EDIT:
An example could be:
<target name="RunVbScript">
<exec executable="cscript">
<arg value="MyScript.vbs"/>
<arg value="argument 1" />
<arg value="argument 2" />
<arg value="argument n" />
</exec>
</target>

How can I get current PID from within Ant?

I have an ant task, and within it I'd like to get the current process id (a la echo $PPID from command line).
I'm running ksh on Solaris, so I thought I could just do this:
<property environment="env" />
<target name="targ">
<echo message="PID is ${env.PPID}" />
<echo message="PID is ${env.$$}" />
</target>
But that didn't work; the variables aren't substituted. Turns out PPID, SECONDS, and certain other env variables don't make it into Ant's representation.
Next I try this:
<target name="targ">
<exec executable="${env.pathtomyfiles}/getpid.sh" />
</target>
getpid.sh looks like this:
echo $$
This gets me the PID of the spawned shell script. Closer, but not really what I need.
I just want my current process ID, so I can make a temporary file with that value in the name. Any thoughts?
You can find PID using java process monitoring tool JPS, then output stream can be filtered and if needed process can be killed. check out this tomcat pid kill script:
<target name="tomcat.kill" depends="tomcat.shutdown">
<exec executable="jps">
<arg value="-l"/>
<redirector outputproperty="process.pid">
<outputfilterchain>
<linecontains>
<contains value="C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
</linecontains>
<replacestring from=" C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
</outputfilterchain>
</redirector>
</exec>
<exec executable="taskkill" osfamily="winnt">
<arg value="/F"/>
<arg value="/PID"/>
<arg value="${process.pid}"/>
</exec>
<exec executable="kill" osfamily="unix">
<arg value="-9"/>
<arg value="${process.pid}"/>
</exec>
</target>
Why not just use the tempfile Ant task, instead? It does what you really want to do, while hiding all the gory details.
See http://ant.apache.org/manual/Tasks/tempfile.html.
your second method doesn't get ANT's pid. Change the shell script to (I use bash, I don't know if ksh is the same):
echo "$PPID"

Resources