Ant exec executable="cmd" with '-' issue - ant

So I am trying to execute some git commands in the old tag of ANT.
<exec executable="cmd" failonerror="true" dir = "D:\Test">
<arg value="/c"/>
<arg value="git remote -v"/>
</exec>
But I get this error:
[exec] error: unknown switch ` '
[exec] usage: git remote [-v | --verbose]
If I execute any other commant that is not having '-' in it it works fine.

Put each argument of the git command in its own <arg> element:
<exec executable="cmd" failonerror="true" dir="D:\Test">
<arg value="/c"/>
<arg value="git"/>
<arg value="remote"/>
<arg value="-v"/>
</exec>

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

How to access values passed from ant script to shell script

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>

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>

Running a .cmd file from Ant

Is it possible to run a command (.cmd file) from Ant? Would I need to write Java code for this?
<exec executable="cmd" os="Windows XP">
<arg value="/C"/>
<arg value="command to run"/>
</exec>
You can do this by using the exec task. From the ant exec 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.
So you would need to do something like:
<exec executable="cmd">
<arg value="/c"/>
<arg value="batchfile.cmd"/>
</exec>
Note that by doing this you have created a dependency of running your ant script in windows.
Adding to eradicus answer, you can also execute .bat, .cmd, ... from any directory with argument on your Window machine by
<target name="test">
<exec executable="cmd" dir="C:/Users/mydir/">
<arg value="/C" />
<arg value="myjob.bat arg1 arg2" />
</exec>
</target>

Ant exec refactoring

I've this code in my build.xml:
<exec executable="cmd" osfamily="winnt">
<arg value="/c"/>
<arg value="xsltproc\bin\xsltproc.exe"/>
<arg value="--xinclude"/>
<arg value="-o"/>
<arg value="dist/html/main.html"/>
<arg value="xsl/html/docbook.xsl"/>
<arg value="xml/main.xml"/>
</exec>
<exec executable="xsltproc" osfamily="unix">
<arg value="--xinclude"/>
<arg value="-o"/>
<arg value="dist/html/main.html"/>
<arg value="xsl/html/docbook.xsl"/>
<arg value="xml/main.xml"/>
</exec>
the sequence is the same... I'm wondering about how refactoring this small fragment in order to keep it DRY.
maybe try using a property for the common bits with arg-line? something like this:
<property name="xslt.common" value="--xinclude -o dist/html/main.html xsl/html/docbook.xsl xml/main.xml"/>
<exec executable="cmd" osfamily="winnt">
<arg value="/c"/>
<arg value="xsltproc\bin\xsltproc.exe"/>
<arg line="${xslt.common}"/>
</exec>
<exec executable="xsltproc" osfamily="unix">
<arg line="${xslt.common}"/>
</exec>
Define a macro.
You can glob the shared portions in an element, and conditionally execute the specific parts.
I think the Unix version will work under NT if you have the xsltproc.exe available in via the PATH environment variable. You could try removing the osfamily and see.

Resources