How to set failonerror=false on UNWAR task - ant

I have a target where multiple unwar tasks gets executed.
If an UNWAR task fail due to missing archieve file, i still want to execute other(next) UNWAr tasks in that target. How to do this.
Thanks,
Raj

The third-party Ant-Contrib has a <trycatch> task that catches exceptions and then continues an Ant script:
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<trycatch>
<try>
<unwar/> <!-- A fully configured unwar task here -->
</try>
<catch>
<echo>The unwar task failed.</echo>
</catch>
</trycatch>
<!-- The script continues here, even if the unwar task failed. -->

Related

ant parallel to mark build success

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>

Tags between <echo> and </echo> in ant

I have an Ant script like this:
<target name="create_report_file">
<echo file="${testResultsDir}/test_report.xml">
<testsuite name="${platformTask}">
</testsuite>
</echo>
</target>
What do the tags between <echo> and </echo> mean? Will Ant run them or output? Or both?
file is an echo parameter. It's the file to write the message to.
testsuite is possibly a JUnit task.
The Ant target runs the test suite and outputs the results to the test_report.xml file.
Does your Ant script work? What about if it attempts to execute the create_report_file target?
Usually, echo tasks simply echo the contents to either the console or to a file if the file parameter is specified.
However, as written, it makes <testsuite> is a sub-entity of the <echo/> task, and it's not. In fact, there's no documented sub-entities in the <echo/> task. In fact, <echo> doesn't even take <condition/> sub-entity tasks like <fail/> would.
This is why I'm asking whether or not your build file is even working.
It appears they might want to log the testsuite being executed. There are two ways to do this to make this work:
Change all < and > to character entities:
<target name="create_report_file">
<echo file="${testResultsDir}/test_report.xml">
<testsuite name="${platformTask}">
</testsuite>
</echo>
</target>
Use <echoxml> instead of <echo>:
<target name="create_report_file">
<echoxml file="${testResultsDir}/test_report.xml">
<testsuite name="${platformTask}">
</testsuite>
</echoxml>
</target>
Another possibility
It is possible that you're using some Ant plugin that has a <testsuite> task. I don't know what this would be. The <testsuite> task isn't part of JUnit or TestNG. However, if there is an Ant plugin being used that defines a <testsuite> task, it might redefine the <echo> task which it's at it. Does your build script have a <taskdef> in it? If so, what's the class reference?
It could be that the user defines their own <testsuite> macro in your build script. However, that wouldn't redefine the <echo> task and it still wouldn't work.

call one Ant task after another

I want to call sendmail once RunTests is complete, but its not working
<project name="Sample usage of Salesforce Ant tasks" basedir="." xmlns:sf="antlib:com.salesforce" default="RunTests">
<!--******************************************* P R O P E R T Y F I L E *********************************************-->
<property file="build.properties"/>
<property environment="env"/>
<!--*******************************************RunTests***********************************************************-->
<target name="RunTests">
<record name="RunTest.log" action="start"/>
<sf:compileAndTest username="${sf.backup_username}" password="${sf.backup_password}" serverurl="${sf.backup_serverurl}" >
<runTests Alltests="true"/>
</sf:compileAndTest>
<record name="RunTest.log" action="stop"/>
</target>
<target name="sendmail" depends="RunTests">
<mail mailhost="email.corp.testcompany.com" mailport="25" subject="Run all tests - Sandbox 1337" messagefile="RunTest.log">
<from address="user2#testcompany.com"/>
<replyto address="user2#testcompany.com"/>
<to address="user2#testcompany.com"/>
</mail>
</target>
</project>
I get following failures -
BUILD FAILED
C:\ANT_HOME\QA_1337_TestRunner\build.xml:8: Failed:
is there any way I can execute "sendemails" task even if there is failure for earlier task.
try to run
ant sendmail
or change the default to sendmail
<project name="Sample usage of Salesforce Ant tasks" basedir="." xmlns:sf="antlib:com.salesforce" default="sendmail">
and simply execute
ant
EDIT:
After a quick look at salesforce doc for the task sf:compileAndTest, I didn't find any useful information on how avoid target execution failure whenever the tests and/or compilation fail. (maybe the checkonly property, but I'm not sure this is what you need nor if you can pass it though the ant task)
So I think that you have to handle the target execution failure externally. To do so you can use the trycatch task from ant-contrib.
It will look like this:
<trycatch property="foo" reference="bar">
<try>
<record name="RunTest.log" action="start"/>
<sf:compileAndTest username="${sf.backup_username}" password="${sf.backup_password}" serverurl="${sf.backup_serverurl}" >
<runTests Alltests="true"/>
</sf:compileAndTest>
<record name="RunTest.log" action="stop"/>
</try>
<catch>
<echo>Got error while running compileAndTest</echo>
</catch>
<finally>
<mail mailhost="email.corp.testcompany.com" mailport="25" subject="Run all tests - Sandbox 1337" messagefile="RunTest.log">
<from address="user2#testcompany.com"/>
<replyto address="user2#testcompany.com"/>
<to address="user2#testcompany.com"/>
</mail>
</finally>
</trycatch>
Note that you can use the <catch>section to send special email when tests fail.
The property and reference can be use to get information about the failure (if any).
I think you want to look into the failureProperty and haltOnFailure attributes.
More info in this thread's answer:
https://stackoverflow.com/a/134563/708777

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>

How can I apply a timeout to an Ant task?

Without writing a custom Ant task, is there a way to use a timeout on a regular ant target?
To give some background info: we are using the 'delete' task to remove the contents of a given directory.
Sometimes this directory is massive, with lots of generated folders and files.
We wanted to have that task timeout after, say, 5 minutes.
You might use the parallel task, which has a timeout, with a parallel degree of one:
<target name="timed_del">
<parallel threadCount="1" timeout="300000">
<sequential>
... your tasks here ...
</sequential>
</parallel>
</target>
You can also use the limit task.
<target name="my-target">
<limit seconds="2" failonerror="true">
<sshexec ... />
</limit>
</target>

Resources