Thursday, November 03, 2011

How to run ANT script from .BAT/.CMD?

Imagine you need to call ANT script from BAT/CMD file.
Ant script:


 
  
 


And here is BAT file:

@echo off
echo BAT - Before Ant run
ant -f build.xml
echo BAT - After Ant run

Unfortunately, when you run this BAT you get the next output:
BAT - Before Ant run
Buildfile: D:\Projects\blog\build.xml

bat-test:
     [echo] Message from ANT

BUILD SUCCESSFUL
Total time: 0 seconds

So, where is the "BAT - After Ant run" echo message?

The problem is that Ant on Windows executed via ant.bat file and based on this we're calling one BAT file from another. We have to use CALL command to solve this issues, here is official note from CALL help:
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
batch-parameters   Specifies any command-line information required by the batch program. 
And, here is the update BAT file:

@echo off
echo BAT - Before Ant run
call ant -f build.xml
echo BAT - After Ant run

11 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Could you please provide the beanshell snippet?

    ReplyDelete
  3. I deleted the some lines. Sorry Orest Ivasiv.

    Here's the new one with the beanshell snippet at the end:

    In my case it doesn't work.
    The result of the command CALL ant -q -f C:\\...\\build.xml is:

    BUILD SUCCESSFUL
    Total time: 2 minutes 12 seconds

    I'm running it from beanshell. Any idea? Thank's in advance!

    -----------------------------------------------
    beanshell snippet:
    =================
    bat( "CALL ant -q -f C:\\DriveAMnet\\build.xml" );

    where bat is:

    abreBat( )
    {
    g_pwBat = new PrintWriter( new FileWriter( g_strBat ) );
    bat( ":\n@echo off\nsetlocal\nC:\necho. > LibVersao.log" );
    }

    bat( str )
    {
    g_pwBat.println( str );
    g_pwBat.flush( );
    }

    fechaBat( )
    {
    bat( "CD " + bsh.cwd + "\\" );
    g_pwBat.flush( );
    g_pwBat.close( );
    }

    ReplyDelete
  4. Adding just some explanation:

    abreBat()= openBat()
    fechaBat() = closeBat()
    g_strBat = "LibVersao_" + bsh.args[0] + ".bat";

    Again, thank's in advance!

    ReplyDelete
  5. bsh % exec("cmd /k call ant -f build.xml && exit");
    Buildfile: D:\temp\beanshell\build.xml

    bat-test:
    [echo] Message from ANT
    [echo]

    BUILD SUCCESSFUL
    Total time: 0 seconds
    bsh %

    ReplyDelete
  6. The main trick is to use "cmd /k" and "exit" commands. Have a look the "cmd /?" for more details.

    ReplyDelete
  7. You've got the point!!!
    It's working now! Thanks a lot!

    ReplyDelete
  8. If my calling BAT command is setting some environment variables, can the same be available to ant?

    ReplyDelete
  9. Ant has access to env vars: via adding "-Dvar=value" when calling ANT script and inside ANT script itself.

    Also, you can set env vars in ANT script:

    ReplyDelete
  10. I think you have got tons to brag concerning! and that i would not worry about being "overly boastful" as a result of you are tooting your own horn! i prefer to listen to the great stuff that folks do. It's far better than hearing the long list of crap and ailments they're experiencing over ... and over ... and all over again. Hearing dangerous stuff all the time is simply not smart.
    cricket-bat

    ReplyDelete