无法通过 Ant 运行 Windows 命令,但可以在 cmd.exe 上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4868758/
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
Cannot run a Windows command via Ant, but can on cmd.exe
提问by Mick Knutson
I have an ant macro I want to run;
我有一个要运行的蚂蚁宏;
<macrodef name="serviceTask">
<attribute name="server"/>
<attribute name="operation"/>
<attribute name="service"/>
<sequential>
<echo message="sc \@{server} @{operation} @{service}"/>
<exec executable="sc.exe" failonerror="true">
<arg line="\@{server} @{operation} @{service}"/>
</exec>
</sequential>
</macrodef>
<target name="startTomcat">
<echo message="Start Tomcat ${service} on ${server}"/>
<serviceTask server="MyServer" operation="start" service="Tomcat8180"/>
</target>
But I get an RPC error:
但我收到一个 RPC 错误:
startTomcat:
[echo] Start Tomcat Tomcat8180 on pacdcdtadeva02
[echo] sc \pacdcdtadeva02 start Tomcat8180
[exec] [SC] OpenSCManager FAILED 1722:
[exec]
[exec] The RPC server is unavailable.
[exec]
stopTomcat:
[echo] Stop Tomcat Service Tomcat8180 on pacdcdtadeva02
[echo] sc \pacdcdtadeva02 stop Tomcat8180
[exec] [SC] OpenSCManager FAILED 1722:
[exec]
[exec] The RPC server is unavailable.
[exec]
[echo] -------------------------------------------------------------------
[echo] --- Completed on 02/01/2011 05:11:42 PM
[echo] -------------------------------------------------------------------
Now when I run this from the command line like
现在,当我从命令行运行它时
sc \stage01 start Tomcat8180
the service starts/stops
服务开始/停止
C:\usr\svn_workspaces\xIVR\agent-ivr>sc \stage01 start Tomcat8180
SERVICE_NAME: Tomcat8180
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x1
WAIT_HINT : 0xbb8
PID : 11228
FLAGS :
回答by Raghuram
Perhaps this needs a shell environment. How about trying cmd /c sc ...
也许这需要一个shell环境。试试怎么样cmd /c sc ...
<exec executable="cmd.exe" failonerror="true">
<arg line="/c sc \@{server} @{operation} @{service}"/>
</exec>
回答by Ulrik
This may or may not help you, but I had problems with the <exec>
task with ant on a windows machine as well, in combination with the <arg line=..>
statement. Ant just refused to treat the line of parameters as separate entities with spaces between them and I got weird errors like "unrecognised parameter" when it should have been a valid one.
这可能对您有帮助,也可能无济于事,但是我<exec>
在 Windows 机器上的 ant 任务中也遇到了问题,结合<arg line=..>
语句。Ant只是拒绝将参数行视为单独的实体,它们之间有空格,当它应该是有效的时,我得到了奇怪的错误,例如“无法识别的参数”。
My problems disappeared after I exchanged the one <arg line=>
with one <arg value=...>
for every parameter instead.
I also had to do it like Raghuram specified, and wrap everything in a shell environment as well.
在我将每个参数的一个<arg line=>
与一个交换后,我的问题消失了<arg value=...>
。我还必须像 Raghuram 指定的那样做,并将所有内容也包装在 shell 环境中。
Might be worth a try.
也许值得尝试一下。