Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute an interactive application from Ant build script?

From http://ant.apache.org/manual/Tasks/exec.html :

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes. Also note that since Ant 1.6, any attempt to read input in the forked program will receive an EOF (-1). This is a change from Ant 1.5, where such an attempt would block.

How do I launch and interact with interactive console program from ant?

What I want to do is similar to drush sqlc functionality, that is launch the mysql client interpreter using the proper database credentials, but not limited to this use case.

Here's a sample use case:

<project name="mysql">
  <target name="mysql">
    <exec executable="mysql">
      <arg line="-uroot -p"/>
    </exec>
  </target>
</project>

When run using ant :

$ ant -f mysql.xml mysql
Buildfile: /home/ceefour/tmp/mysql.xml

mysql:
Enter password:

BUILD SUCCESSFUL
Total time: 2 seconds

After inputting password, it immediately exits.

Compare this with what happens when executing directly on the shell (expected behavior):

$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1122
Server version: 5.1.58-1ubuntu1 (Ubuntu)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
like image 865
Hendy Irawan Avatar asked Jun 10 '11 07:06

Hendy Irawan


People also ask

How do I run an Ant build script?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

How do I run a specific target in Ant?

Enclose the task name in quotes. Targets beginning with a hyphen such as "-restart" are valid, and can be used to name targets that should not be called directly from the command line. For Ants main class every option starting with hyphen is an option for Ant itself and not a target.


1 Answers

You can launch your command via a shell, redirecting standard input/output/error from/to/to /dev/tty, which corresponds to the controlling terminal of the process.

<target name="dbshell" description="Open a shell for interactive tasks">
  <exec executable="/bin/sh">
    <arg value="-c"/>
    <arg value="mysql -u root -p &lt; /dev/tty &gt; /dev/tty 2&gt; /dev/tty"/>
  </exec>
</target>
like image 68
Sam Morris Avatar answered Nov 10 '22 00:11

Sam Morris