开发者

ANT: Using conditional tags, <IF>

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

ANT: Using conditional tags, <IF>

Additionally, under Ant Build configuration, in classpath, i have

ANT: Using conditional tags, <IF>

When running my ANT script, i sti开发者_开发问答ll 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


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


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>


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜