提示用户输入ant并根据输入执行操作

时间:2015-05-28 21:59:30

标签: ant

我希望能够在执行ant期间提示用户输入。 我想要做的伪代码是:

echo "Enter y or n:"
read yorn
if [ $yorn = 'y' ]
then
   echo "$yorn" > /tmp/junk
fi

这是我拥有的,但不起作用:

 41         <target name="-after-build">
 42                 <input message="What's your name?" addproperty="your-name" validargs="Bob,Fred" />
 43                 <echo>What's up, ${your-name}?</echo>
 44                 <exec executable="/bin/echo">
 45                         <arg value="${your-name}"/>
 46                         <redirector output="/tmp/junk" alwayslog="true"/>
 47                 </exec>
 48                 <target name="ask_if_install">
 49                         <input message="do you want to run rsync?" addproperty="install_it" validargs="y,n" />
 50                 </target>
 51                 <target name="install.it"
 52                         if="install_it"
 53                         depends="ask_if_install">
 54                         <exec executable="/bin/echo">
 55                                 <arg value="${install_it}"/>
 56                                 <redirector output="/tmp/junk2" alwayslog="true"/>
 57                         </exec>
 58                 </target>
 59         </target>

以下是我得到的输出:

build.xml:48: Problem: failed to create task or type target
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

1 个答案:

答案 0 :(得分:2)

您可以使用input task

<project  xmlns:if="ant:if"  xmlns:unless="ant:unless">

    <input  message="Enter y or n:" validargs="y,n" addproperty="yes.or.no" />

    <condition property="yes.is.true">
      <equals arg1="${yes.or.no}" arg2="y" />
    </condition>


    <concat destfile="junk" if:true="${yes.is.true}">${yes.or.no}</concat>

</project>

编辑:您几乎可以在所有蚂蚁任务中使用if:true, if:set, unless:true,...

<exec executable="echo" if:true="${yes.is.true}">
    <arg value="${yes.or.no}" />
</exec>