How to run ruby script as a task from ant build? - ant

I have an exiting ant build that is called via ant -lib lib -f test_build.xml
I wanted to add one more <target> section to be run after the existing one finishes. I did some research and found ant manual for exec and even a question here on SO. After some reading I added new target to this existing build but it didn't work.
I tried to create new build file only with my target. It doesn't work either. Although the ant run finishes with message BUILD SUCCESSFUL
Total time: 0 seconds
If I run my ruby script from a command line it works. I tried to create bat file that would call my ruby script with the same result. If I call the bat file from dos window it works.
My ant run build file looks like
<project name="RunRubyExample">
<target name="calling ruby " >
<exec executable="ruby.exe">
<arg value="C:\EduTester\others\afterant.rb 1 2 tri four"/>
</exec>
</target>
<target name="calling batach">
<exec executable="cmd">
<arg value="/c"/>
<arg value="C:\EduTester\others\rubruby.bat 1 2 tri four"/>
</exec>
</target>
</project>
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
on Windows XP

It looks like you're passing a single arg value with embedded spaces
<arg value="C:\EduTester\others\afterant.rb 1 2 tri four"/>
Is that right? Or should it be either an arg line:
<arg line="C:\EduTester\others\afterant.rb 1 2 tri four"/>
or multiple arg values:
<arg value="C:\EduTester\others\afterant.rb"/>
<arg value="1"/>
<arg value="2"/>
<arg value="tri"/>
<arg value="four"/>

There needs to be specified target that needs to be run
either by <project name="RunRubyExample" default="callingruby">
or when calling the ant build file. Where we pass the target name we want to be run as an argument. ant -lib lib callingruby
Writing a Simple Buildfile
Thank you #Steve

Related

Pass password in remote using rsync

As part of project, I prepared a Ant script which will download files from SVN and copy the files to remote server using scp.
But client asked me to copy only changed files from next deployment onwards in Jenkins instead of copying complete set of 20+ MB files.
got rsync command to do this but here the problem, how to send password from Ant
<exec executable="rsync" dir="/copy-files/js/" failonerror="true">
<arg value="-rcv"/>
<arg value="${username}#server:/media/js/"/>
</exec>
How to pass password from Jenkins, tried with RSYNC_PASSWORD and password-file options but not working.
You can pass values for environment variables by using nested env elements:
<exec executable="rsync" dir="/copy-files/js/" failonerror="true">
<arg value="-rcv"/>
<arg value="${username}#server:/media/js/"/>
<env key="RSYNC_PASSWORD" value="[your.password]" />
</exec>

What does the "-c" mean in an ant exec task?

These are two ant tasks:
<target name="hg.add" >
<exec dir="." executable="hg">
<arg line="add ${reports-summary}" />
</exec>
</target>
<target name="hg.add">
<exec executable="/bin/sh">
<arg value="-c"/>
<arg value="hg add ${reports-summary}"/>
</exec>
</target>
This two tasks seem to have the same function. But why the second one needs to write"/bin/sh" and "-c"?
The -c is just an argument to the executable it does not mean anything special to the ant exec task.
The first target is running the hg executable directly.
The second target is running the Linux/Unix shell command (/bin/sh) and passing it the -c argument which tells the shell to execute a command given in the next argument. So this will run the same command as the first target, but the shell command may set up things like environment variables before running the command.

Start Jboss with Ant. Command exec doesn't work

I try to start JBoss with ant.
When i execute this script :
<target name="start-jboss" >
<exec executable="${jboss.bin.dir}\run.bat" >
<arg line="--configuration=Myserver -b localhost" />
</exec>
</target>
JBoss is blocking at this step :
[exec] 15:52:55,373 INFO [AjpProtocol] Initializing Coyote AJP/1.3 on ajp-localhost%2F127.0.0.1-8009
But when i run the run.bat it works... Its the same when i add spawn="true" in exec.
I think that the problem comes from eclipse...
Thanks
To run a batchfile use cmd as executable, something like :
<exec dir"yourworkingdir" executable="cmd" failonerror="true">
<arg line="/c ${jboss.bin.dir}\run.bat --configuration=Myserver -b localhost"/>
</exec>
if arg line=... doesn't work, use arg value=... for every parameter.
EDIT : if you have trouble using the batchfile, why not get rid of using that addtional batchfile and use the java task straightforward as explained here ?

Execute command in new window with Ant from IDEA

In IntelliJ IDEA I have this task in my Ant script
<target name="emulator-logcat">
<exec command="adb" spawn="false" osfamily="windows">
<arg value="-e"/>
<arg value="shell"/>
<arg value="logcat"/>
</exec>
</target>
It works but command output sent to IDEA ant window, not in Windows console window. How i can forward command output into new Windows console window as if i start this command from cmd.exe?
Problem resolved:
I create runner.bat file witch contains %* and call it like this
<target name="emulator-logcat">
<exec command="cmd.exe" spawn="true" osfamily="windows">
<arg line="/c start runner.bat adb -e shell logcat"/>
</exec>
</target>

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>

Resources