【问题标题】:How to execute an interactive application from Ant build script?如何从 Ant 构建脚本执行交互式应用程序?
【发布时间】:2011-09-12 07:16:13
【问题描述】:

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

请注意,您无法与 分叉程序,唯一的发送方式 输入是通过输入和 输入字符串属性。另请注意 从 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>

【问题讨论】:

    标签: ant console exec task interactive


    【解决方案1】:

    我试过在 cosnole 上运行,如果你不分叉它可以工作。 正如文档中提到的那样。

    除了 eclipse 之外,还有其他配置 inputhandler 的方法。

    正如这里所承认的那样。 http://www.coderanch.com/t/419646/tools/java-program-accept-user-input

    完成这项工作的干净方法 http://www.myeclipseide.com/PNphpBB2-viewtopic-t-25337.html

    【讨论】:

    • 它在我的用例中不起作用(请参阅修订后的问题)。你能在你的系统中测试我的 Ant 脚本并告诉我它是否有效吗?
    【解决方案2】:

    您可以通过 shell 启动您的命令,将标准输入/输出/错误从/到/到/dev/tty,它对应于进程的controlling terminal。

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

    【讨论】:

      猜你喜欢
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多