ANT: Using conditional tags,

2019-02-09 01:08发布

I would like to do something like this

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

As per suggestions found on stackoverflow.com, i downloaded ant-contrib-1.0b3.jar and put it into my path

Classpath

Additionally, under Ant Build configuration, in classpath, i have enter image description here

When running my ANT script, i still get

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.

What am i missing? Please advise

6条回答
虎瘦雄心在
2楼-- · 2019-02-09 01:27

On the tasks tab for your Ant Runtime do you see the 'if' task?

查看更多
别忘想泡老子
3楼-- · 2019-02-09 01:32

export ANT_HOME=/path/to/ant/apache-ant-1.8.2 I couldn't get it to work until I had ANT_HOME set properly. Java kept picking another ant installed on the box.

查看更多
疯言疯语
4楼-- · 2019-02-09 01:38

Same issue resolved in that way. I put at the start of the buid XML file the following lines:

<taskdef resource="net/sf/antcontrib/antlib.xml">
  <classpath>
    <pathelement location="your/path/to/ant-contrib-${version}.jar" />
  </classpath>
</taskdef>
查看更多
对你真心纯属浪费
5楼-- · 2019-02-09 01:39

I got the same issue. I resolved in the following way. Sometimes this might be compatibility problem.

  • Right click on build.xml.
  • Go to "Run As" --> 2 Ant... Select Classpath tab check Ant Home version (Sometimes eclipse selects default ant version).
  • If the version listed is different, then change Ant Home Classpath to C:\XXXX\ant\X.X.X.
  • Finally click on the User Entries --> Add External JARS..--> add ant-contrib.x.x.jar form C:\XXXX\ant\X.X.X\ant-contrib\ directory.
查看更多
Ridiculous、
6楼-- · 2019-02-09 01:47

Alternately, once can include the following line in your ANT script

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

As long as ant-contribs is in your path, nothing else is needed

This solution is a bit cleaner, as one gains access to ALL the tags, not just the ones manually specified

查看更多
Explosion°爆炸
7楼-- · 2019-02-09 01:50

The standard ant way would be something like =

 <target name="check">
  <condition property="delbuild">
    <available file="${build}" type="dir"/>
  </condition>
 </target>

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

A snippet with the Ant Plugin Flaka, a recent alternative to antcontrib. Installation in Eclipse as usual via
Preferences | Ant | Runtime | Global Entries | 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>

output with ant -f build.xml -Dbuildtype=prod or
ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever

[echo] ..starting ProductionBuild

output with typo => ant - build.xml -Dbuildtype=testt

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

output with ant -f build.xml -Ddummybuild=whatever

[echo] .. is DummyBuild
查看更多
登录 后发表回答