【问题标题】:ANT: Using conditional tags, <IF>ANT:使用条件标签,<IF>
【发布时间】:2011-06-13 21:48:08
【问题描述】:

我想做这样的事情

<target name="clean" description="clean">
    <if>
        <available file="${build}" type="dir" />
        <then>
            <delete dir="${build}" />
        </then>
    </if>
</target>

根据 stackoverflow.com 上的suggestions,我下载了 ant-contrib-1.0b3.jar 并将其放入我的路径中

此外,在 Ant Build 配置下,在类路径中,我有

运行我的 ANT 脚本时,我仍然得到

BUILD FAILED
C:\Repositories\blah\build.xml:60: 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.

我错过了什么?请告知

【问题讨论】:

    标签: ant tags if-statement


    【解决方案1】:

    或者,一次可以在您的 ANT 脚本中包含以下行

    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    

    只要ant-contribs 在您的路径中,就不需要其他任何东西

    这个解决方案更简洁,因为一个可以访问所有标签,而不仅仅是手动指定的标签

    【讨论】:

    【解决方案2】:

    以这种方式解决了同样的问题。 我在 buid XML 文件的开头添加了以下几行:

    <taskdef resource="net/sf/antcontrib/antlib.xml">
      <classpath>
        <pathelement location="your/path/to/ant-contrib-${version}.jar" />
      </classpath>
    </taskdef>
    

    【讨论】:

      【解决方案3】:

      标准的蚂蚁方式类似于 =

       <target name="check">
        <condition property="delbuild">
          <available file="${build}" type="dir"/>
        </condition>
       </target>
      
       <target name="delbuild" depends="check" if="delbuild">
       <delete dir="${build}"/>
          <!-- .. -->
       </target>
      

      带有Ant Plugin Flaka 的sn-p,是antcontrib 的最新替代品。像往常一样通过
      Preferences | 在 Eclipse 中安装蚂蚁 |运行时 |全球条目 | ant-flaka-1.02.-1.02.jar =

      <project xmlns:fl="antlib:it.haefelinger.flaka">
        <!-- some standalone if construct -->
        <fl:when test=" '${build}'.isdir ">
          <delete dir="${build}"/>
        </fl:when>
      
          <!-- some if/then/else construct -->
          <fl:choose>
           <!-- if -->
           <when test=" '${buildtype}' eq 'prod' ">
              <!-- then -->
              <echo>..starting ProductionBuild</echo>
           </when>
           <when test=" '${buildtype}' eq 'test' ">
              <!-- then -->
          <echo>..starting TestBuild</echo>
         </when>
          <!-- else -->
           <otherwise>
            <fl:unless test="has.property.dummybuild">
             <fail message="No valid buildtype !, found => '${buildtype}'"/>
            </fl:unless>
              <echo>.. is DummyBuild</echo>
           </otherwise>
          </fl:choose>
      </project>
      

      使用 ant -f build.xml -Dbuildtype=prod 或输出
      ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever

      [echo] ..starting ProductionBuild
      

      输出有错字 => ant - build.xml -Dbuildtype=testt

      BUILD FAILED
      /home/rosebud/workspace/AntTest/build.xml:21: No valid buildtype !, found => 'testt'
      

      使用 ant -f build.xml -Ddummybuild=whatever 输出

      [echo] .. is DummyBuild
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题。我通过以下方式解决。 有时这可能是兼容性问题。

        • 右键单击 build.xml。
        • 转到“运行方式”--> 2 Ant... 选择 Classpath 选项卡检查 Ant Home 版本(有时 eclipse 会选择默认的 ant 版本)。
        • 如果列出的版本不同,则更改 Ant Home Classpath 到 C:\XXXX\ant\X.X.X.
        • 最后点击 User Entries --> Add External JARS..--> add ant-contrib.x.x.jar 形成 C:\XXXX\ant\X.X.X\ant-contrib\ 目录。

        【讨论】:

        • 非常感谢!我必须在这里自己下载 jar 文件:goo.gl/xZe5c8
        【解决方案5】:

        在 Ant Runtime 的任务选项卡上,您是否看到“if”任务?

        【讨论】:

        • 嗯……我没有。您拥有的任务的名称是什么?我假设,我需要选择 ant-contrib jar 文件并选择包含任务的类?这是什么课?
        • 是的,这是正确的。我不使用“if”,但在这种情况下,它看起来像 net.sf.antcontrib.logic.IfTask 是相应的类。
        【解决方案6】:

        export ANT_HOME=/path/to/ant/apache-ant-1.8.2 在正确设置 ANT_HOME 之前,我无法让它工作。 Java 不断挑选安装在盒子上的另一只蚂蚁。

        【讨论】:

        • 对我来说是相似的。只是提到这一点,以提高意识,如果您正确包含“if”任务,也会发送此错误消息,但某些文件路径,例如所需的 jar 设置不正确
        猜你喜欢
        • 1970-01-01
        • 2019-11-19
        • 2011-05-19
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 2017-06-07
        • 1970-01-01
        • 2014-04-13
        相关资源
        最近更新 更多