java 带管道的 Ant 运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1187402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ant run command with pipes
提问by Le_Coeur
I must to implement command : java -jar test.jar page.xml | mysql -u user -p basein ant. So i Have tried with this task:
我必须执行命令:java -jar test.jar page.xml | mysql -u user -p base在蚂蚁中。所以我尝试过这个任务:
<java jar="test.jar" fork="true">
<arg line="page.xml | mysql -u user -p base"/>
</java>
But i have got en exception with pipe - "|" :
但是我有管道例外 - “|” :
java.lang.IllegalArgumentException: Input already set; can't set to |
So, that's the problem:)
所以,这就是问题所在:)
回答by Brian Agnew
The pipe (|) can only be used in a shell script. You're passing it as an argument to the java process.
管道 (|) 只能在 shell 脚本中使用。您将它作为参数传递给 java 进程。
So you need to execute a shell script. You can do this by executing (say) bash -cand passing the above as a shell statement (albeit inline- you could write a separate script file but it seems a bit of an overhead here)
所以你需要执行一个shell脚本。您可以通过执行(例如)bash -c并将上述内容作为 shell 语句传递(尽管是内联的- 您可以编写一个单独的脚本文件,但在这里似乎有点开销)来完成此操作
<exec executable="bash">
<arg value="-c"/>
<arg line="java -jar test.jar page.xml | mysql -u user -p base"/>
</exec>
回答by Le_Coeur
I don't know if this was ever resolved, but I was having a similar problem which I solved by using the following:
我不知道这是否曾经解决过,但我遇到了类似的问题,我使用以下方法解决了这个问题:
<exec executable="bash">
<arg value="-c"/>
<arg line='"java -jar test.jar page.xml | mysql -u user -p base"'/>
</exec>
Just thought I would share.
只是想我会分享。
回答by Grzegorz Oledzki
Another solution would be to wrap the java -jar test.jar page.xml | mysql -u user -p baseinto a separate script and call it with simple <exec>task.
另一种解决方案是将其包装java -jar test.jar page.xml | mysql -u user -p base到一个单独的脚本中并使用简单的<exec>任务调用它。
回答by skaffman
When you run a java program from Ant, the input and out from the program are captured by the Ant runtime - you can't try and redirect them elsewhere using that pipe.
当您从 Ant 运行 java 程序时,程序的输入和输出被 Ant 运行时捕获 - 您不能尝试使用该管道将它们重定向到其他地方。
If you want to do that, you might have better luck with the exectask, although that might suffer from the same problem.
如果您想这样做,您可能会在exec任务中获得更好的运气,尽管这可能会遇到同样的问题。
回答by Bhushan Bhangale
There you are actually running a java command.
在那里您实际上正在运行一个 java 命令。
You need to use Exec task http://ant.apache.org/manual/Tasks/exec.htmlbut not sure if there also you can run piped commands or not. Give it a try.
您需要使用 Exec 任务http://ant.apache.org/manual/Tasks/exec.html但不确定是否也可以运行管道命令。试一试。
回答by trash80
Explaining why Amilie's answer is the correct solution:
解释为什么 Amilie 的答案是正确的解决方案:
The difference between Amilie's correct solution compared to Brain Agnew's solution is the subtle difference between them. Brian had the second arg as a "value" while Amilie is using "line".
Amilie 的正确解决方案与 Brain Agnew 的解决方案之间的区别在于它们之间的细微差别。当 Amilie 使用“line”时,Brian 将第二个 arg 作为“值”。
Here's why Amilie's is correct, per Apache Ant Documentation:
根据Apache Ant 文档,这就是为什么 Amilie 是正确的:
"value | a single command-line argument; can contain space characters."
"line | a space-delimited list of command-line arguments."
“值 | 单个命令行参数;可以包含空格字符。”
"line | 以空格分隔的命令行参数列表。"
<exec executable="bash">
<arg value="-c"/>
<arg line='"java -jar test.jar page.xml | mysql -u user -p base"'/>
</exec>

