How to access values passed from ant script to shell script - ant

We have a shell script which we invoke it from an ant script. We need to pass input to shell script from ant script. It can be done as follows
<target name="help">
<exec executable="cmd">
<arg value="Hello"/>
<arg value="Welcome"/>
<arg value="World"/>
</exec>
</target>
But We are not able to figure out how to access the values passed from ant script in shell script. Could anyone please suggest me with the right information.Thanks.

Use properties as input, something like :
<project>
<property name="foobar" location="C:/foobar" />
<property name="foo" value="bar" />
<exec executable="cmd">
<arg value="/c" />
<env key="PATH" path="${env.PATH};${foobar}/bin" />
<arg value="set" />
</exec>
<exec executable="cmd">
<arg value="/c" />
<arg value="echo" />
<arg value="${foo}" />
</exec>
</project>
You have to use /c as first arg value.
When calling a batfile which expects %1 ... %9 as input, first arg is <arg value=/c">,second arg <arg value="yourbatfile.bat/>.The following args <arg value=.../> would be %1 and so on, f.e. :
foobar.bat
#echo off
echo First argument %1
echo Second argument %2
build.xml
<project>
<exec dir="dir="path/to/batfile" executable="cmd">
<arg value="/c"/>
<arg value="foobar.bat"/>
<arg value="foo"/>
<arg value="bar"/>
</exec>
</project>
output
[exec] First argument foo
[exec] Second argument bar
Example for calling a shellscript, first arg has to be <arg value="/path/to/shellscript.sh"/>, the following args <arg value="..."/> will be $1 ...
foobar.sh
#!/bin/bash
echo "\$# = $#"
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
build.xml
<project>
<exec executable="/bin/bash">
<arg value="/path/to/foobar.sh"/>
<arg value="foo"/>
<arg value="bar"/>
</exec>
</project>

Related

How to run multiple commands with arguments from single Ant exec task

On windows, I am trying to execute two commands (.cmd and .exe) later requiring parameters,in one exec() task. This is to avoid using two shell ,however only first command is getting executed.
Following is the Ant snippet
<exec executable="cmd" dir="C:\PROGRA~1\IBM\IIB\10.0.0.7\server\bin\">
<arg value="/c mqsiprofile.cmd & C:\PROGRA~1\IBM\IIB\10.0.0.7\server\bin\mqsideploy.exe" />
<arg value="IIBNODE1" />
<arg value="-e" />
<arg value="default" />
<arg value="-a" />
<arg value="${bar.name}" />
</exec>
I also ran it without &amp and replacing "PROGRA~1" with "Program Files", still the same issue. Please suggest.
You can include both in a single target:
<target name="execute.this">
<exec dir="${testworkspace}\${moduleName}"
executable="cmd" failonerror="true"
output="${testworkspace}/${moduleName}/BuildConsole_TC${tc_num}.log"
resultproperty="execrc">
<arg value="/c echo Download Status is ${DownloadStatus}"/>
<exec dir="${testworkspace}\${moduleName}"
executable="cmd" failonerror="true"
output="${testworkspace}/${moduleName}/BuildConsole_TC${tc_num}.log"
resultproperty="execrc">
<arg value="/c Load.bat ${moduleName} ${Intapp} ${CcvStatus}"/>
</exec>
Or better yet, just use the <echo> task:
<echo message="/c echo Download Status is ${DownloadStatus}"/>
<exec dir="${testworkspace}\${moduleName}"
executable="cmd"
failonerror="true"
output="${testworkspace}/${moduleName}/BuildConsole_TC${tc_num}.log"
resultproperty="execrc">
<arg value="/c Load.bat ${moduleName} ${Intapp} ${CcvStatus}"/>
</exec>
If you need the output of the echo task in the same file, you can use the file parameter in the echo command, and the append parameter in the exec task.
Ref: How to run multiple commands from ant exec task

Ant exec, How to arg

So I have the below build file:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\Abc\Desktop\cmd.bat" >
<arg value="exit"/>
<arg value="mkdir C:\Users\Abc\Desktop\ant\test"/>
</exec>
</target>
I thought it would create a test folder there but its not. It pops out another command prompt window, I thought the exit would close that but it doesn't. I'm not sure what I am doing wrong.
The content of bath script test1.bat:
#echo off
echo %1
And ant's build.xml:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\AAA\Desktop\ant\test1.cmd">
<arg value="hello"/>
</exec>
</target>
As per the official documentation:
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch
Follow this format instead:
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
Source: https://ant.apache.org/manual/Tasks/exec.html

build.xml ignoring <echo> tags and if options

I have a sencha Ext.js project and I am trying to get user input and take action based on that user input in my build.xml file.
Here's my build.xml:
<?xml version="1.0" encoding="utf-8"?>
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="PSOGN" default=".help">
<import file="${basedir}/.sencha/app/build-impl.xml"/>
<target name="-after-build">
<echo>57 ...</echo>
<property name="realperl" value="/usr/bin/perl"/>
<echo>59 ...</echo>
<exec executable="/bin/hostname" outputproperty="myhost">
<arg value="-s"/>
</exec>
<echo>64 ...</echo>
<exec executable="${realperl}" outputproperty="env">
<arg value="../bin/getCf.pl"/>
<arg value="--config"/>
<arg value="../etc/config/currentCf"/>
<arg value="ENV"/>
</exec>
<echo>70 ...</echo>
<input message="Install ${env} version on ${myhost}?" addproperty="install" validargs="y,n" />
<echo>72 ...</echo>
<condition property="doinstall">
<equals arg1="${install}" arg2="y" />
</condition>
<echo>Hello ...</echo>
<exec executable="${realperl}" outputproperty="htdocspath">
<arg value="../bin/getCf.pl"/>
<arg value="--config"/>
<arg value="../etc/config/currentCf"/>
<arg value="HTDOCSPATH"/>
</exec>
<echo>do install is ${doinstall}</echo>
<echo if:true="${doinstall}">Please wait. Executing rsync to ${htdocspath}. Output logged to /tmp/rsync.log.</echo>
<echo if:false="${doinstall}">Skipping rsync to ${htdocspath}.</echo>
<exec executable="/usr/local/bin/rsync" if:true="${doinstall}">
<arg value="--exclude='*.log'" />
<arg value="--chmod=ugo=rwX" />
<arg value="-rltDq" />
<arg value="../info"/>
<arg value="../sbin"/>
<arg value="../bin"/>
<arg value="../cec"/>
<arg value="../cec.list.html"/>
<arg value="../cgi"/>
<arg value="../common"/>
<arg value="../dblib"/>
<arg value="../etc"/>
<arg value="../ext-2.3.0"/>
<arg value="../framework"/>
<arg value="../help"/>
<arg value="../icon-loading-animated.gif"/>
<arg value="../images"/>
<arg value="../img"/>
<arg value="../include"/>
<arg value="../index.shtml"/>
<arg value="../kicker"/>
<arg value="../lib"/>
<arg value="../msgs"/>
<arg value="../src"/>
<arg value="../thingold"/>
<arg value="../thing"/>
<arg value="${htdocspath}"/>
<redirector output="/tmp/rsync.log" alwayslog="true"/>
</exec>
</target>
</project>
When I run sencha app build which invokes ant I only get this output from by build.xml:
Install Dev version on tools-dev1? (y, n)
There is no output from my <echo... tags and if I answer 'n' I can that the rsync command executes. What am I doing wrong here?
I simplified your build.xml
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="PSOGN" >
<input message="Install ?" addproperty="install" validargs="y,n" />
<condition property="doinstall">
<equals arg1="${install}" arg2="y" />
</condition>
<echo>do install is ${doinstall}</echo>
<echo if:true="${doinstall}">Please wait. Executing rsync</echo>
<echo unless:true="${doinstall}">Skipping rsync </echo>
<exec executable="echo" if:true="${doinstall}">
<arg value="ECHO IS EXECUTED" />
<redirector output="./rsync.log" alwayslog="true"/>
</exec>
</project>
and I replaced if:false with unless:true
<echo unless:true="${doinstall}">Skipping rsync </echo>
Everything works fine:
oleg#oleg-ThinkPad-X201:~/temp/aa$ ant
Buildfile: /home/oleg/temp/aa/build.xml
[input] Install ? (y, n)
y
[echo] do install is true
[echo] Please wait. Executing rsync
[exec] ECHO IS EXECUTED
BUILD SUCCESSFUL
Total time: 2 seconds
oleg#oleg-ThinkPad-X201:~/temp/aa$ ant
Buildfile: /home/oleg/temp/aa/build.xml
[input] Install ? (y, n)
n
[echo] do install is ${doinstall}
[echo] Skipping rsync
BUILD SUCCESSFUL
Total time: 2 seconds

Any analog of rsync for Ant?

I need some analog of rsync for ant. The issue is to copy files from source directory to the set of subdirectories that was previously accomplished with script
rsync -r --ignore-existing $BASE_DIR/common/config/* $BASE_DIR/config
Thanks for any help
You can use exec to call rsync from Ant, you could use the java task to call Jarsync or java-sync or you could create a custom ant task to call either of those libraries.
Zombie question but posting https://gist.github.com/garethr/878364 in case I ever search for it myself again. Pasting Gist content in case something something.
<project name="{{ name }}" default="help" basedir=".">
<property name="username" value="{{ username }}"/>
<property name="host" value="{{ host }}"/>
<property name="dir" value="/srv/{{ path }}/"/>
<tstamp>
<format property="TODAY_UK" pattern="yyyyMMddhhmmss" locale="en,UK"/>
</tstamp>
<target name="help" description="show available commands" >
<exec executable="ant" dir="." failonerror="true">
<arg value="-p"/>
</exec>
</target>
<target name="deploy-to" description="show where we are deploying to" >
<echo>${username}#${host}:${dir}</echo>
</target>
<target name="deploy" description="deploy usng rsync" >
<exec executable="rsync" dir="." failonerror="true">
<arg value="-r"/>
<arg value="."/>
<arg value="${username}#${host}:${dir}"/>
<arg value="--exclude-from=rsync.excludes"/>
<arg value="-v"/>
</exec>
</target>
<target name="deploy-test" description="test deploy usng rsync with the dry run flag set" >
<exec executable="rsync" dir="." failonerror="true">
<arg value="-r"/>
<arg value="."/>
<arg value="${username}#${host}:${dir}"/>
<arg value="--exclude-from=rsync.excludes"/>
<arg value="--dry-run"/>
<arg value="-v"/>
</exec>
</target>
<target name="backup" description="backup site" >
<exec executable="scp" dir="." failonerror="true">
<arg value="-r"/>
<arg value="${username}#${host}:${dir}"/>
<arg value="backups/${TODAY_UK}"/>
</exec>
</target>
</project>

How to pass a list of files to <exec>?

This is a part of my ant script:
<target>
<exec executable="find" outputproperty="found">
<arg value="src/main/java"/>
<arg line="-name '*.java'"/>
</exec>
<exec executable="xgettext">
<arg value="-k_"/>
<arg line="-o gettext.pot"/>
<arg line="${found}"/>
</exec>
</target>
Doesn't work because xgettext receives a quoted list of files and treats this list as a single file name. How to solve it?
You'd need to separate out each file to a separate arg for that to work.
You can supply a list-of-files file to process to 'xgettext' using the --files-from option.
How about something like this: write the 'find' output to a file, then reload into 'xgettext':
<target>
<exec executable="find" outputproperty="found">
<arg value="src/main/java"/>
<arg line="-name '*.java'"/>
</exec>
<echo file="xgettext.files" message="${found}" />
<exec executable="xgettext">
<arg value="-k_"/>
<arg value="-o" />
<arg value="gettext.pot"/>
<arg value="--files-from=xgettext.files"/>
</exec>
</target>
Alternatively, here's a variation that assumes you have the Bourne Shell sh - if you have something else you can probably adapt. This pipes the 'find' output directly to 'xgettext':
<exec executable="sh">
<arg value="-c"/>
<arg value="find src/main/java -name '*.java' | xgettext -k_ -o gettext.pot -f -"/>
</exec>

Resources