Ant:条件复制

mai*_*rgs 13 ant conditional

如果用户允许,我想覆盖Windows机器上的hosts文件:

<input message="Do you want to overwrite the HOSTS file?"
       addproperty="overwrite.hosts" validargs="yes,no" />

<copy tofile="${env.WINDIR}/system32/drivers/etc/hosts.backup">
    <fileset file="${env.WINDIR}/system32/drivers/etc/hosts" />
</copy>

<copy todir="${env.WINDIR}/system32/drivers/etc">
    <fileset file="${trainer.dir}/hosts" />
</copy>
Run Code Online (Sandbox Code Playgroud)

仅当用户说"是"时,我该如何处理副本?

编辑:

我试过这个:

    <input message="Do you want to overwrite the HOSTS file?" addproperty="overwrite.hosts" validargs="yes,no" />

    <if>
        <equals arg1="${overwrite.hosts}" arg2="yes" />
        <then>
            <copy tofile="${env.windir}/system32/drivers/etc/hosts.backup">
                <fileset file="${env.windir}/system32/drivers/etc/hosts">
                </fileset>
            </copy>

            <copy todir="${env.windir}/system32/drivers/etc">
                <fileset file="${trainer.dir}/hosts">
                </fileset>
            </copy>
        </then>
    </if>
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

C:\trainer\build.xml:16: Problem: failed to create task or type if
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.
Run Code Online (Sandbox Code Playgroud)

我是一个蚂蚁新手...我需要做什么?

mik*_*iku 9

你可以使用一个condition或一个if任务.(后者是ant-contrib项目的一部分.)

  • 很长一段时间没有使用过ant,所以我不确定命名法,但实际上在docs`条件`下出现在任务下(http://ant.apache.org/manual/Tasks/). (2认同)

Jay*_*Jay 7

您可以在目标上使用"if"参数,使其以所设置的属性为条件.

我从来没有使用过"输入"任务 - 我直到现在才知道它存在(感谢抬头!) - 但是快速查看文档表明它将命名属性设置为输入的值,即在"输入"之后,始终设置属性.所以我猜你需要一个"条件"来测试值并设置或不设置其他属性.

像这样的东西.我刚刚进行了快速测试,这确实有效.也就是说,如果您回答问题"y",则会打印出该消息,如果您回答"n"则不会.

<project name="test" default="do.whatever">
  <target name="decide.do.whatever">
    <input message="So you wanna do this or not?" validargs="y,n" addproperty="wanna"/>
    <condition property="wanna.yes">
      <equals arg1="${wanna}" arg2="y"/>
    </condition>
  </target>
  <target name="do.whatever" depends="decide.do.whatever" if="wanna.yes">
    <echo message="Yeah he wannas."/>
  </target>
</project>
Run Code Online (Sandbox Code Playgroud)