ANT:使用条件标签,

问题描述 投票:10回答:6

我想做这样的事情

<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配置下,在classpath中,我有

运行我的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
6个回答
9
投票

或者,一旦在ANT脚本中包含以下行

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

只要ant-contribs在你的道路上,就没有别的东西了

此解决方案更清晰,因为可以访问所有标记,而不仅仅是手动指定的标记


5
投票

以这种方式解决了同样的问题。我在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的片段,最近替代antcontrib。像往常一样在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 = what

[echo] ..starting ProductionBuild

使用拼写错误输出=> ant - build.xml -Buildtype = test

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

输出与ant -f build.xml -Ddummybuild =无论如何

[echo] .. is DummyBuild

3
投票

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

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

1
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.