Ant http doesn't fail target - ant

I've been trying to get this to work all afternoon and can't. I want to verify that my app is up and running from my ant script. It seems that the below task should do the job but it doesn't. I've gone through the ant documentation with a fine tooth comb trying various permutations but the documentation is very scant in terms of catching a failure from http. Can anyone help. Has anyone else got http working with ant ok?
<?xml version="1.0" encoding="UTF-8"?>
<project name="hermes" default="test-app-running" xmlns:epam="epam://epam.com" xmlns:catalina="antlib://catalina.apache.org" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<target name="test-app-running" >
<waitfor maxwait="10" maxwaitunit="second">
<http url="http://localhost:8080/" />
</waitfor>
<fail message="App did not come up. Check your log files, fix and try again. Good Luck :-).">
<condition>
<http url="http://localhost:8080/" />
</condition>
</fail>
</target>
</project>

The condition needs a <not />. I just tested it and it works.
<fail message="App did not come up. Check your log files, fix and try again. Good Luck :-).">
<condition>
<not>
<http url="http://localhost:8080/" />
</not>
</condition>
</fail>
Without the not it will fail if the server is up.

Related

Retry task on exec task in Ant gives incorrect resultproperty

I have a piece of code in my ant build.xml
<retry retrycount="10" retrydelay="30000">
<exec executable="${env.M2_HOME}/bin/mvn.cmd" output="#{log}" failonerror="true" resultproperty="res" timeout="#{timeoutmillis}" errorproperty="error">
...
</exec>
</retry>
<echo message="${res}"/>
I retry my cmd task if if fails once upto 10 times. But even if it were to succeed after retrying a few turns, the value returned in res is 1 even though it is a build success. I expect it to be 0 as if it was SUCCESS.
Properties in Ant are immutable (they're not variables), so even if your exec task eventually succeeds, the result property will have already been set to whatever it got from the first run.
You can get past this by creating a sequential block and using the local task inside it to control your property scope.
<retry retrycount="10">
<sequential>
<local name="do.fail" />
<input addproperty="do.fail" validargs="y,n" message="fail?" />
<fail>
<condition>
<equals arg1="${do.fail}" arg2="y" />
</condition>
</fail>
</sequential>
</retry>
Running the above example will prompt the user until "n" is provided as input (or 10 retries).
This is not a direct answer to the question I asked. But taking into consideration what CAustin and jdpjamesp said, I'm reading the content of my output log to see whether the command failed or not.
The cmd will return 1 even if it passed after some n number of failures. But the output log in case of mvn will have "BUILD SUCCESS" only if the command passes at the end. So searching for this string works for me. The log should not be appended after every time though.
So I implemented this-
<property name="pass.string" value="BUILD SUCCESS" />
<resourcecount property="pass.string.count">
<fileset id="matches" file="#{log}">
<contains text="${pass.string}" />
</fileset>
</resourcecount>
<if>
<equals arg1="${pass.string.count}" arg2="0" />
<then>
..
</then>
<else>
..
</else>
</if>

Condition in Ant not working properly

I'm doing a task in Ant which tests whether a server is stopped or running, and in case the server is stoppped, the Ant task fail and sends an email. The problem is that my condition isn't working properly, and the tasks always goes good, regardless the server is stopped or running.
Here is the code of my condition:
<condition property="check" value="true" >
<or>
<equals arg1="${state}" arg2="STOPPED" />
<equals arg1="${state}" arg2="STOPPING" />
</or>
</condition>
<fail if="${check}" message="Server stopped"/>
The variable state can have the next values: STOPPED, STOPPING or STARTED (I checked before that the value is correct). As I said, even if state = STOPPED or state = STOPPING, the property check is never set, so my task is always successful.
I have tried using forcestring and casesensitive in equals, and I have tried:
<fail if="check" message="Server stopped"/>
but the condition is still not working.
Any advice or idea on how to solve this and make it work?
Thanks.
try this:
<fail message="Server stopped">
<condition>
<or>
<equals arg1="${state}" arg2="STOPPED" />
<equals arg1="${state}" arg2="STOPPING" />
</or>
</condition>
</fail>
Try :
<target name="server_stopped" if="check">
<fail message="Server stopped"/>
</target>

exist-db : Issues with ant task xdb:adduser or xdb:users : no such handler

I'm using ant scripts for initializing my exist-db.
But, I have this script below (dummy version) which is working in my local platform and not on my procution one. As I'm not the one who handle the configuration of this database, I don't know where to look in the conf to fix this.
Here is the script (which is just trying to add a new user) :
<project basedir="." default="default" name="ANTProject">
<property file="load.properties"/>
<path id="classpath.core">
<fileset dir="${path}/lib/core">
<include name="*.jar"/>
</fileset>
<pathelement path="${path}\exist.jar"/>
<pathelement path="${path}\exist-optional.jar"/>
</path>
<typedef resource="org/exist/ant/antlib.xml" uri="http://exist-db.org/ant">
<classpath refid="classpath.core"/>
</typedef>
<target name="default">
<echo message="Création du compte ${login}"/>
<xdb:adduser xmlns:xdb="http://exist-db.org/ant"
uri="xmldb:exist://${exist.uri}/exist/xmlrpc/db"
name="${login}" secret="${password}" primaryGroup="${user.group}" user="${root.login}"
password="${root.password}"/>
</target>
</project>
I get this error message :
XMLDB exception caught: No such handler: Default.setUser
I get the same thing if I use xdb:users task, but the xdb:store is working well... I'm running exist 2.1, in both my local and production plateform, and as already told, same scripts working well on local one...
I guess, it's something about the exist configuration, but I didn't find anything on enabling this tasks in the documentation.
If someone could help...
Ok, I got it.
Just for completeness, It was an issue on jar librairies. It seems I used ones which support xdb:store, but not others tasks (didn't find any release version of this)...
This ant depandancies is quite tricky and It's hard to know what your jars offer...

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)

what's meaning of Ant exec Result:128

my ant was stoped when execute "<exec excultable="c:\myExe.exe"/>",the result code is just "<message priority="error"><![CDATA[Result: 128]]></message>".I don't know what's meaning of that.
I've tried to search some info like: error code 128---no such exe file,but I havn't.
some one could help me to explain what's the meaning?
Thanks
Oh sorry.
More info:
<macrodef name="gtest-layer-macro">
<attribute name="execfile"/>
<attribute name="layerpath" default=""/>
<attribute name="outputDir" default="${basedir}/${reports}/gtest"/>
<attribute name="reportfile" default="#{outputDir}/gtest_report.xml"/>
<sequential>
<check-layer-path layerpath="#{layerpath}"/>
<if>
<and>
<length string="#{execfile}" when="gt" length="0" trim="true"/>
<available file="#{execfile}"/>
</and>
<then>
<var name="##report.dir##" unset="true"/>
<dirname property="##report.dir##" file="#{reportfile}"/>
<mkdir dir="${##report.dir##}"/>
<exec executable="#{execfile}">
<arg value="--gtest_output="xml:#{reportfile}""/>
</exec>
</then>
</if>
</sequential>
</macrodef>
When I run the <exec>, there’s "error Result:128".
The “#{execfile}” is a gtest.exe file(a exe file to test a module),it could run correctly when I double-click it,it could print the unit test result in the console;Run in the CMD with “--gtest_output="xml:#{reportfile}"” is also could print the unit test result in the console,and output a unit test report(a .xml file).And I have changed another .exe instead of the “gtest.exe”,it’s right too.
So,I don’t know where I’m wrong.
An [exec] Result:128 is a process that can't be found/doesn't exist. At least this is what I've found out from my Ant target to kill any remaining processes at the end of certain tests. I get that result when the process I want to kill isn't running. My build log output looks like this;
[shutdown.server] exec
[13:45:33][exec] ERROR: The process "firefox.exe" not found.
[13:45:34][exec] Result: 128
So if that result is from the execution of your .exe then I'd check the path to ensure the exe is available.

Resources