Ant exec after end or with daemon delay - ant

In a ant script, i need to schedule the execution of a batch file after the end of script (or after a time-lapse).
I tried to wrap the exec task into the parallel+daemons tasks after a sleep task but without success...
<echo>--- start ---</echo>
<parallel>
<daemons>
<echo>start daemon</echo>
<sleep seconds="10" />
<exec dir="Y:\Test" executable="Y:\Test\exec.bat" spawn="true"/>
<echo>stop daemon</echo>
</daemons>
</parallel>
<echo>--- stop ---</echo>
There is any way for do this in Ant? Or it's mandatory insert a timeout into script file?

Related

passing value to remote ant target

I have two ant files say antFile1.xml and antFile2.xml on windows and linux machine respectively.
antFile1.xml
<target name="executeOnLinux">
<sshexec
host="${remote.host.ip}"
username="${remote.user.id}"
password="${remote.user.ssh.password}"
command="ant -f antFile2.xml 'linuxAntTarget'"
trust="true"/>
</target>
antFile2.xml
<target name="linuxAntTarget">
<input message="Enter application edition number" addproperty="editionNo"/>
<echo message="${editionNo}"/>
</target>
When I execute the the target named "executeOnLinux" from antFile1.xml from my, it connects to the linux machine and starts executing the "linuxAntTarget" from antFile2.xml file. But, as soon as it reaches to the the point where user input is required keeps waiting since I am not able to pass the input. My question is how can I pass the value to it? Is this even possible or there is any other better way of doing it?
Since Ant 1.9.4, the sshexec task provides a useSystemIn attribute:
Whether to pass the current standard input to the remote process, defaults to false
You may also try the input attribute from the same task:
A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring and inputproperty attributes.
When executing more than one command via commandResource, input will be read for each command. since Ant 1.8.0

How to list a directory using ANT's SCP task over SFTP?

The FTP task in ant allows you to list a given directory using syntax as follows:
<ftp action="list"
server="ftp.apache.org"
userid="admin"
password="${password}"
listing="c:\\temp\\listing-temp.txt">
<fileset>
<include name="response/*.zip"/>
</fileset>
</ftp>
The Groovy Syntax is:
ant.ftp(
action:"list",
server:"ftp.apache.org",
userid:"admin",
password:"$password",
verbose:false,
listing:"c:\\temp\\listing-temp.txt"){
fileset(dir:"") { include(name:"response/*.zip") }
}
The SCP Task is primarly used when needing to work over the SFTP protocol. However, I don't see any mechanism to list over SFTP using the SCP task. Is there a known technique to perform a listing using the scp task? The only alternative I have right now is to pull the files down as a 'listing'. I would really like to just list the contents and avoid the overhead of pulling the files down.
ant.scp(
file: "aUserID#sftp.apache.org:$path/*.zip",
toDir: "C:\\dev\\tmp",
password: "$password",
trust: 'true',
verbose: "false",
sftp: "true")

Removing comment in ini file writen by ant when updating the ini file

I have an ant file which updates the data in ant file due this ini file gets updated and at the top it has a comment as follows
#Thu, 07 Jul 2011 06:54:54 -0500
I don't want this comment as i am accessing this file by php using parse_ini. Due to this comment i get an failure
Comments starting with '#' are deprecated in build.ini on line 1
so is there any way so that i will not get the comment in ini file.
Thanks.
EDIT:
<propertyfile file="build.ini">
<entry key="build-number" type="int" operation="+" value="1" />
</propertyfile>
this updates my ini file build-number by +1
Martin's comment points you to a way to remove comments using replaceregexp. (I was about to show you a similar idea but using move, filterchain and striplinecomments. But the replaceregexp is more compact.)
The other suggestion I have is that since you are editing ini files, maybe you should use a task dedicated to that, rather than using the PropertyFile task. There is an IniFile taks in ant-contrib might do the job.
If the replaceregexp doesn't work for you because your file has other # comments in it and you only want to remove that top line, then try this:
<target name="test">
<propertyfile file="test.properties">
<entry key="key" value="value"/>
</propertyfile>
<move file="test.properties" tofile="test.properties.tmp">
<filterchain>
<headfilter lines="-1" skip="1"/>
</filterchain>
</move>
<move file="test.properties.tmp" tofile="test.properties"/>
</target>
Output:
$ cat test.properties
one=1
# existing comment
$ ant
Buildfile: C:\tmp\ant\build.xml
test:
[propertyfile] Updating property file: C:\tmp\ant\test.properties
[move] Moving 1 file to C:\tmp\ant
[move] Moving 1 file to C:\tmp\ant
BUILD SUCCESSFUL
Total time: 0 seconds
$ cat test.properties
one=1
# existing comment
key=value

Passing input to Ant's <exec> task

I have an Ant script running a standard -task after taking in an inputed password:
<input message="Password:" addproperty="password">
<handler classname="org.apache.tools.ant.input.SecureInputHandler" />
</input>
<exec executable="/bin/sh" input="${password}" failonerror="true">
<arg line='-c "myScript.sh"' />
</exec>
The script myScript.sh prompts the user for a password, and, it was my understanding that from the Ant documentation that input is supposed relay input into whatever the <exec> task is executing, but instead I get (for entering the password foobar)
[exec] Failed to open /usr/local/foobar
which is followed by a stack trace from my script complaining about an incorrect password...so obviously I've understood the documentation wrong. Does anybody know how to handle prompted input from external scripts in Ant?
input="${password}"
This will try to read from the file ${password} and send the contents into your script. Try using:
inputstring="${password}"
instead. This will send the string itself instead of treating it like a filename

bounce multiple servers using ant

This is restart target code which is defined in build.xml
target name="restart"
propertycopy name="remote.host" from="deploy.${target.env}.host.${remote.id}"
propertycopy name="remote.port" from="deploy.${target.env}.port.${remote.id}"
sshexec trust="true"
host="${remote.host}"
port="${remote.port}"
username="${scm.user}"
keyfile="${scm.user.key}"
command="sudo /usr/local/bin/bounce_jboss"
target
server information is defined in build.properties.
The above code is working fine, but the restarting process is very late bcas its stopping-starting server one and later its stopping-starting another server,
Is there a way where i can restart both servers parallely with a time frame of 45 seconds.
Have you investigated the Ant Parallel task? You should be able to parallelise the rebooting fairly simply using this.
e.g.
<parallel>
<!-- first server reboot -->
<ssh ...>
<!-- second server reboot -->
<ssh ...>
</parallel>
The parallel task will work for you. Another example:
<target name="restart" ... >
<parallel>
<!-- first server reboot call -->
<!-- second server reboot call -->
</parallel>
</target>
From command line:
>ant restart
Don't execute "ant restart" twice. Only call it once and your servers should only restart once.

Resources