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
Related
I have some job in jenkins and i need start it using ant
ant run
Build.xml
<project name="jenkins-facade" default="run" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<description>
simple example build file
</description>
<property name="post.json.encoded" value=""/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<property name="post.json" value='{"parameter": [{"name":"foo_param", "value":"123"}]}'/>
<urlencode name="post.json.encoded" value="${post.json}" />
</target>
<target name="run" depends="init"
description="run jenkins job">
<post to = "http://jankhost:8080/job/Test/buildWithParameters/"
verbose="true" failonerror="true">
<prop name="token" value="1234567"/>
<prop name="json" value="${post.json.encoded}"/>
</post>
</target>
</project>
But when i run it i got 403 error.
I need to run a task on your behalf, and not by anonymous. How can i do this? How to pass through the ant username and token?
scr
I'm surprised/horrified that there's an Ant task to do HTTP POST requests, but looking at its documentation, it appears that there's no way to set HTTP request headers, which you would need for a Jenkins API request like this.
So as an alternative, you could try setting the username and API token in the URL — I don't know whether the Ant task supports this, but you could try http://<user>:<api_token>#jankhost:8080/job/Test/buildWithParameters/
Another possibility, according to any https://<jenkins>/job/<name>/api page may be to use the secret token mechanism:
Another alternative (but deprecated) is to configure the 'Trigger builds remotely' section in the job configuration. Then building or polling can be triggered by including a parameter called token in the request.
You can then include this secret token in the build URL itself. However, as you have security enabled, this will not work out-of-the-box — you will need to install the Build Token Root plugin, and follow the instructions there.
When debugging a build.xml file, or an Ant task, I often want to execute one task without executing its dependencies. Is there a way to do this from the command line?
For example, with this build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="A" />
<target name="B" depends="A" />
</project>
is there a command that will execute task B but not task A?
You can make execution of any target conditional on a property using if or unless.
<project default="B">
<target name="A" unless="no.a">
<echo>in A</echo>
</target>
<target name="B" depends="A" >
<echo>in B</echo>
</target>
</project>
Output with no condition specified:
$ ant
Buildfile: C:\Users\sudocode\tmp\ant\build.xml
A:
[echo] in A
B:
[echo] in B
BUILD SUCCESSFUL
Total time: 0 seconds
Output with condition specified on command line:
$ ant -Dno.a=any
Buildfile: C:\Users\sudocode\tmp\ant\build.xml
A:
B:
[echo] in B
BUILD SUCCESSFUL
Total time: 0 seconds
Notes:
Ant console output will show that the target was "hit" even if entry was blocked by the condition.
The if and unless conditions do not do boolean check. They just check whether the property is defined or not.
You will have to restructure your Ant script to achieve this:
<target name="B">
<if>
<isset property="some.property"/>
<then>
<antcall target="A">
</then>
</if>
<!-- execute task B here -->
</target>
If some.property is set, then it will first execute A followed by B. Otherwise, it will skip task A and execute B by itself.
how can i deploy a app in weblogic only if it is not present in it using wldeploy ant task
when i run ant testapp it deploys fresh everytime over existing app as far as i could see in console messages (ie in sysout).
i call this ant target as dependency in some other target, and i want this to run only if app is not already present in weblogic server (to be more efficient)
<target name="testapp" depends="init-wls">
<wldeploy action="deploy" verbose="true" debug="true"
name="testapp" failonerror="false"
...
source="testapp.war"/>
</target>
an interesting question. I'm not sure if wldeploy can do what you want. One approach that might work would be to use the wlconfig ant task. You could use it to get the ApplicationRuntimeMBeans and then query their ApplicationName attributes (again, with wlconfig task) to see if the application is deployed. Not super straightforward but at least you would avoid the application redeployment.
This is just a quick idea off the top of my head so not sure if it is feasible in practice, sorry.... :)
--edit: tried it out, something like this should work, the assumption here is that if we can find the MBean then it is already deployed which should be a valid assumption since these beans live under AppDeployments:
<project name="test" default="deploy">
<property name="domainName" value="ejbTestDomain"/>
<property name="serverName" value="AdminServer"/>
<property name="appName" value="ejbWebEAR"/>
<target name="findApp">
<wlconfig url="t3://localhost:7001" username="weblogic" password="password_for_weblogic">
<query pattern="${domainName}:ServerRuntime=${serverName},Name=${appName},*,Type=ApplicationRuntime" property="app.is.deployed"/>
</wlconfig>
</target>
<target name="deploy" unless="app.is.deployed" depends="findApp">
<echo message="Deploying..."/>
<!-- deploy using wldeploy task -->
</target>
</project>
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>
I am running javac task using ant and I send the output to a log file using -Xstdout compiler argument for reporting purposes, but I would like the output also still being send to the console so hudson can capture it for on screen review.
Is there a way for this to be done?
Just came across another alternative using the recorder task.
Nearer as you don't have to introduce new targets.
<compile >
<record name="log.txt" action="start"/>
<javac ...
<record name="log.txt" action="stop"/>
<compile/>
use the ant task with an output attribute to call a target that has the javac task.
e.g.
<target name="javac" depends="libs" description="Compile java source">
<mkdir dir="${classes.dir}" />
<ant target="actual-javac" output="javac.log"/>
</target>
<target name="actual-javac">
<javac .../>
</javac>
</target>
you can use tee on any process on the command line to output to the console and a file:
>myprocess.sh | tee myprocess.log
will print the output of myprocess.sh both to the console and myprocess.log.