如何在Ant构建脚本中执行交互式应用程序?

9

来自http://ant.apache.org/manual/Tasks/exec.html

请注意,您无法与分叉的程序交互,发送输入的唯一方法是通过input和inputstring属性。此外,请注意自Ant 1.6以来,任何尝试在分叉的程序中读取输入的尝试都将收到EOF(-1)。这是从Ant 1.5开始的更改,早期尝试会被阻塞。

如何从Ant启动并与交互式控制台程序交互?

我想要做的类似于drush sqlc功能,即使用正确的数据库凭据启动mysql客户端解释器,但不仅限于此用例。

这里是一个示例用例:

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

使用ant运行时:

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

mysql:
Enter password:

BUILD SUCCESSFUL
Total time: 2 seconds

输入密码后,程序立即退出。

与在Shell上直接执行的情况进行比较(预期行为):

$ 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>
2个回答

3

您可以通过shell启动命令,并将标准输入/输出/错误重定向到/dev/tty,它对应于进程的控制终端

<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>

-1

在我的使用情况下它不起作用(请参见修订后的问题)。你能否在你的系统中测试我的Ant脚本并告诉我它是否有效? - Hendy Irawan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接