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

发布于 2024-11-15 04:59:20 字数 1509 浏览 3 评论 0 原文

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-11-22 04:59:20

您可以通过 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 < /dev/tty > /dev/tty 2> /dev/tty"/>
  </exec>
</target>

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 < /dev/tty > /dev/tty 2> /dev/tty"/>
  </exec>
</target>
狂之美人 2024-11-22 04:59:20

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

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

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

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

I have tried running on cosnole and if you do not fork it works.
As mentioned in the doc too.

Beside with eclipse there are additional ways to configure inputhandler.

As is acknowledged here.
http://www.coderanch.com/t/419646/tools/java-program-accept-user-input

A clean way to get this work
http://www.myeclipseide.com/PNphpBB2-viewtopic-t-25337.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文