You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Rhino <rh...@sympatico.ca> on 2004/10/19 16:44:21 UTC

Running Ant from a Windows batch file

I would like to be able to run an Ant (1.6.1) script from a Windows batch file on XP. Unfortunately, my Ant script contains <input> tasks and they are messing me up. 

Here is my batch file, cond3.bat:
------------------------------------
rem Batch file to run an Ant script.

rem This variable is the drive that contains the executable program. 
set progdrive=d:

rem This variable sets the path to the script.
set Script=eclipse\workspace\resume\xml

rem Get into the directory containing the program.
%progdrive%
cd %Script%

rem This command runs the program.
ant -f cond3.xml

pause
------------------------------------

This is my Ant script:

------------------------------------
<?xml version="1.0"?>

<project name="cond3" default="end" basedir=".">

<!--==================================================================

Determine which server is the target.

==================================================================-->

<target name="getserver" description="Determine which server is the target">

<input message="Which server should receive the files? 1. Foo 2. Bar 3. Test" 

validargs="1,2,3"

addproperty="server.choice" 

defaultvalue="2"/>

<condition property="servername" value="foo">

<equals arg1="${server.choice}" arg2="1"/>

</condition>

<condition property="servername" value="bar">

<equals arg1="${server.choice}" arg2="2"/>

</condition>

<condition property="servername" value="test">

<equals arg1="${server.choice}" arg2="3"/>

</condition> 

</target> 

<!--==================================================================

Load the properties file for the appropriate server.

==================================================================-->

<target name="getprops" depends="getserver" description="Get the appropriate properties file depending on the server which was chosen">

<property file="server.${servername}.properties"/>

</target>

<!--==================================================================

Get the userid and password for the desired server.

==================================================================-->

<target name="getlogin" depends="getprops" description="Get userid and password for server.">

<input message="Please supply the userid for the ${server} server:" addproperty="userid" defaultvalue="dougb"/>

<input message="Please supply the password for the ${server} server:" addproperty="password" defaultvalue="dougbpw"/>

</target> 

<!--==================================================================

Execute the appropriate upload target, depending on which server 

was chosen.

==================================================================-->

<target name="upload" depends="getlogin" description="Upload to the selected server.">

<antcall target="upload-${servername}"/> 

</target>

<!--==================================================================

Upload to the 'foo' server.

==================================================================-->

<target name="upload-foo" description="Upload to the foo server.">

<echo message="Uploading to foo...."/>

<echoproperties prefix="server"/>

</target>

<!--==================================================================

Upload to the 'bar' server.

==================================================================-->

<target name="upload-bar" description="Upload to the bar server.">

<echo message="Uploading to bar...."/>

<echoproperties prefix="server"/>

</target>

<!--==================================================================

Upload to the 'test' server.

==================================================================-->

<target name="upload-test" description="Upload to the test server.">

<echo message="Uploading to test...."/>

<echoproperties prefix="server"/>

</target>

<!--==================================================================

Display a message to the user.

==================================================================-->

<target name="end" depends="upload" description="Display a message.">

<echo message="Uploads to server ${servername} are complete."/>

</target>

</project>

------------------------------------

So far my script works fine when run from a batch file except that the command window closes very quickly upon completion of the script and the user can't see the message generated by the 'end' target, even though I have a Windows 'pause' command at the end of the batch file. The 'pause' command works on other batch files I've written so I don't see why it doesn't in this case.

As a workaround, I tried changing the last lines of the batch file to say:

ant -f cond3.xml > cond3.out
notepad cond3.out

but this doesn't work properly either. In this case, the script starts fine but the prompts, like "Which server should receive the files? 1. Foo 2. Bar 3. Test"  from the input task don't appear in the command window so the user doesn't understand what the command window is waiting for.

Does anyone know how I can have my cake and eat it too, in other words run Ant from a batch file, see the prompts for inputs, and see the output messages from Ant?


Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies." - C.A.R. Hoare

Re: Running Ant from a Windows batch file

Posted by Sebastian Redl <wa...@gmx.net>.
Rhino wrote:

>  
> I would like to be able to run an Ant (1.6.1) script from a Windows 
> batch file on XP. Unfortunately, my Ant script contains <input> tasks 
> and they are messing me up.
>  
> Here is my batch file, cond3.bat:
> ------------------------------------
> rem Batch file to run an Ant script.
>  
> rem This variable is the drive that contains the executable program.
> set progdrive=d:
>  
> rem This variable sets the path to the script.
> set Script=eclipse\workspace\resume\xml
>  
> rem Get into the directory containing the program.
> %progdrive%
> cd %Script%
>  
> rem This command runs the program.
> ant -f cond3.xml
>  
> pause


> So far my script works fine when run from a batch file except that the 
> command window closes very quickly upon completion of the script and 
> the user can't see the message generated by the 'end' target, even 
> though I have a Windows 'pause' command at the end of the batch file. 
> The 'pause' command works on other batch files I've written so I don't 
> see why it doesn't in this case.
>  


The pause command is never executed. Windows batch files are weird 
beasts. Control returns to them after the program they call has 
finished, but it does not return to them when they call another batch 
file. Weird, huh? Especially considering that the call syntax is the same.
ant is a batch file. Specifically, you're calling %ANT_HOME%\bin\ant.bat 
(or ant.cmd). Thus, execution doesn't return to your batch file after 
ant finishes.

The solution is to use the special call keyword, which ensures that 
execution returns:
call ant -f cond3.xml

Sebastian Redl

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org