ant parallel to mark build success - ant

I am using ANT parallel task to timeout after starting a server. At the timeout ant returns build failure. I want build to be successful after timeout seconds. Please find the below code.
<target name="tomcat-start">
<parallel threadCount="1" timeout="30000" failonany="true">
<sshexec host="10.0.19.47"
trust="true"
username="${server_username}"
password="${server_password}"
command="nohup java -jar /home/techteam/TravelSecure-deployments/DTCMWS_TravelSecure-0.0.1-SNAPSHOT.jar & disown" />
<echo> Microsite service started successfully. Please wait for service to start running</echo>
</parallel>
</target>

Related

Is there a way to output failed TestNG selenium test stacktraces to the Ant logfile?

I have the following TestNG snippet in an ant xml script:
<testng haltonfailure="true" outputdir="${webtest.outputDir}" dumpCommand="true">
<classpath>
<path refid="project.webtest.classpath"/>
<pathelement path="${java.class.path}"/>
</classpath>
<sysproperty key=".test.web.webdriver.configFile" value="${webtest.configFile}"/>
<xmlfileset dir="${webtest.classes}/test/web/webdriver" includes="testng.xml">
</xmlfileset>
</testng>
Sometimes my Selenium tests using this script fail or get skipped, and if this happens I always need to remote into the build server the script runs on in order to check the TestNG results themselves, because they aren't put in the logfile. Is there a Reporter or a Listener or something like that which simply writes the failure cause to the ant log?

Ant script to check if remote instance of Jboss is running

I have an Ant script which is used to deploy my application on two different machines at a time. With the local machine i do not have a problem, but when it comes to the remote machine, I want to check if Jboss is up or not. If it is up then I want to shut it down but if it is not then nothing should be done. I tried to do this by keeping the attribute of <sshexec> as failonerror="on". This works well when the Jboss is already down and the shutdown command only gives some errors. But the real problem that i faced was when Jboss was running and when the shutdown command was executed, it did not shutdown properly and gave some error. It is in these situations that I want to stop my build script and let the user know that there is something wrong with Jboss on the other machine and it needs to be looked at.
The target code for stopping the remote Jboss is
<target name="stopRemoteJboss" description="Stops Remote Instance of Jboss">
<echo message="Stopping Remote Jboss" />
<sshexec trust="true" host="${jboss.remote.host}" username="${jboss.remote.username}" password="${jboss.remote.password}" command="${jboss.remote.home}/bin/shutdown.sh -S" port="${jboss.remote.port}"/>
</target>
After a short check, I've found following, maybe you could reuse/or use as an inspiration for your script: http://shrubbery.homeip.net/c/display/W/Starting+JBoss+with+ANT
The relevant part for you seem to be:
<java jvm="#{jdkHome}/bin/java"
classname="org.jboss.Shutdown" fork="true" failonerror="false" resultproperty="shutdown.rc">
<arg line="-s jnp://#{bindAddr}:#{jnpPort}"/>
<classpath>
<pathelement path="#{jbossInstallDir}/bin/shutdown.jar"/>
<pathelement path="#{jbossInstallDir}/client/jbossall-client.jar"/>
</classpath>
</java>
<echo>Shutdown rc = ${shutdown.rc}</echo>
<condition property="shutdown.okay">
<equals arg1="${shutdown.rc}" arg2="0"/>
</condition>
<fail unless="shutdown.okay" message="Unable to shut down JBoss (maybe it hasn't fully started yet?)."/>
<echo>Waiting for #{bindAddr}:#{jnpPort} to stop listening...</echo>
Why not check to see if the remote port is active?
<project name="demo" default="check">
<condition property="server.running" value="running" else="not running">
<socket server="remoteserver" port="80"/>
</condition>
<target name="check" description="Print status message">
<echo message="Web server status: ${server.running}"/>
</target>
</project>
If your JBoss instance is configured as a reverse proxy you could use the alternative http condition to check the HTTP response code (which would be 503 "Service unavailable", if the appserver is down)

'make -n' equivalent for ant

According to the man page of make, -n option does the following job:
Print the commands that would be executed, but do not execute them.
I am looking for an option which acts the same in Apache Ant.
Horrific, but here it is. We can hack the targets at runtime using some code inside a <script> tag*. The code in do-dry-run below sets an unless attribute on each of your targets, and then sets that property so that none of them executes. Ant still prints out the names of targets that are not executed because of an unless attribute.
*(JavaScript script tags seem to be supported in Ant 1.8+ using the Oracle, OpenJDK and IBM versions of Java.)
<?xml version="1.0" encoding="UTF-8"?>
<project default="build">
<target name="targetA"/>
<target name="targetB" depends="targetA">
<echo message="DON'T RUN ME"/>
</target>
<target name="targetC" depends="targetB"/>
<target name="build" depends="targetB"/>
<target name="dry-run">
<do-dry-run target="build"/>
</target>
<macrodef name="do-dry-run">
<attribute name="target"/>
<sequential>
<script language="javascript"><![CDATA[
var targs = project.getTargets().elements();
while( targs.hasMoreElements() ) {
var targ = targs.nextElement();
targ.setUnless( "DRY.RUN" );
}
project.setProperty( "DRY.RUN", "1" );
project.executeTarget( "#{target}" );
]]></script>
</sequential>
</macrodef>
</project>
When I run this normally, the echo happens:
$ ant
Buildfile: build.xml
targetA:
targetB:
[echo] DON'T RUN ME
build:
BUILD SUCCESSFUL
Total time: 0 seconds
But when I run dry-run, it doesn't:
$ ant dry-run
Buildfile: build.xml
dry-run:
targetA:
targetB:
build:
BUILD SUCCESSFUL
Total time: 0 seconds
Ant has no dry-run option as make or maven have. But you could run the ant file step by step it in debugging mode under eclipse.
No I belive. There is no such way by default in Ant. And many unstisfying attempts you would find on google. But I have searched once and was unsuccessful.
It would be a useful feature, but not easily implemented.
Make and ANT are architecturally quite different. ANT doesn't run external OS commands, instead, most ANT "tasks" execute within the same Java thread.
It would be possible to emulate a "dry run" as follows:
<project name="Dry run" default="step3">
<target name="step1" unless="dry.run">
<echo>1) hello world</echo>
</target>
<target name="step2" depends="step1" unless="dry.run">
<echo>2) hello world</echo>
</target>
<target name="step3" depends="step2" unless="dry.run">
<echo>3) hello world</echo>
</target>
</project>
Running ANT as follows will print the target name but won't execute the enclosed tasks:
$ ant -Ddry.run=1
Buildfile: build.xml
step1:
step2:
step3:
BUILD SUCCESSFUL
Total time: 0 seconds
Create a special target in your buildscript that does some echoing only i.e. to check whether properties, path .. are resolved correctly.
see https://stackoverflow.com/a/6724412/130683 for a similar question answered.
For checking the details of your ant installation use ant -diagnostics

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
Try ant-contrib's try/catch/finally commands.
http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html
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>

try finally in ant

In my ant script, which runs the end-to-end integration tests, I first start a process, then do some other stuff, then run the tests, and then I need to make sure I kill the process. However, I need to make sure I kill the process even if something fails (so I need an equivalent to try finally). What is the recommended way of doing it?
You could use Trycatch task from Antcontrib
<trycatch property="error.message">
<try>
<echo message="Run integration test..."/>
<echo message="Start process"/>
<antcall target="launchTests"/>
</try>
<catch>
<echo message="Integration test failed"/>
</catch>
<finally>
<echo message="Kill the process"/>
<exec executable="kill -9 ..."/>
</finally>
</trycatch>

Resources