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

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

Related

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

No output running yui-compressor from Ant

I'm going to minify my js files by yuicomressor via ant, I wrote this:
<property name="concat-js-file-name" value="main.concat.js"/>
<property name="concat-js-file-path" value="${temp-folder}/js/${concat-js-file-name}"/>
<property name="yui-jar-path" value="lib/yuicompressor-2.4.7.jar"/>
<target name="minification" depends="concatation">
<echo>---Minification is started</echo>
<java jar="${yui-jar-path}" fork="true">
<arg value="${concat-js-file-path}"/>
<arg value="-o minified.js"/>
</java>
<echo>---Minification is finished successfully...</echo>
</target>
The problem is the output file is not generated!
Any idea?
You should set <java ... failonerror="true"/> and increase the noiselevel to see what's going on, means start your ant build with ant -f yourbuild.xml -debug
Actually, after some tries I found a solution:
I used <arg line="-o outputfile inputfile"/> instead, and it worked.
I suggest using <arg value="..."> instead of <arg line="...">. <arg value="..."> ensures that each command line argument has quotes around it, if necessary.
In the case of yui-compressor, the "-o" and "<file>" arguments should each go in their own <arg value="..."> elements:
<java jar="${yui-jar-path}" fork="true">
<arg value="-o"/>
<arg value="minified.js"/>
<arg value="${concat-js-file-path}"/>
</java>

ANT: Starting a background process on windows

I want ANT to execute a .cmd file on windows machine... but the command prompt should not be displayed. the process should be running in background. Ant should just fire the command and proceed with the next command.
I even tried using the windows "/B" switch, but it did not work.
Does anybody have a suggestion?
Use exec task like that :
<exec executable="cmd" spawn="true">
<arg value="/c"/>
<arg value="foobar.cmd"/>
</exec>
That should be sufficient, otherwise if you need to use start /B ... :
<exec executable="cmd">
<arg value="/c"/>
<arg value="start"/>
<arg value="/b">
<arg value="foobar.cmd"/>
</exec>
See ant manual exec task for details.

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>

How can I execute a VBScript file from Ant script?

I want to run a VBScript file from an Ant script. How can I do that?
Have a look at the exec task. It allows you to execute a system command from your Ant script.
EDIT:
An example could be:
<target name="RunVbScript">
<exec executable="cscript">
<arg value="MyScript.vbs"/>
<arg value="argument 1" />
<arg value="argument 2" />
<arg value="argument n" />
</exec>
</target>

Resources